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

时间:2015-11-14 17:53:48

标签: firefox-addon addeventlistener

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

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

但是它阻止所有点击,我想防止当前的doucment,但是这个案例的东西窗口和文件是整个浏览器。 我有一些像这样的代码来获取当前文档,但我希望它能够完成加载:

onPageLoad: function(aEvent) {

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

1 个答案:

答案 0 :(得分:1)

这取决于您运行脚本所需的时间。

如果您需要在发出请求之前运行代码,则可以使用HTTP Observers

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

您还可以向相关节点添加“点击”事件监听器,并阻止他们使用Event.preventDefault()&amp; 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()&amp; Event.stopPropagation()

相关问题