简体   繁体   中英

Disabling / enabling javascript only in one tab from the Firefox/Chrome extension (using Web Extensions)

I'm making an extension (add on) for Firefox/Chrome/Opera/etc using Web Extensions system.

I want to enable or disable javascript, but per tab , not globally. Ie in first tab it would be disabled, but in a second one - enabled.

I'm trying to find a solution here, but I can't see it:

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Browser_support_for_JavaScript_APIs

Or maybe, there is a way to do that with just pure Javascript? BTW - that is why I also tagged this with Javascript.

You can do this using the chrome.contentsettings API. You will need to declare "contentsettings" permission in your manifest.json:

"permissions": [   
   "contentSettings"
 ]

Then, to allow/disallow Javascript:

this.setJavascript=function(setting){

    if(!(['allow','block'].includes(setting)) ){
        console.log("invalid setting passed to setjavascript");
        setting='allow';
    }

    //  *://www.example.com/*
    var pattern= "*://"+this.domain+"/*";
    chrome.contentSettings.javascript.set({
            'primaryPattern': pattern,
            'setting': setting,
        }, function () {
           // console.log("Javascript blocked");
        }
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM