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?
