Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!