2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µBlock - a Chromium browser extension to block requests.
|
|
|
|
Copyright (C) 2014 Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
/* global vAPI, µBlock */
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
2014-10-19 13:11:27 +02:00
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
var µb = µBlock;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// When the DOM content of root frame is loaded, this means the tab
|
|
|
|
// content has changed.
|
|
|
|
vAPI.tabs.onNavigation = function(details) {
|
|
|
|
if ( details.frameId !== 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-17 13:19:48 +01:00
|
|
|
µb.bindTabToPageStats(details.tabId, details.url, 'afterNavigate');
|
2014-10-17 21:44:19 +02:00
|
|
|
};
|
2014-09-08 19:45:03 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// 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.
|
|
|
|
vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
|
|
|
|
if ( !tab.url || tab.url === '' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( !changeInfo.url ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated');
|
2014-10-17 21:44:19 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
vAPI.tabs.onClosed = function(tabId) {
|
|
|
|
if ( tabId < 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.unbindTabFromPageStats(tabId);
|
2014-10-17 21:44:19 +02:00
|
|
|
};
|
2014-10-15 19:14:25 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/297
|
|
|
|
vAPI.tabs.onPopup = function(details) {
|
2014-12-28 16:07:43 +01:00
|
|
|
//console.debug('vAPI.tabs.onPopup: url="%s"', details.url);
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
var pageStore = µb.pageStoreFromTabId(details.sourceTabId);
|
2014-10-17 21:44:19 +02:00
|
|
|
if ( !pageStore ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var requestURL = details.url;
|
|
|
|
var result = '';
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/323
|
|
|
|
// If popup URL is whitelisted, do not block it
|
2015-01-02 19:42:35 +01:00
|
|
|
if ( µb.getNetFilteringSwitch(requestURL) ) {
|
2015-01-02 20:19:12 +01:00
|
|
|
result = µb.staticNetFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup');
|
2014-10-17 21:44:19 +02:00
|
|
|
}
|
2014-10-15 19:14:25 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/91
|
2014-10-24 16:14:25 +02:00
|
|
|
if ( result !== '' ) {
|
2015-01-06 14:01:15 +01:00
|
|
|
var context = {
|
|
|
|
requestURL: requestURL,
|
|
|
|
requestHostname: µb.URI.hostnameFromURI(requestURL),
|
|
|
|
requestType: 'popup'
|
|
|
|
};
|
|
|
|
pageStore.logBuffer.writeOne(context, result);
|
2014-10-24 16:14:25 +02:00
|
|
|
}
|
2014-10-15 19:14:25 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// Not blocked
|
|
|
|
if ( pageStore.boolFromResult(result) === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blocked
|
2014-10-24 16:14:25 +02:00
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// It is a popup, block and remove the tab.
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.unbindTabFromPageStats(details.tabId);
|
2014-11-16 14:09:28 +01:00
|
|
|
vAPI.tabs.remove(details.tabId);
|
2014-12-01 20:45:00 +01:00
|
|
|
|
2015-01-02 18:41:41 +01:00
|
|
|
// for Safari and Firefox
|
2014-12-01 20:45:00 +01:00
|
|
|
return true;
|
2014-10-17 21:44:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.tabs.registerListeners();
|
2014-10-15 19:14:25 +02:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/303
|
|
|
|
// Some kind of trick going on here:
|
|
|
|
// Any scheme other than 'http' and 'https' is remapped into a fake
|
|
|
|
// URL which trick the rest of µBlock into being able to process an
|
|
|
|
// otherwise unmanageable scheme. µBlock needs web page to have a proper
|
|
|
|
// hostname to work properly, so just like the 'chromium-behind-the-scene'
|
|
|
|
// fake domain name, we map unknown schemes into a fake '{scheme}-scheme'
|
|
|
|
// hostname. This way, for a specific scheme you can create scope with
|
|
|
|
// rules which will apply only to that scheme.
|
|
|
|
|
2015-01-20 00:42:58 +01:00
|
|
|
µb.normalizePageURL = function(tabId, pageURL) {
|
|
|
|
if ( vAPI.isNoTabId(tabId) ) {
|
|
|
|
return 'http://behind-the-scene/';
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
var uri = this.URI.set(pageURL);
|
2015-01-20 00:42:58 +01:00
|
|
|
var scheme = uri.scheme;
|
|
|
|
if ( scheme === 'https' || scheme === 'http' ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
return uri.normalizedURI();
|
|
|
|
}
|
2015-01-20 00:42:58 +01:00
|
|
|
|
|
|
|
if ( uri.hostname !== '' ) {
|
|
|
|
return 'http://' + scheme + '-' + uri.hostname + uri.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'http://' + scheme + '-scheme/';
|
2014-06-27 23:06:42 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Create an entry for the tab if it doesn't exist.
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.bindTabToPageStats = function(tabId, pageURL, context) {
|
2014-07-07 01:14:32 +02:00
|
|
|
this.updateBadgeAsync(tabId);
|
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/303
|
2014-07-14 17:24:59 +02:00
|
|
|
// Normalize page URL
|
2015-01-20 00:42:58 +01:00
|
|
|
pageURL = this.normalizePageURL(tabId, pageURL);
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-07-14 17:24:59 +02:00
|
|
|
// Do not create a page store for URLs which are of no interests
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( pageURL === '' ) {
|
2014-07-14 17:24:59 +02:00
|
|
|
this.unbindTabFromPageStats(tabId);
|
2014-06-24 00:42:43 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-17 13:19:48 +01:00
|
|
|
// Reuse page store if one exists: this allows to guess if a tab is a popup
|
2014-06-24 00:42:43 +02:00
|
|
|
var pageStore = this.pageStores[tabId];
|
2014-08-02 17:40:27 +02:00
|
|
|
|
2015-01-17 13:19:48 +01:00
|
|
|
// Tab is not bound
|
|
|
|
if ( !pageStore ) {
|
|
|
|
return this.pageStores[tabId] = this.PageStore.factory(tabId, pageURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/516
|
|
|
|
// If context if 'beforeRequest', do not rebind
|
|
|
|
if ( context === 'beforeRequest' ) {
|
|
|
|
return pageStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( context === 'afterNavigate' ) {
|
|
|
|
pageStore.reuse(pageURL, context);
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pageStore;
|
|
|
|
};
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.unbindTabFromPageStats = function(tabId) {
|
2014-07-16 23:20:11 +02:00
|
|
|
//console.debug('µBlock> unbindTabFromPageStats(%d)', tabId);
|
2014-09-14 22:20:40 +02:00
|
|
|
var pageStore = this.pageStores[tabId];
|
|
|
|
if ( pageStore !== undefined ) {
|
|
|
|
pageStore.dispose();
|
|
|
|
delete this.pageStores[tabId];
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.pageUrlFromTabId = function(tabId) {
|
2014-06-24 00:42:43 +02:00
|
|
|
var pageStore = this.pageStores[tabId];
|
|
|
|
return pageStore ? pageStore.pageURL : '';
|
|
|
|
};
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.pageUrlFromPageStats = function(pageStats) {
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( pageStats ) {
|
|
|
|
return pageStats.pageURL;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
µb.pageStoreFromTabId = function(tabId) {
|
2014-06-24 00:42:43 +02:00
|
|
|
return this.pageStores[tabId];
|
|
|
|
};
|
|
|
|
|
2015-01-20 00:42:58 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Permanent page store for behind-the-scene requests. Must never be removed.
|
|
|
|
|
|
|
|
µb.pageStores[vAPI.noTabId] = µb.PageStore.factory(
|
|
|
|
vAPI.noTabId,
|
|
|
|
µb.normalizePageURL(vAPI.noTabId)
|
|
|
|
);
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Stale page store entries janitor
|
|
|
|
// https://github.com/gorhill/uBlock/issues/455
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-01-20 00:42:58 +01:00
|
|
|
var tabId;
|
2015-01-02 19:42:35 +01:00
|
|
|
var n = Math.min(pageStoreJanitorSampleAt + pageStoreJanitorSampleSize, tabIds.length);
|
|
|
|
for ( var i = pageStoreJanitorSampleAt; i < n; i++ ) {
|
2015-01-20 00:42:58 +01:00
|
|
|
tabId = tabIds[i];
|
|
|
|
// Do not remove behind-the-scene page store
|
|
|
|
if ( vAPI.isNoTabId(tabId) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
checkTab(tabId);
|
2015-01-02 19:42:35 +01:00
|
|
|
}
|
|
|
|
pageStoreJanitorSampleAt = n;
|
|
|
|
|
|
|
|
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-02 19:42:35 +01:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|