this fixes #297 and #91

This commit is contained in:
gorhill 2014-10-10 13:26:43 -04:00
parent c2d0e3dd3f
commit c7eebec310
3 changed files with 42 additions and 8 deletions

View File

@ -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);
})();
/******************************************************************************/

View File

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

View File

@ -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;
/******************************************************************************/