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

380
Visualizações
Is there a way to get a variable from another script?

I created a chrome extension, the extension needs to get a variable from a script to use it in another context, i can access the variable using browser console but i can't access it by my extension using window.VARIABLE_NAME

const getCodeBtn= document.getElementById('get-code-btn');

async function getCode() {
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    function: addCode,
  })
}

//we need to add code in a website input
function addCode() {
  const input = document.getElementById('some-id');
  const btn = document.getElementById('some-id');

  input.value = window.code;  //window.code is undefined
  console.log(window.code)  // undefined
  btn.click();
}

getCodeBtn.addEventListener('click', getCode)

I still can access the variable value in browser console

code;  // 1235469
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to documentation, from Chrome 95+ you can choose the execution environment to run the script in with the world parameter. So, in order to run in the web page's environment, your injection should be something like:

chrome.scripting.executeScript({
    target: { tabId: tab.id },
    func: addCode,
    world: 'MAIN'
  })
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