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

151
Visualizações
chrome.scripting.executeScript takes too long to start at document_start

Building a sample Chrome extension, its sole purpose is to wrap/replace fetch and XMLHttpRequest.

It was easy with Manifest V2, but seems to be tricky with Manifest V3.

The replacement is achieved using chrome.scripting.executeScript, which is triggered immediately from a document_start content script, which sends a message to the background worker, which in turn uses chrome.scripting.executeScript on the calling tab.

The whole thing takes from 500ms to 1500ms, which usually misses many initial fetch/XMLHttpRequest requests, which is unacceptable for my use case - I need to control all requests.

Is it possible to delay scripts on the original tab until the replacement is finished? Am I doing something wrong here? Any other solution to guarantee a replacement before requests are made?

(using https://www.msnbc.com/ in this example, but tried many other websites)

manifest.json:

{
  "name": "v3ChangeFetchExample",
  "description": "v3ChangeFetchExample",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": ["tabs", "scripting"],
  "content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'"
  },
  "content_scripts": [
    {
      "all_frames": true,
      "js": ["script.js"],
      "matches": ["https://www.msnbc.com/*"],
      "run_at": "document_start"
    }
  ],
  "host_permissions": ["https://www.msnbc.com/*"]
}

background.js:

const replaceFetch = async function (message) {
  fetch = {};
  XMLHttpRequest = {};
  console.error(`Took ${Date.now() - message}ms`);
};

chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
  chrome.scripting.executeScript({
    world: "MAIN",
    target: { tabId: sender.tab.id, allFrames: true },
    func: replaceFetch,
    args: [message],
  });

  sendResponse(true);
});

script.js:

(function () {
  chrome.runtime.sendMessage(Date.now());
})();

Edit: Just found this in the documentation of chrome.scripting.executeScript:

Injects a script into a target context. The script will be run at document_idle.

This sheds some light on the issue. So maybe the question should be: Is it possible to execute a script on document_start, and not document_idle using Manifest V3?

about 4 years ago · Juan Pablo Isaza
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