Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在chrome扩展中阻止内联javascript?_Javascript_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

如何在chrome扩展中阻止内联javascript?

如何在chrome扩展中阻止内联javascript?,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,需要的是用另一个替换网站语法高亮。通过在manifest.json文件中添加以下内容,我成功地创建了与块相关的JS和CSS文件: "background": { "scripts": ["background.js"] }, "permissions": [ "webRequest", "webRequestBlocking", "http://*.oschina.net/", "https://*.oschina.net/" ], chrome.webRe

需要的是用另一个替换网站语法高亮。通过在
manifest.json
文件中添加以下内容,我成功地创建了与块相关的JS和CSS文件:

"background": {
   "scripts": ["background.js"]
},
"permissions": [   
    "webRequest",
    "webRequestBlocking",
    "http://*.oschina.net/", "https://*.oschina.net/"
],
chrome.webRequest.onBeforeRequest.addListener(
function(){ 
    return {cancel: true}; 
},
{
    urls: [
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/scripts/brush.js",
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/styles/shCore.css",
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/styles/shThemeDefault.css"
    ],
    types: ["script","stylesheet"]
},
["blocking"]
background.js
文件中:

"background": {
   "scripts": ["background.js"]
},
"permissions": [   
    "webRequest",
    "webRequestBlocking",
    "http://*.oschina.net/", "https://*.oschina.net/"
],
chrome.webRequest.onBeforeRequest.addListener(
function(){ 
    return {cancel: true}; 
},
{
    urls: [
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/scripts/brush.js",
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/styles/shCore.css",
        "http://my.oschina.net/js/syntax-highlighter-2.1.382/styles/shThemeDefault.css"
    ],
    types: ["script","stylesheet"]
},
["blocking"]
))

因此,前语法highlighter需要的js和css被阻止,但在内联html中有一个名为initialize的函数:

$(document).ready(function(){
    SyntaxHighlighter.config.clipboardSwf = '/js/syntax-highlighter-2.1.382/scripts/clipboard.swf';
    SyntaxHighlighter.all();
});

所以我在控制台中收到了错误消息,我如何在chrome扩展脚本中阻止它呢

Chrome不提供任何允许您阻止特定内联脚本的方法。()

您可以使用API禁用所有内联脚本,也可以使用API每页添加不包含
不安全内联
内容安全策略

很可能您不想阻止所有脚本,而只是阻止代码段运行。没有适合所有人的方法,但是在您的情况下,您可以将一个被阻止的脚本重定向到定义这些方法的存根,例如

 chrome.webRequest.onBeforeRequest.addListener(
    function() {
        return {redirectUrl: chrome.runtime.getURL("brush.js") };
    },
    {
        urls: [
            "http://my.oschina.net/js/syntax-highlighter-2.1.382/scripts/brush.js"
        ],
        types: ["script"]
    },
    ["blocking"]
brush.js

if (!window.SyntaxHighlighter) window.SyntaxHighlighter = {};
if (!SyntaxHighlighter.config) SyntaxHighlighter.config = {};
SyntaxHighlighter.all = function() {};

不要忘记将脚本添加到清单文件中。修改清单文件后,别忘了重新加载Chrome扩展名。

让我尝试一下并给出答复。因为JS是内联的,所以您可能会在
document\u end
中插入一个运行的,并让内容脚本从DOM中删除内联脚本。