Overwrite native JS function with Chrome Extension (manifest V3)

Viewed 37

my aim is to override the default setInterval JS function in all pages with a browser extension (by injecting my javascript code just at the beginning of pages). This script should run before any other script, jQuery included. My latest attempt (not working) is this:

manifest.js

...
"content_scripts": [
    {
    "matches": ["<all_urls>"],
    "js": ["content.js"],
    "run_at": "document_start",
    "all_frames": true
    }
],
    "background": {
    "service_worker": "background.js"
},  
    "permissions": [
    "scripting"
],  
    "host_permissions": ["<all_urls>"],
...

content.js

 function do_newInterval() {
      window.setInterval = function setInterval() {console.log('hello')}
 };

 chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
      if (msg.type === 'newInterval') { do_newInterval(); }
 });

background.js

 chrome.tabs.onUpdated.addListener(async () => {
    let activeTab=await chrome.tabs.query({ active: true, 
    lastFocusedWindow: true })
    chrome.tabs.sendMessage(activeTab[0].id, {  type: "newInterval" 
    });
 })

Unfortunately the code to replace the setInterval function is run after other page scripts are executed!

Could you please show me the right way to make my JS run as first?

Thank you!

0 Answers
Related