0

In our firefox extension from the background script, we are checking the currently loading tab and check whether the URL is our desired URL if it is our desired URL then we are executing a javascript file on the tab with the help of browser.tabs.onupdated event using browser.tabs.executeScript and the file is executed successfully but the window.onload event present on the content script doesn't execute but the console statement on the first line executed in the content script

Background.js

 browser.tabs.onUpdated.addListener(
    function (tabId, changeInfo, tab) {
        // here checking wether to execute the script for the currently loading tab based on the URL of tab
        browser.tabs.executeScript(tab.id, { file: "jquery.min.js" });
         browser.tabs.executeScript(tab.id, { file: "automate.js" });
      
    },
    { properties: ["status"] }
  );

automate.js

console.log("automate.js executing");
window.addEventListener("load",function(){
// this console.log statement not printed
 console.log("window is loaded");
})
Janaravi
  • 125
  • 1
  • 1
  • 10

1 Answers1

0

By default content scripts run at document_idle:

The document and all its resources have finished loading.

Solution:

It is the same as load event conditions so you don't need it. Simply do what you need right away.

Alternative:

If for some reason you need to do something before the load event and something afterwards, you can use document_end to run the script at DOMContentLoaded event (DOM is ready, resources are still loading) or document_start (DOM is empty, only <html> node is parsed).

browser.tabs.executeScript(tab.id, { file: 'jquery.min.js', runAt: 'document_end' });
browser.tabs.executeScript(tab.id, { file: 'automate.js', runAt: 'document_end' });
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • How can use 'run_at' property at MV3? At docuemnt [link](https://developer.chrome.com/docs/extensions/reference/scripting/#method-executeScript) just say 'The script will be run at document_idle'. But I want to run at document_end – 4rigener Jun 28 '21 at 08:14
  • There's no run_at in MV3, see https://crbug.com/1217895. – wOxxOm Jun 28 '21 at 08:52
  • **the main use cases I can think of where having scripts inject before this point is useful (devtools, content blocking, polyfilling, etc) *also* require precise timing - which cannot be provided by this API.** That means they can't provide 'run_at' property because extension like adblock who hinder advertising revenue can use them? – 4rigener Jun 28 '21 at 13:59
  • No, they just didn't understand why it was useful. – wOxxOm Jun 28 '21 at 14:03
  • If I reply with example like **If don't have run_at property when want to delete a specific node or change the css style through chrome extension like Adblock(not just erase advertise), can't do that without flickering phenomenon**, will the project member accept it? – 4rigener Jun 28 '21 at 14:13
  • The bug was already accepted so hopefully it'll be fixed. You can add your own use case of course but make sure you include the necessary details because I didn't understand it from your description here. – wOxxOm Jun 28 '21 at 14:18
  • like this [example](https://stackoverflow.com/questions/32876835/flickering-during-css-injection-in-chrome-extension) what you answered before – 4rigener Jun 28 '21 at 14:19