Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
Javascript chrome扩展删除脚本标记_Javascript_Jquery_Dom_Google Chrome Extension - Fatal编程技术网

Javascript chrome扩展删除脚本标记

Javascript chrome扩展删除脚本标记,javascript,jquery,dom,google-chrome-extension,Javascript,Jquery,Dom,Google Chrome Extension,我到处找,试图找到这个问题的答案。 我希望我的扩展可以禁用页面上的所有javascript,但允许插入一个可以工作的Coent脚本。(因此chrome.contentSettings.javascript目前不是有效选项) 或者,我想要一种在任何脚本标记触发之前删除所有脚本标记的方法(这有点类似) 我尝试将内容脚本插入到runat:document\u start中,但dom当时并不完全存在。我尝试在加载状态时在tabs.onUpdate上添加内容脚本,但为时已晚;我尝试在文档末尾添加内容脚本(

我到处找,试图找到这个问题的答案。 我希望我的扩展可以禁用页面上的所有javascript,但允许插入一个可以工作的Coent脚本。(因此chrome.contentSettings.javascript目前不是有效选项) 或者,我想要一种在任何脚本标记触发之前删除所有脚本标记的方法(这有点类似)

我尝试将内容脚本插入到runat:document\u start中,但dom当时并不完全存在。我尝试在加载状态时在tabs.onUpdate上添加内容脚本,但为时已晚;我尝试在文档末尾添加内容脚本(所有这些脚本都试图删除脚本标记),但为时已晚。 在绝望中,我尝试将element.innerHTML的getter和setter的行为更改为。移除标签,但这不起作用

我试图避免向location.href发送xhr请求,并解析和重新设置内容,因为这太密集了


有什么想法吗

那么,真正阻止脚本的唯一方法就是使用contentSettings。因此,您需要将代码放在其他地方的另一个域中,因为contentSettings规则可以应用于特定的URL

将内容脚本放在文档开始时运行

contentScript.js:

window.stop();
document.all[0].innerHTML = "\
<html>\
    <body>\
        <iframe src=\"chrome-extension://ID/inject.html?url="+encodeURIComponent(location.href)+"\"></iframe>\
    </body>\
</html>";
<html>
<head>
<script>
    var frame = document.querySelector('iframe');
    frame.src = location.search.replace('?url=', '');
    frame.onload = function() {
        //Your stuff here
    }
</script>
</head>
<body>
    <iframe></iframe>
</body>
<html>
window.stop();
文档。所有[0]。innerHTML=”\
\
\
\
\
";
inject.html:

window.stop();
document.all[0].innerHTML = "\
<html>\
    <body>\
        <iframe src=\"chrome-extension://ID/inject.html?url="+encodeURIComponent(location.href)+"\"></iframe>\
    </body>\
</html>";
<html>
<head>
<script>
    var frame = document.querySelector('iframe');
    frame.src = location.search.replace('?url=', '');
    frame.onload = function() {
        //Your stuff here
    }
</script>
</head>
<body>
    <iframe></iframe>
</body>
<html>

var frame=document.querySelector('iframe');
frame.src=location.search.replace(“?url=”,”);
frame.onload=函数(){
//你的东西在这里
}
现在,您的代码位于父框架和另一个域中,但它可能会导致一些CORS问题,您可以稍后尝试找到一些解决方法。
试一试,然后告诉我是否有问题需要解决。

看到您的评论后,我认为这可能符合您的需要。它的工作原理是获取页面源代码,将其呈现到dom中,禁用所有js,然后将其放回页面中。不完全是你想要的,但应该适合你的情况

mainfest.json

{
  "name": "Reload and Kill JS - Using a content script",
  "version": "1.0",
  "permissions": [
    "tabs", "<all_urls>" , "storage"
  ],
  "background": {
    "scripts": ["background.js"]
  },
   "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["injectedCode.js"],
      "run_at" : "document_start"
    }
  ],
  "minimum_chrome_version" : "20",
  "manifest_version" : 2
}
reloadAndKillJS = function() {
    document.documentElement.innerHTML = 'Reloading Page...';

    var xhr = new XMLHttpRequest();

    xhr.open('GET', window.location.href, true);

    xhr.onerror = function() {
        document.documentElement.innerHTML = 'Error getting Page';
    }

    xhr.onload = function() {
        var page = document.implementation.createHTMLDocument("");
        page.documentElement.innerHTML = this.responseText;

        var newPage = document.importNode(page.documentElement, true);

        var nodeList = newPage.querySelectorAll('script');
        for (var i = 0; i < nodeList.length; ++i) {
            var node = nodeList[i];
            if (node.src) {
                node.setAttribute('original-src', node.src);
                node.removeAttribute('src');
            }
            node.innerText = '';
        }

        document.replaceChild(newPage, document.documentElement);
        delete page;

        // Do your thing here
    }

    xhr.send();
}

chrome.storage.local.get("block"+window.location.href,function(items){
    if (items["block"+window.location.href]){
    window.stop();
    reloadAndKillJS();  
    }
});
injectedCode.js

{
  "name": "Reload and Kill JS - Using a content script",
  "version": "1.0",
  "permissions": [
    "tabs", "<all_urls>" , "storage"
  ],
  "background": {
    "scripts": ["background.js"]
  },
   "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["injectedCode.js"],
      "run_at" : "document_start"
    }
  ],
  "minimum_chrome_version" : "20",
  "manifest_version" : 2
}
reloadAndKillJS = function() {
    document.documentElement.innerHTML = 'Reloading Page...';

    var xhr = new XMLHttpRequest();

    xhr.open('GET', window.location.href, true);

    xhr.onerror = function() {
        document.documentElement.innerHTML = 'Error getting Page';
    }

    xhr.onload = function() {
        var page = document.implementation.createHTMLDocument("");
        page.documentElement.innerHTML = this.responseText;

        var newPage = document.importNode(page.documentElement, true);

        var nodeList = newPage.querySelectorAll('script');
        for (var i = 0; i < nodeList.length; ++i) {
            var node = nodeList[i];
            if (node.src) {
                node.setAttribute('original-src', node.src);
                node.removeAttribute('src');
            }
            node.innerText = '';
        }

        document.replaceChild(newPage, document.documentElement);
        delete page;

        // Do your thing here
    }

    xhr.send();
}

chrome.storage.local.get("block"+window.location.href,function(items){
    if (items["block"+window.location.href]){
    window.stop();
    reloadAndKillJS();  
    }
});
reloadAndKillJS=function(){
document.documentElement.innerHTML='重新加载页面…';
var xhr=new XMLHttpRequest();
xhr.open('GET',window.location.href,true);
xhr.onerror=函数(){
document.documentElement.innerHTML='获取页面时出错';
}
xhr.onload=函数(){
var page=document.implementation.createHTMLDocument(“”);
page.documentElement.innerHTML=this.responseText;
var newPage=document.importNode(page.documentElement,true);
var nodeList=newPage.querySelectorAll('script');
对于(变量i=0;i
.javascript,它还禁用了我希望继续运行的内容脚本…您的解决方案听起来很有趣,但它对我不起作用,因为我特别需要访问该页面中的事件。例如,当我的应用程序被激活时单击任何页面时。。。做一些事情,但当应用程序激活时,禁用访问的所有jspages@tak3r,您能添加更多详细信息吗?因此,我正在做一个扩展,让用户能够在不同的项目上单击网页,选择一系列网页,并应用模式自动刮除它们,我需要HTML的初始结构不被JS=>改变,只为用户提供非JS功能。同时,我想为鼠标单击添加事件侦听器,以便捕获它们并获得元素的路径(父链)。如果js被运行时javascript更改,那么用户单击的路径对于只看到初始HTMLS的脚本无效。因此,我在网页中插入内容脚本以绑定事件并提供内容反馈的视觉效果,但我需要一种方法来防止所有本机脚本标记在实际页面上运行。谢谢你的回答。虽然这有效。。。我在最初的帖子中写道,这正是我试图避免的。。引用我自己的话:)“1.最后一种方法允许用户使用HTML5 getUserMedia API[15]现场拍照,并访问用户的网络摄像头(对于笔记本电脑/台式机)、摄像头(对于移动设备)或系统可用的任何其他视频音频输入设备”呵呵,奇数引用;)。。所以你这么做了,对不起……但它无论如何都不起作用,因为在setInterval上运行的任何东西仍在运行。我有另一个想法,使用背景页面和webRequest.onHeaders插入一个可能有效的内容安全策略,但我似乎无法修改标题?。也不知道为什么。另一种方法可能是在内容脚本中添加上述内容的变体,停止页面加载,然后获取页面,但内容脚本如何知道何时重写?或者可能是一个带有CSP的框架…总之,祝你好运:PI知道它不能按你想要的方式工作,但更新了它,因此它阻止了JS…使用新的存储api作为Im来懒散地发送消息,这就是为什么最低的chrome版本是的,我知道我无法避免重新获取页面,至少在content.javascript问题得到解决之前,您可以选择内容脚本。我认为你的答案是好的,因为它确实提供了一个非常具体的解决方案,对于其他可能找到这篇文章并有类似问题的人来说:)