想要在地址栏值发生变化时以chrome方式触发事件

时间:2014-02-12 06:30:28

标签: javascript google-chrome google-chrome-extension tabs

我对Chrome进行了扩展,以阻止广告。但有个问题。每当我将地址栏的值从www.flipkat.com更改为www.snapdeal.com时,我的adblock都会显示两个网址上阻止的广告组合数量。对于同一个tabId,如果我在地址栏中更改了我的网址,则会显示在同一个标​​签中输入的所有网址的合计计数。

是否有一个函数可以在每次更改地址栏中的URL时使计数无效?

这是我的代码........

var FilterNotifier = require("filterNotifier").FilterNotifier;
var newURL = [];
var temp1 = [];
var onFilterChangeTimeout = null;
function onFilterChange()
{
  onFilterChangeTimeout = null;
  ext.webRequest.handlerBehaviorChanged();
}

var importantNotifications = {
  'filter.added': true,
  'filter.removed': true,
  'filter.disabled': true,
  'subscription.added': true,
  'subscription.removed': true,
  'subscription.disabled': true,
  'subscription.updated': true,
  'load': true
};

FilterNotifier.addListener(function(action)
{
  if (action in importantNotifications)
  {
    // Execute delayed to prevent multiple executions in a quick succession
    if (onFilterChangeTimeout != null)
      window.clearTimeout(onFilterChangeTimeout);
    onFilterChangeTimeout = window.setTimeout(onFilterChange, 2000);
  }
});

function onBeforeRequest(url, type, tab, frame)
{
  if (isFrameWhitelisted(tab, frame))
    return true;

  var docDomain = extractHostFromURL(frame.url);
  var filter = defaultMatcher.matchesAny(
    url,
    type == "sub_frame" ? "SUBDOCUMENT" : type.toUpperCase(),
    docDomain,
    isThirdParty(extractHostFromURL(url), docDomain)
  );

  //FilterNotifier.triggerListeners("filter.hitCount", filter, 0, 0, tab);
  //return !(filter instanceof BlockingFilter);

  if(filter instanceof BlockingFilter){
    try{
    if(newURL[tab._id]==undefined){
        var urls = []
        urls.push(extractHostFromURL(url))
        newURL[tab._id]=urls
    }else if(newURL[tab._id]!=undefined &&newURL[tab._id].length >0){
        //alert("suresh")
        temp1 = newURL[tab._id];
        temp1.push(extractHostFromURL(url))
        newURL[tab._id]=temp1
    }
    }catch(e){}
    //alert(newURL)
    return false;
  }else if(!filter instanceof BlockingFilter){
    return true
  }
}

/*function  UniqueArray (newURL,tabId) 
{
    var newArr = [],
        found,
        x, y;
     var flag=0;
    for( z=0;z<newURL.length;z++)
    {     
           var temp1=[];

     for ( x = 0; x < newURL[tab._id].length; x++ ) 
      {
        for(y=0;y<temp.length;y++)
        {
           if(newURL[tab._id][x]!= temp[y])
           {
             temp1.push(newURL[tab._id][x]);
           }
        }
      }
    Unique[tab._id]=temp1;
    }


}*/


//alert(newURL);


ext.webRequest.onBeforeRequest.addListener(onBeforeRequest);

if (require("info").platform == "chromium")
{
  function onHeadersReceived(details)
  {
    if (details.tabId == -1)
      return;

    if (details.type != "main_frame" && details.type != "sub_frame")
      return;

    var tab = new Tab({id: details.tabId});
    var frame = new Frame({id: details.frameId, tab: tab});

    if (frame.url != details.url)
      return;

    for (var i = 0; i < details.responseHeaders.length; i++)
    {
      var header = details.responseHeaders[i];
      if (header.name.toLowerCase() == "x-adblock-key" && header.value)
        processKeyException(header.value, tab, frame);
    }
  }

  chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["<all_urls>"]}, ["responseHeaders"]);
}


chrome.webNavigation.onDOMContentLoaded.addListener(function (epics) {
      if(epics.frameId==0){
        //Frame loading has been happend we need to start our code for showing popups
        var dc = [];
        //alert(newURL+epics.tabId)
        var temp = [];
        var uni =[];
        temp = newURL[epics.tabId]
        //alert("abc" +temp);
        for(i=0;i<temp.length;i++)
        {
           //alert("abc" +temp);
           var flag=0;
           if(uni.length>=0)
           {
             for(j=0;j<uni.length;j++)
             {
                if(temp[i]==uni[j])
                flag=1;
             }
           }
           if(!flag)
            uni.push(temp[i]);

        }
        chrome.tabs.sendRequest(epics.tabId, {
                      msg: "show",
                      force: true,
                      all: true,
                      others:dc,  // not 
                      bugs: uni, // blocked companies 
                      title_page: "pagetitle",
                      alert_cfg: {
                          pos_x: 'right',
                          pos_y: 'bottom',
                          timeout: 5000000
                      }
         });
      }
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
alert("chrome.tabs.onUpdated.addListener")

})

0 个答案:

没有答案