From c7eebec310e00b915ef7bcff2f9d95e9a48d3731 Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 10 Oct 2014 13:26:43 -0400 Subject: [PATCH] this fixes #297 and #91 --- js/tab.js | 36 ++++++++++++++++++++++++++++++------ js/traffic.js | 4 ++-- js/xal.js | 10 ++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/js/tab.js b/js/tab.js index f0394e6c5..7c1993dbc 100644 --- a/js/tab.js +++ b/js/tab.js @@ -28,18 +28,18 @@ // When the DOM content of root frame is loaded, this means the tab // content has changed. - function onNavigationCommitted(details) { + var onNavigationCommitted = function(details) { if ( details.frameId !== 0 ) { return; } µb.bindTabToPageStats(details.tabId, details.url); - } + }; chrome.webNavigation.onCommitted.addListener(onNavigationCommitted); // It may happen the URL in the tab changes, while the page's document // stays the same (for instance, Google Maps). Without this listener, // the extension icon won't be properly refreshed. - function onTabUpdated(tabId, changeInfo, tab) { + var onTabUpdated = function(tabId, changeInfo, tab) { if ( !tab.url || tab.url === '' ) { return; } @@ -47,16 +47,40 @@ return; } µb.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated'); - } + }; chrome.tabs.onUpdated.addListener(onTabUpdated); - function onTabRemoved(tabId) { + var onTabRemoved = function(tabId) { if ( tabId < 0 ) { return; } µb.unbindTabFromPageStats(tabId); - } + }; chrome.tabs.onRemoved.addListener(onTabRemoved); + + // https://github.com/gorhill/uBlock/issues/297 + var onCreatedNavigationTarget = function(details) { + var pageStore = µb.pageStoreFromTabId(details.sourceTabId); + if ( !pageStore ) { + return; + } + if ( pageStore.getNetFilteringSwitch() !== true ) { + return; + } + var requestURL = details.url; + var result = µb.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup'); + // https://github.com/gorhill/uBlock/issues/91 + pageStore.recordResult('popup', requestURL, result); + // Not blocked + if ( pageStore.boolFromResult(result) === false ) { + return; + } + // Blocked + // It is a popup, block and remove the tab. + µb.unbindTabFromPageStats(details.tabId); + µb.XAL.destroyTab(details.tabId); + }; + chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget); })(); /******************************************************************************/ diff --git a/js/traffic.js b/js/traffic.js index e5c126ea4..a3fdd38ce 100644 --- a/js/traffic.js +++ b/js/traffic.js @@ -154,7 +154,7 @@ var onBeforeSendHeaders = function(details) { // Lookup the page store associated with this tab id. var pageStore = µb.pageStoreFromTabId(tabId); if ( !pageStore ) { - console.error('µBlock> onBeforeSendHeaders(): no page store for "%s"', requestURL); + // This happens under normal circumstances in Opera. return; } @@ -201,7 +201,7 @@ var onBeforeSendHeaders = function(details) { // It is a popup, block and remove the tab. µb.unbindTabFromPageStats(tabId); - chrome.tabs.remove(tabId); + µb.XAL.destroyTab(tabId); return { 'cancel': true }; }; diff --git a/js/xal.js b/js/xal.js index 9eef68b29..51ef83d9a 100644 --- a/js/xal.js +++ b/js/xal.js @@ -86,6 +86,16 @@ exports.restart = function() { /******************************************************************************/ +exports.destroyTab = function(tabId) { + chrome.tabs.remove(tabId, function() { + // required by chrome API, or else warnings at console (also, mind jshint) + if ( chrome.runtime.lastError ) { + } + }); +}; + +/******************************************************************************/ + return exports; /******************************************************************************/