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

120
Visualizações
Inject async function into page

I am developing a chrome-extension, where I am trying to inject a function into the page. Injecting a regular function works without problems.

But as soon as I try to inject anything with async-functions, I get an error similar to this:

Uncaught ReferenceError: g is not defined

For example, injecting an async-function directly.

chrome.scripting.executeScript({
  target: { tabId: tab.id },
  func: async () => {
    console.log("Does it work?");
  }
});

Or even if I inject a regular function, but have an async function inside:

chrome.scripting.executeScript({
  target: { tabId: tab.id },
  func: () => {
    (async function () {
      console.log("Does it work?");
    })();
  }
});

It can't be right, that I am forced to write and inject synchronous code, making me unable to use the async/await functionality... Can it? What am I doing wrong?

EDIT: ---------------

Upon further investigation, I can see that this function

async () => { console.log("Does it work?"); };

Get translated to this

function(){return e.apply(this,arguments)}

Which explains the ReferenceError but still leaves me clueless as to why this happens.

EDIT2: ---------------

So I tried to resort to "synchronous" code for now. But hoped I at least could use promises without async/await - But also no...

  chrome.storage.local.set({ someArray: [] })
    .then(() => console.log("It worked?"));

The code above results in the following error:

Cannot read properties of undefined (reading 'then')

So no promises and no async/await for any injected code... Is this really the case?

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

0

Injecting async function should not cause a problem. I have included a minimal example below to demonstrate.

manifest.json

{
  "name": "TEST",
  "version": "0.0.1",
  "manifest_version": 3,
  "permissions": [
    "scripting",
    "activeTab"
  ],
  "host_permissions": [
    "<all_urls>"
  ],
  "background": {
    "service_worker": "background.js"
  }
}

background.js

function inject(tab) {
    chrome.scripting.executeScript({
        target: {tabId: tab.id},
        func: async () => {
            window.alert("I'm here!");
        }
    })
}

// first open tab with some web page -> try inject there
chrome.tabs.query({
    url: "https://*/*"
}, function (tabs) {
    inject(tabs.shift())
});

Regarding OP and each edit:

  • Uncaught ReferenceError: g is not defined - identifier g does not exist: check what it is and where is it supposed to be defined

  • EDIT 1: if using a compiler/bundler, renaming may be the source of a reference error

  • EDIT 2: chrome.storage.local.set docs on usage indicate it needs a callback . There is no return value, i.e. it is undefined, cannot chain a then after it.

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