Google Chrome extension - how to turn JavaScript on/off?

后端 未结 5 2095
自闭症患者
自闭症患者 2020-12-12 22:26

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

For example, in Opera browser, there are simple possibilities t

相关标签:
5条回答
  • 2020-12-12 22:46

    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();
    
    0 讨论(0)
  • 2020-12-12 22:47

    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.

    0 讨论(0)
  • 2020-12-12 22:49

    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

    0 讨论(0)
  • 2020-12-12 22:58

    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",
            "*://*/*"
      ]
    }
    
    0 讨论(0)
  • 2020-12-12 23:08

    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

    0 讨论(0)
提交回复
热议问题