Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Manifest v3 inject script from popup.js

In manifest v2 this code worked and injected the script when button was clicked:

popup.js v2 (works)

document.addEventListener('DOMContentLoaded', function () {
// Get button by ID
var button = document.getElementById('btnScan');

// Define button on click action
button.onclick = function () {
    chrome.tabs.executeScript(null, {
        file: 'Scripts/script.js'
    });
    window.close();
    }
});

Now in manifest v3, chrome.tabs.executeScript is replaced with chrome.scripting.executeScript.

scripting permission is added in manifest.json.

popup.js v3 (not working)

document.addEventListener('DOMContentLoaded', function () {
// Get button by ID
var button = document.getElementById('btnScan');

// Define Scan button on click action
button.onclick = function () {
    chrome.scripting.executeScript
        (
        {
            target: { tabId: null}, // ???????
            files: ['Scripts/script.js']
        }
        );
    window.close();
    }
});

The problem is that chrome.tabs.executeScript requires tabId value as one of the parameters. How can I get tabId value in popup.js or convert the manifest v2 version javascript so that it works the same?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Thanks to @wOxxOm who posted a link as a comment.

The solution was to get the active tab and use its tabId.

document.addEventListener('DOMContentLoaded', function () {
    // Get button by ID
    var button = document.getElementById('btnScan');
    button.onclick = injectScript;
});

async function injectScript() {
    const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
    await chrome.scripting.executeScript({
    target: { tabId: tab.id },
    files: ['Scripts/script.js']
    });
    window.close();
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!