41

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).

Community
  • 1
  • 1
Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
  • do you mean your extension should toggle js of the browser? or just disable it for the extension? without javascript your extension would do nothing... – DarkLeafyGreen Jan 20 '11 at 13:24
  • The first version, so I would like to toggle JS of the browser - this way I could test my sites how these work without JS. It's too circumstantial when building sites to go to Options and turn JS off and back on all the time - it would be much simplier if I could just turn it on and off with just a single click (or a hotkey combination). I know, this is contradictious, because extensions need JavaScript... :)) But I just wonder if there are any solutions. Thanks! – Sk8erPeter Jan 20 '11 at 18:55

5 Answers5

55

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

mlb
  • 661
  • 7
  • 11
  • Wow, thank you *mlb*, this post was REALLY useful! I tried this extension, and it seems to work perfectly after enabling the experimental API usage! I changed the accepted answer to yours, because this seems to be a real solution! :) – Sk8erPeter Jan 25 '12 at 12:38
  • By the way, do you know anything about when the opportunity of toggling some settings via an extension (like the one you linked) will be "final" (and not just experimental)? Thanks once again! :) I have to add one more comment: this experimental API seems to be really good, as with this, one can also add a new item in the right click context menu of Chrome with an extension! It would be really good if it could be done without enabling the experimental API too. :) – Sk8erPeter Jan 25 '12 at 12:55
  • Hey Sk8erPeter, since Chrome 16, the ContentSettings API is no longer experimental ;) And for your suggestion, if i understand, you want the same QJS button but in the right clic menu ? – mlb Feb 06 '12 at 16:07
  • Hi mlb! No, not yet, I just realized how useful it is that developers can already extend the right click context menu via an extension. :) Thanks! This could be useful for extension developers: http://code.google.com/chrome/extensions/trunk/contentSettings.html! – Sk8erPeter Feb 06 '12 at 17:24
  • looks really nice, but is there any reason why it needs permission to read my browsing history? – DanielG Dec 28 '20 at 22:04
4

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.

mlemos
  • 1,235
  • 13
  • 21
  • thanks for your answer, mlemos! I edited my original post, please read the modifications too, thanx! :) I hope Google will let us control the options similar to Opera (or any other comfortable solutions would be good) in the near future. – Sk8erPeter Feb 17 '11 at 21:56
1

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",
        "*://*/*"
  ]
}
Wen Qi
  • 655
  • 7
  • 9
1

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

Myster
  • 17,704
  • 13
  • 64
  • 93
  • 1
    thanks, but this is EXACTLY the same extension which **mlb** mentioned here earlier on the 6th of October, 2011. :) Look: http://stackoverflow.com/a/7673539/517705. But it was useful to know that it already seems possible to toggle JS on and off *without* the usage of the *experimental API* too (if I'm correct), so thanks for that too! For this you deserve a +1. :) – Sk8erPeter Feb 03 '12 at 00:01
0

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();
Nathan B
  • 1,625
  • 1
  • 17
  • 15