My injected [removed] runs after the target-page's javascript, despite using run_at: document_start?

后端 未结 4 2020
渐次进展
渐次进展 2021-01-01 19:36

I am having some problem with the order of javascript execution when I use the content script to inject some javascript into the HTML page:

This is my HTML page I us

4条回答
  •  情歌与酒
    2021-01-01 20:28

    JavaScript script tags are not blocking the execution (thankfully!). If you'd like to get the kind of functionality you want (lazy-loading), you'll need to do something like the following:

    function loadScript(scriptName, callback) {
       var sTag = document.createElement("script");
       sTag.src = scriptName;
       sTag.onreadystatechange = sTag.onload = function() {
        var state = s.readyState;
    
        if (!state || /loaded|complete/.test(state)) {
            callback();
        }
      };
     }
    

    This will fire a callback function when the script tag is loaded. From there, load everything and make sure that every callback is processed, then fire your page-load events in the sanity of mind that you have every library you need.

提交回复
热议问题