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

132
Visualizações
chrome.scripting.executeScript pass parameter

I have this code in the popup.js file on my Chrome Extension:

chrome.scripting.executeScript({
  target: { tabId: tab.id },
  function: myFunc,
});


function myFunc() {
  // do something
}

However, I would like to pass a parameter from the executeScript to myFunc. Something like this:

chrome.scripting.executeScript({
  argument: {"arg1", "arg2"}
  target: { tabId: tab.id },
  function: connectCmd,
});


function myFunc(arg1: string, arg2: string) {
  // do something
}

or something equivalent. Is there any way to do this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Per the documentation the args (array of arguments) property is available since Chrome 92.

  • These arguments must be JSON-serializable i.e. string, number, boolean, null, array/object that consists of the mentioned types. In short the injected code will see the same thing as you can see by running JSON.parse(JSON.stringify(args)) meaning that you can't transfer classes, Set, Map, and other nontrivial objects.
  • You can use func property in Chrome 92+, which is the new preferred alias of function.
chrome.scripting.executeScript({
  args: ['foo', 'bar']
  target: { tabId: tab.id },
  func: myFunc,
});

function myFunc(arg1, arg2) {
  console.log(arg1, arg2); // will print `foo bar` in console of the web page
}

In the older Chrome you'll have to use messaging.

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