1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00
This commit is contained in:
gorhill 2015-01-02 13:42:35 -05:00
parent b1143614fb
commit 00995992d2
2 changed files with 70 additions and 23 deletions

View File

@ -86,18 +86,22 @@ vAPI.tabs.get = function(tabId, callback) {
var onTabReady = function(tab) { var onTabReady = function(tab) {
// https://code.google.com/p/chromium/issues/detail?id=410868#c8 // https://code.google.com/p/chromium/issues/detail?id=410868#c8
if ( chrome.runtime.lastError ) { if ( chrome.runtime.lastError ) {
return; ;
} }
// Caller must be prepared to deal with nil tab value
callback(tab); callback(tab);
}; };
if ( tabId !== null ) { if ( tabId !== null ) {
if ( typeof tabId === 'string' ) {
tabId = parseInt(tabId, 10);
}
chrome.tabs.get(tabId, onTabReady); chrome.tabs.get(tabId, onTabReady);
return; return;
} }
var onTabReceived = function(tabs) { var onTabReceived = function(tabs) {
// https://code.google.com/p/chromium/issues/detail?id=410868#c8 // https://code.google.com/p/chromium/issues/detail?id=410868#c8
if ( chrome.runtime.lastError ) { if ( chrome.runtime.lastError ) {
return; ;
} }
callback(tabs[0]); callback(tabs[0]);
}; };

View File

@ -19,18 +19,29 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
/* global µBlock */ /* global vAPI, µBlock */
/******************************************************************************/
/******************************************************************************/
(function() {
'use strict'; 'use strict';
/******************************************************************************/ /******************************************************************************/
var µb = µBlock;
/******************************************************************************/
/******************************************************************************/
// When the DOM content of root frame is loaded, this means the tab // When the DOM content of root frame is loaded, this means the tab
// content has changed. // content has changed.
vAPI.tabs.onNavigation = function(details) { vAPI.tabs.onNavigation = function(details) {
if ( details.frameId !== 0 ) { if ( details.frameId !== 0 ) {
return; return;
} }
µBlock.bindTabToPageStats(details.tabId, details.url); µb.bindTabToPageStats(details.tabId, details.url);
}; };
// It may happen the URL in the tab changes, while the page's document // It may happen the URL in the tab changes, while the page's document
@ -43,19 +54,19 @@ vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
if ( !changeInfo.url ) { if ( !changeInfo.url ) {
return; return;
} }
µBlock.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated'); µb.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated');
}; };
vAPI.tabs.onClosed = function(tabId) { vAPI.tabs.onClosed = function(tabId) {
if ( tabId < 0 ) { if ( tabId < 0 ) {
return; return;
} }
µBlock.unbindTabFromPageStats(tabId); µb.unbindTabFromPageStats(tabId);
}; };
// https://github.com/gorhill/uBlock/issues/297 // https://github.com/gorhill/uBlock/issues/297
vAPI.tabs.onPopup = function(details) { vAPI.tabs.onPopup = function(details) {
var pageStore = µBlock.pageStoreFromTabId(details.sourceTabId); var pageStore = µb.pageStoreFromTabId(details.sourceTabId);
if ( !pageStore ) { if ( !pageStore ) {
return; return;
} }
@ -64,8 +75,8 @@ vAPI.tabs.onPopup = function(details) {
// https://github.com/gorhill/uBlock/issues/323 // https://github.com/gorhill/uBlock/issues/323
// If popup URL is whitelisted, do not block it // If popup URL is whitelisted, do not block it
if ( µBlock.getNetFilteringSwitch(requestURL) ) { if ( µb.getNetFilteringSwitch(requestURL) ) {
result = µBlock.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup'); result = µb.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup');
} }
// https://github.com/gorhill/uBlock/issues/91 // https://github.com/gorhill/uBlock/issues/91
@ -81,7 +92,7 @@ vAPI.tabs.onPopup = function(details) {
// Blocked // Blocked
// It is a popup, block and remove the tab. // It is a popup, block and remove the tab.
µBlock.unbindTabFromPageStats(details.tabId); µb.unbindTabFromPageStats(details.tabId);
vAPI.tabs.remove(details.tabId); vAPI.tabs.remove(details.tabId);
// for Safari // for Safari
@ -90,7 +101,6 @@ vAPI.tabs.onPopup = function(details) {
vAPI.tabs.registerListeners(); vAPI.tabs.registerListeners();
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
@ -104,7 +114,7 @@ vAPI.tabs.registerListeners();
// hostname. This way, for a specific scheme you can create scope with // hostname. This way, for a specific scheme you can create scope with
// rules which will apply only to that scheme. // rules which will apply only to that scheme.
µBlock.normalizePageURL = function(pageURL) { µb.normalizePageURL = function(pageURL) {
var uri = this.URI.set(pageURL); var uri = this.URI.set(pageURL);
if ( uri.scheme === 'https' || uri.scheme === 'http' ) { if ( uri.scheme === 'https' || uri.scheme === 'http' ) {
return uri.normalizedURI(); return uri.normalizedURI();
@ -116,7 +126,7 @@ vAPI.tabs.registerListeners();
// Create an entry for the tab if it doesn't exist. // Create an entry for the tab if it doesn't exist.
µBlock.bindTabToPageStats = function(tabId, pageURL, context) { µb.bindTabToPageStats = function(tabId, pageURL, context) {
this.updateBadgeAsync(tabId); this.updateBadgeAsync(tabId);
// https://github.com/gorhill/httpswitchboard/issues/303 // https://github.com/gorhill/httpswitchboard/issues/303
@ -144,7 +154,7 @@ vAPI.tabs.registerListeners();
return pageStore; return pageStore;
}; };
µBlock.unbindTabFromPageStats = function(tabId) { µb.unbindTabFromPageStats = function(tabId) {
//console.debug('µBlock> unbindTabFromPageStats(%d)', tabId); //console.debug('µBlock> unbindTabFromPageStats(%d)', tabId);
var pageStore = this.pageStores[tabId]; var pageStore = this.pageStores[tabId];
if ( pageStore !== undefined ) { if ( pageStore !== undefined ) {
@ -155,27 +165,60 @@ vAPI.tabs.registerListeners();
/******************************************************************************/ /******************************************************************************/
µBlock.pageUrlFromTabId = function(tabId) { µb.pageUrlFromTabId = function(tabId) {
var pageStore = this.pageStores[tabId]; var pageStore = this.pageStores[tabId];
return pageStore ? pageStore.pageURL : ''; return pageStore ? pageStore.pageURL : '';
}; };
µBlock.pageUrlFromPageStats = function(pageStats) { µb.pageUrlFromPageStats = function(pageStats) {
if ( pageStats ) { if ( pageStats ) {
return pageStats.pageURL; return pageStats.pageURL;
} }
return ''; return '';
}; };
µBlock.pageStoreFromTabId = function(tabId) { µb.pageStoreFromTabId = function(tabId) {
return this.pageStores[tabId]; return this.pageStores[tabId];
}; };
/******************************************************************************/
/******************************************************************************/ /******************************************************************************/
// µBlock.forceReload = function(pageURL) { // Stale page store entries janitor
// var tabId = this.tabIdFromPageUrl(pageURL); // https://github.com/gorhill/uBlock/issues/455
// if ( tabId ) {
// chrome.tabs.reload(tabId, { bypassCache: true }); var pageStoreJanitorPeriod = 15 * 60 * 1000;
// } var pageStoreJanitorSampleAt = 0;
// }; var pageStoreJanitorSampleSize = 10;
var pageStoreJanitor = function() {
var vapiTabs = vAPI.tabs;
var tabIds = Object.keys(µb.pageStores).sort();
var checkTab = function(tabId) {
vapiTabs.get(tabId, function(tab) {
if ( !tab ) {
//console.error('tab.js> pageStoreJanitor(): stale page store found:', µb.pageUrlFromTabId(tabId));
µb.unbindTabFromPageStats(tabId);
}
});
};
if ( pageStoreJanitorSampleAt >= tabIds.length ) {
pageStoreJanitorSampleAt = 0;
}
var n = Math.min(pageStoreJanitorSampleAt + pageStoreJanitorSampleSize, tabIds.length);
for ( var i = pageStoreJanitorSampleAt; i < n; i++ ) {
checkTab(tabIds[i]);
}
pageStoreJanitorSampleAt = n;
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
};
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
/******************************************************************************/
/******************************************************************************/
})();
/******************************************************************************/