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");
})

1 Answer 1

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' });
Sign up to request clarification or add additional context in comments.

7 Comments

How can use 'run_at' property at MV3? At docuemnt link just say 'The script will be run at document_idle'. But I want to run at document_end
There's no run_at in MV3, see crbug.com/1217895.
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?
No, they just didn't understand why it was useful.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.