N
N
Nick_Apex2017-02-02 18:30:17
JavaScript
Nick_Apex, 2017-02-02 18:30:17

Loading remote js script from js script (google chrome extensions)?

Not a bad name, right?)
Initially, we have an extension for google chrome that can run a script packaged in the extension itself. In order to update the script, you need to collect it and re-upload it in your google developer account.
The essence of the task is to put a code in the built-in script that will load another script from a remote server and execute it on the page being loaded (instead of updating). There was an idea to create a new html node and point the src there to the server with the script

function include_js(file) {
    var html_doc = document.getElementsByTagName('head')[0];
    js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', 'http://server.com/script.js');
    html_doc.appendChild(js);
}
include_js();
but that's bad luck - when the extension creates this line, the browser will execute this part of the html page a long time ago ... Please help, friends)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2017-02-03
@Nick_Apex

function loadScript(url,callback) {
    var script = document.createElement("script")
    script.type = "text/javascript";
    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
    	        script.parentNode.removeChild( script );
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
         this.parentNode.removeChild(this);
         callback();
        };
    }
   	script.src = url;
   	document.getElementsByTagName("head")[0].appendChild(script);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question