Google Chrome extension - how to turn JavaScript o

2019-01-21 08:11发布

问题:

Is it possible to turn JavaScript on/off with a self-made Google Chrome extension?

For example, in Opera browser, there are simple possibilities to do that with widgets, user-made buttons, etc., but I didn't find any solutions to do that in Chrome (my first source was the official Google Chrome extensions' documentations).

A strange aspect is that I need JavaScript to run the extension itself...if I'm able to turn JavaScript off with an extension, can I still use JavaScript in my extension after doing it?


Edit:

It's already possible to do it via chrome.contentSettings.javascript!
See this example which shows how to use it (Quick JavaScript Switcher extension, which mlb linked here).

回答1:

It's now possible with the ContentSettings API,
and there is an extension named Quick Javascript Switcher that turns on/off javascript on the fly : https://github.com/maximelebreton/quick-javascript-switcher

QJS on the Chrome Webstore : https://chrome.google.com/webstore/detail/geddoclleiomckbhadiaipdggiiccfje

Enjoy !

seo : disable javascript chrome extension



回答2:

It seems currently it is not possible for extensions to disable JavaScript support. There is even a feature request for that in the Chromium tracking site. We need to be patient and wait until Google decides to support that.



回答3:

Currently, we can NOT access chrome://settings/content data with your Chrome extension

In my code, when tab "chrome://settings/content" created, the alert(0) does NOT work, and always get the follow error:

Error during tabs.executeScript: Cannot access contents of url "chrome://settings/content". Extension manifest must request permission to access this host.

but when tab "http://www.google.com.hk" created, alert(0) works.

So I think we can NOT access chrome://settings/* data :

popup.html:

<html>
<head>
<script>
  function openSetting() {
    chrome.tabs.create({"url":"chrome://settings/content", "selected":true});
  }

  function openGoogle() {
    chrome.tabs.create({"url":"http://www.google.com.hk", "selected":true});
  }

  //chrome.browserAction.onClicked.addListener(enableDisableImage);

    chrome.tabs.onCreated.addListener(function(tab) {
        chrome.tabs.executeScript(null, {code:"alert(0)"});
    });
</script>
</head>
<body>
<input type="button" onClick="openSetting()" value="Switch"/>
<input type="button" onClick="openGoogle()" value="Switch"/>
</body>
</html>

manifest.json:

{
  "name": "ImageSwitcher",
  "version": "1.0",
  "description": "Disable/Enable loading image",
  "browser_action": {
    "default_icon": "icon.png",
        "default_popup": "popup.html"
  },
  "permissions": [
    "tabs",
        "*://*/*"
  ]
}


回答4:

It's now possible with the release version of chrome (as of chrome 16) to toggle java-script off and on from an extension.

Here's an extension which does just that:

https://chrome.google.com/webstore/detail/geddoclleiomckbhadiaipdggiiccfje



回答5:

That's the only way that worked for me to stop chrome extension from running javascript. Paste this code:

function exit() {
    'use strict';
    window.addEventListener('error', function (e) {e.preventDefault();e.stopPropagation();}, false);

    let handlers = [
        'copy', 'cut', 'paste',
        'beforeunload', 'blur', 'change', 'click', 'contextmenu', 'dblclick', 'focus', 'keydown', 'keypress', 'keyup', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'resize', 'scroll', 'selectstart',
        'DOMNodeInserted', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument', 'DOMNodeInsertedIntoDocument', 'DOMAttrModified', 'DOMCharacterDataModified', 'DOMElementNameChanged', 'DOMAttributeNameChanged', 'DOMActivate', 'DOMFocusIn', 'DOMFocusOut', 'online', 'offline', 'input',
        'abort', 'close', 'drop', 'dragstart', 'drag', 'load', 'paint', 'reset', 'select', 'submit', 'unload'
    ];

    function eventHandler(e) {
        e.stopPropagation();
        // e.preventDefault(); // Stop for the form controls, etc., too?
    }
    for(let i=0; i < handlers.length; i++) {
        window.addEventListener(handlers[i], eventHandler, true);
    }

    if(window.stop) {
        window.stop();
    }

    Array.prototype.forEach.call(document.querySelectorAll("*"), el => {
        if( document.defaultView.getComputedStyle(el)["-webkit-user-select"] == "none" ) {
            //el.style.webkitUserSelect = "auto";
            el.style.setProperty("-webkit-user-select", "auto", "important");
        }
    });

    throw '';
}
exit();