扩展在页面加载前获取内容

extension get content before page load

我想开发一个扩展来防止加载页面中的某些事件 我写了这段代码,但它在页面完全加载后加载。 我想阻止 body 和 window 单击、鼠标向上、鼠标向下事件,因为有些网站使用它们监听器来 运行 弹出窗口 windows.I 想为它们注册一个监听器并防止 运行宁弹出代码。 这是我的尝试,但需要在文档完成之前注册监听器

window.addEventListener("load", function() { bgnmyExtension.init(); }, false);
var bgnmyExtension = {
    init: function() {
    var bgnappcontent = document.getElementById("appcontent");   // browser
    if(bgnappcontent)
      bgnappcontent.addEventListener("load", bgnmyExtension.onPageLoad, true);
    var bgnmessagepane = document.getElementById("messagepane"); // mail
    if(bgnmessagepane)
      bgnmessagepane.addEventListener("load", function() { bgnmyExtension.onPageLoad(); }, true);
  },

  onPageLoad: function(aEvent) {

      var bgmyfn=setInterval(function(){MyPopupBlocker()},1);
var bgndoc = aEvent.originalTarget; // doc is document that triggered "onload" event



function MyPopupBlocker(){
          window.onclick=null;
          bgndoc.onclick=null;
          window.onmousedown=null;
          bgndoc.onmousedown=null;
          window.onmouseup=null;
          bgndoc.onmouseup=null
}
 if (!bgndoc.addEventListener) 
    bgndoc.attachEvent('onclick',MyPopupBlocker,true);
else
    bgndoc.addEventListener('click',MyPopupBlocker,true);

function bgPreloadExit(){if(bgbgPreload)bgPreload(true)};
function btmainfn(){
    var checkbticon=bgndoc.getElementById("bgnmyID");
    if(checkbticon==null && window==window.top){
        bgndoc.insertAdjacentHTML("afterbegin","<div align='left' style='position:fixed;left:0;top:0;z-index:10000' id='bgnmyID'><a href='http://*****'><img src='http://*****/images/My.jpg'></a></div>");
        setTimeout(function(){var e=document.getElementById("bgnmyID");e.parentNode.removeChild(e)},3000);
        } else {
            clearInterval(checkbtmain)}
}
var checkbtmain=setInterval(function(){btmainfn()},500);

} 
}

编辑:

我现在使用此代码,但它不会将我的脚本添加到我当前的文档中:

init();
 var intval;
 var intvalhead;
function init(){
    intval = setInterval ( "checkForElement()", 200 );
}
function checkForHead(){
    if (typeof document.getElementsByTagName('head')[0] != 'undefined'){
        clearInterval(intvalhead);
        var script = document.createElement('script');
        script.text = "function stopFunction(e){e.preventDefault();e.stopPropagation();}document.addEventListener( 'click', stopFunction, false );document.addEventListener( 'mouseup', stopFunction, false );      document.addEventListener( 'mousedown', stopFunction, false );      window.addEventListener( 'click', stopFunction, false );        window.addEventListener( 'mouseup', stopFunction, false );      window.addEventListener( 'mousedown', stopFunction, false );";
        script.setAttribute('type', 'text/javascript');
        document.getElementsByTagName('head')[0].appendChild(script);
    }

}
function checkForElement(){
    console.log("bbb");
    if (document.getElementById('appcontent') != 'undefined'){
        clearInterval(intval);
        var appcontent = window.document.getElementById("appcontent");
        intvalhead = setInterval ( "checkForHead()", 200 );
    }
}

在此之前我使用了这段代码:

function init(){
    intval = setInterval ( "checkForElement()", 200 );
}
function disable(e){
    e.preventDefault();
    e.stopPropagation();
}

function checkForElement(){
    console.log("bbb");
    if (document.getElementById('appcontent') != 'undefined'){
        clearInterval(intval);
        var appcontent = window.document.getElementById("appcontent");
        window.document.addEventListener("click", disable, false);
        window.document.addEventListener("mouseup", disable, false);
        window.document.addEventListener("mousedown", disable, false);
        document.addEventListener("click", disable, false);
        document.addEventListener("mouseup", disable, false);
        document.addEventListener("mousedown", disable, false);
    }
}

但它会阻止所有点击,我想阻止当前文档,但我想 window 并且在这种情况下文档是整个浏览器。 我有一些这样的代码来获取当前文档,但我希望它在加载完成之前:

onPageLoad: function(aEvent) {

var bgndoc = aEvent.originalTarget; // doc is document that triggered "onload" event

这取决于您需要多早运行脚本。

如果您需要运行 发出请求前的代码,那么您可以使用HTTP Observers

如果您需要运行页面脚本之前的代码运行,那么您可以使用beforescriptexecute

您还可以向有问题的节点添加 'click' 事件侦听器,并使用 Event.preventDefault() & Event.stopPropagation()

阻止它们的正常操作

更新:
除了下面的评论之外,这里还有一个如何完成的示例(使用 GreaseMonkey 脚本测试)。它用于删除多个脚本。

// listening for the script execution
document.addEventListener('beforescriptexecute', removeJS, false);

function removeJS(e) {

  var uri = e.target.src;
  if(!uri) { return; } // end execution if not external script

  switch (true) {

    case uri.indexOf('shoptou.js') !== -1:
    // case uri.indexOf('abcd.com') !== -1: // in case you need to remove other scripts
      e.preventDefault();
      e.stopPropagation();
      console.log('removed ' + uri); // removed http://www.parsnaz.ir/wp-content/plugins/wp-minify/min/?f=shoptou.js&m=1447510600
      break;
  }
}

如果这是您需要删除的唯一脚本,您可以简化上述功能:

function removeJS(e) {

  var uri = e.target.src;
  if(uri && uri.indexOf('shoptou.js') !== -1 ) { 

    e.preventDefault();
    e.stopPropagation();
    console.log('removed ' + uri); // removed http://www.parsnaz.ir/wp-content/plugins/wp-minify/min/?f=shoptou.js&m=1447510600 
  }
}

如果有 on* 个属性,您可以使用 removeAttribute() 简单地删除它们。
如果是注册听众,您可以使用 Event.preventDefault() & Event.stopPropagation()