chrome extension remove script tags

前端 未结 2 1548
长情又很酷
长情又很酷 2021-02-10 10:29

i looked everywhere trying to find an answer to this question. i want my extension to either disable all javascript on the page BUT to allow the insertion of a cotent script th

2条回答
  •  执念已碎
    2021-02-10 11:32

    After seeing your comments I think this might suite your needs. It works by getting the pages source, render it to a dom, disable all the js and then put it back into the page. Not exactly what you wanted but should suite your case well...

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

提交回复
热议问题