yarek
yarek

Reputation: 12054

Include different JS script depending if run inside chrome extension or web

I am developping a chrome extension AND web js application. I have an HTML container.

I need this container.html having

<script src="extension.js"> 

when running in chrome extension and

<script src="web.js"> 

when running in web version

(But Cannot have both at the same time)

Any idea how to include either 1 script either other depending if running in extension or not?

Upvotes: 0

Views: 328

Answers (1)

Manuel Bitto
Manuel Bitto

Reputation: 5253

You should have some property or global object that is available only in chrome extension environment, for example: chrome.cookies. Then you could check the existence of that object or property and load dinamically the javascript file, for example:

function loadScript(url, callback){

    var script = document.createElement("script")
    script.type = "text/javascript";

    script.onload = function(){
        callback();
    }
    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

if(chrome && chrome.cookies){
    loadScript('extension.js', function(){ //loaded })
}
else{
    loadScript('web.js', function(){ //loaded })
}

Upvotes: 1

Related Questions