chrome扩展名删除脚本标签 [英] chrome extension remove script tags

查看:191
本文介绍了chrome扩展名删除脚本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到无处不在,试图找到这个问题的答案。
我想要我的扩展名禁用页面上的所有javascript,但是允许插入一个可以工作的基础脚本。 (所以chrome.contentSettings.javascript现在不是一个有效的选项)
或者,我想要一种方法来删除所有脚本标记之前,任何一个都会发生(这是一样的)



我尝试将内容脚本插入到runat:document_start中,但dom并不完全在那里。在tabs.on添加一个参考文件。在状态加载时更新,但是太晚了,以及document_end(所有这些谁尝试删除脚本标签)的内容脚本,但它还为时已晚。
在绝望的行为中,我尝试改变element.innerHTML的getter和setter的行为。删除标签,但不起作用



我正在尝试避免向location.href发送xhr请求,并解析并重新设置内容太严格。



任何想法?

解决方案

看到您的意见后,我认为这可能会满足您的需求。它通过获取页​​面源代码,将其渲染为dom,禁用所有js,然后将其放回页面。不完全是你想要的,但应该适合你的情况...



mainfest.json

  {
name:重新加载和杀死JS - 使用内容脚本,
version:1.0,
permissions:[
tabs,< all_urls> ,存储
],
background:{
scripts:[background.js]
},
content_scripts:[
{
匹配:[< all_urls>],
js:[injectCode.js],
run_at:document_start
}
],
minimum_chrome_version:20,
manifest_version:2
}

background.js

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

injectCode.js

  reloadAndKillJS = function(){
document.documentElement.innerHTML ='重新加载页面...';

var xhr = new XMLHttpRequest();

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

xhr.onerror = function(){
document.documentElement.innerHTML =获取页面时出错;
}

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'); (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);
删除页面;

//你的东西在这里
}

xhr.send();
}

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


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 that will work. (so chrome.contentSettings.javascript is not a valid option for now) Alternatively i want a way to remove all script tags before any of them fire (which is kinda the same thing)

i tried inserting content scripts to runat:document_start but the dom is not fully there at the time. itried adding a conte t s ript on tabs.onUpdate when state is loading but that is too late and as well as content scripts at document_end (all of which who try to remove script tags) but it is still too late. in an act of desperation i tried altering the behavior of the getters and setters of element.innerHTML to. remove the tags but that did not work as well

i am trying to avoid sending an xhr request to location.href and parse and re_set the content as that is too intensive.

any ideas?

解决方案

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", "<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
}

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

这篇关于chrome扩展名删除脚本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆