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

182
Visualizações
Run Google Analytics Request in a Chrome Extension Background Script

Here I have this code, which runs a request to google analytics (I checked, that part works in a popup script) and I would like it to send a daily "order" so to speak to let me know how many daily uses I have installed.

I have set the interval to 5 seconds for testing purposes, but this does not seem to work anyway.

function doSomething() {

 chrome.storage.sync.get('session', function(result){
var val = result.session;
 var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://bat.global.cf:8000/checkok.html?rando="+val);
xhttp.send();
xhttp.open("GET", "https://www.google-analytics.com/collect?v=1&tid=UA-232507651-1&cid="+val+"&t=pageview&dp=%2Fdaily24hCheck");
xhttp.send();
 });

}

setInterval(doSomething, 5000); // Time in milliseconds


/*Anonymous anylitics code for a background script  end*/

Is there something I am missing about background scripts? The request does not seem to be sent, how can I fix this?

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

0

You are missing the fact that they are not persistently executing. See the Migrating to Service Workers documentation:

It's common for web developers to perform delayed or periodic operations using the setTimeout or setInterval methods. These APIs can fail in service workers, though, because the scheduler will cancel the timers when the service worker is terminated.

The idiomatic way to achieve what you want here is to use the chrome.alarms API, which is also discussed at that link.

Note that "armed" alarms won't survive a full extension restart (e.g. if the browser is restarted), so you probably should save the last time your alarm code has successfully executed and check if you need to send a beacon / when should you schedule a new alarm on extension start (e.g. through chrome.runtime.onStartup).

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