Google Chrome 扩展程序 - 如何打开/关闭 JavaScript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4663359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:31:37  来源:igfitidea点击:

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

javascriptgoogle-chromegoogle-chrome-extension

提问by Sk8erPeter

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

是否可以使用自制的 Google Chrome 扩展程序打开/关闭 JavaScript

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

例如,在 Opera 浏览器中,可以使用小部件、用户制作的按钮等简单地做到这一点,但我没有在 Chrome 中找到任何解决方案(我的第一个来源是官方的 Google Chrome 扩展的文档)。

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?

一个奇怪的方面是我需要 JavaScript 来运行扩展程序本身……如果我能够通过扩展程序关闭 JavaScript,那么在执行此操作后我还可以在我的扩展程序中使用 JavaScript 吗?



Edit:

编辑:

It's already possible to do it via chrome.contentSettings.javascript!
See this examplewhich shows how to use it (Quick JavaScript Switcherextension, which mlblinked here).

已经可以通过chrome.contentSettings.javascript做到这一点
请参阅此示例,其中显示了如何使用它(快速 JavaScript 切换器扩展,此处链接了mlb)。

回答by mlb

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

现在可以使用 ContentSettings API,
并且有一个名为Quick Javascript Switcher的扩展,可以即时打开/关闭 javascript:https: //github.com/maximelebreton/quick-javascript-switcher

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

Chrome 网上商店上的QJShttps: //chrome.google.com/webstore/detail/geddoclleiomckbhadiaipdggiiccfje

Enjoy !

享受 !

seo : disable javascript chrome extension

seo :禁用 javascript chrome 扩展

回答by mlemos

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

目前似乎扩展程序无法禁用 JavaScript 支持。Chromium 跟踪站点中甚至有一个功能请求。我们需要耐心等待,直到谷歌决定支持它。

回答by Wen Qi

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

目前,我们无法使用您的 Chrome 扩展程序访问chrome://settings/content数据

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

在我的代码,当标签“铬://设置/内容”创建警报(0)确实工作,总是得到如下错误:

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

tabs.executeScript 期间出错:无法访问 url“chrome://settings/content”的内容。扩展清单必须请求访问此主机的权限。

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

但是当创建标签“http://www.google.com.hk”时,alert(0) 起作用。

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

所以我认为我们无法访问 chrome://settings/* 数据:

popup.html:

弹出窗口.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:

清单.json:

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

回答by Myster

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

现在可以使用 chrome 的发布版本(从 chrome 16 开始)从扩展中关闭和打开 java 脚本。

Here's an extension which does just that:

这是一个扩展程序,它可以做到这一点:

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

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

回答by Nadav B

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

这是我阻止 chrome 扩展程序运行 javascript 的唯一方法。粘贴此代码:

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();