chrome扩展删除脚本标记

Ste*_*fan 4 javascript jquery dom google-chrome-extension

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

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

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

有任何想法吗?

PAE*_*AEz 6

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

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
}
Run Code Online (Sandbox Code Playgroud)

background.js

chrome.storage.local.set({"blockhttp://paez.kodingen.com/":true});
Run Code Online (Sandbox Code Playgroud)

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();  
    }
});
Run Code Online (Sandbox Code Playgroud)