0

I made an extension for Chrome to block the ads. But, there is a problem. Whenever I change the value of the address bar from www.flipkat.com to www.snapdeal.com, my adblock shows the combined count of ads blocked on both the urls. For the same tabId, if I change my url in address bar it shows the combined count of all the urls typed in the same tab.

Is there a function which can nullify the count every time I change the url in the address bar?

Here is my code ........

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")

})
suresharora
  • 149
  • 1
  • 8

0 Answers0