3

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?

Tsury
  • 729
  • 1
  • 9
  • 24
  • Not possible, see https://crbug.com/1217895. Extensions like yours are currently completely broken in MV3. In the future you will use chrome.scripting.registerContentScripts to register a main-world script at document_start. – wOxxOm Feb 15 '22 at 13:26
  • Thanks @wOxxOm. What do you mean when you say in the future? I see in the docs that RegisteredContentScript is available starting from Chrome 96 and the public version is 98. Also seems like it has a run_at property that can be document_start, so I must be missing something else. – Tsury Feb 15 '22 at 17:18
  • 1
    It doesn't support `world` yet. – wOxxOm Feb 15 '22 at 17:23
  • @wOxxOm, how would your answer change now? The issue was just closed as fixed with this remark: This should be fixed and available for use in M102. – Tsury Apr 04 '22 at 17:05
  • 1
    It's simpler to use inline js in an attribute as shown in [method 3 here](/a/9517879). It'll work on this site because its CSP doesn't forbid inline js. – wOxxOm Apr 04 '22 at 17:11

0 Answers0