chrome扩展删除脚本标记

我到处试图找到这个问题的答案。 我希望我的扩展能够禁用页面上的所有javascript但允许插入一个有效的脚本。 (所以chrome.contentSettings.javascript现在不是一个有效的选项)或者我想要一种方法来删除它们中的任何一个之前的所有脚本标签(这有点相同)

我尝试将内容脚本插入runat:document_start但当时dom并不完全存在。 itried在加载状态时在tabs.onUpdate上添加了一个事件,但是为时已晚,以及document_end上的内容脚本(所有这些脚本都试图删除脚本标记),但现在还为时已晚。 在绝望的行为中,我试图改变element.innerHTML的getter和setter的行为。 删除标签但不起作用

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

有任何想法吗?

在看到您的评论后,我认为这可能满足您的需求。 它的工作原理是获取页面源,将其渲染为dom,禁用所有j,然后将其放回页面。 不完全是你想要的,但应该很好地适应你的情况……

mainfest.json

{ "name": "Reload and Kill JS - Using a content script", "version": "1.0", "permissions": [ "tabs", "" , "storage" ], "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": [""], "js": ["injectedCode.js"], "run_at" : "document_start" } ], "minimum_chrome_version" : "20", "manifest_version" : 2 } 

background.js

 chrome.storage.local.set({"blockhttp://paez.kodingen.com/":true}); 

injectedCode.js

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

嗯,真正防止脚本的唯一方法是使用contentSettings。 因此,您需要将代码放在其他域中的其他位置,因为contentSettings规则可以应用于特定的URL。

将内容脚本放在文档启动时运行。

contentScript.js:

 window.stop(); document.all[0].innerHTML = "\ \ \ \ \ "; 

inject.html:

         

现在您的代码位于父框架和另一个域中,但它可能会导致一些CORS问题,您可以尝试稍后找到一些解决方法。 试一试,然后告诉我是否有什么需要解决的。