Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda