1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-06 02:42:33 +01:00
uBlock/js/tab.js

182 lines
6.0 KiB
JavaScript
Raw Normal View History

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
*/
/* global chrome, µBlock */
/******************************************************************************/
(function(){
2014-09-08 19:45:03 +02:00
var µb = µBlock;
2014-07-07 01:14:32 +02:00
// When the DOM content of root frame is loaded, this means the tab
// content has changed.
2014-10-10 19:26:43 +02:00
var onNavigationCommitted = function(details) {
2014-07-07 01:14:32 +02:00
if ( details.frameId !== 0 ) {
2014-06-24 00:42:43 +02:00
return;
}
2014-09-08 19:45:03 +02:00
µb.bindTabToPageStats(details.tabId, details.url);
2014-10-10 19:26:43 +02:00
};
2014-08-02 17:40:27 +02:00
chrome.webNavigation.onCommitted.addListener(onNavigationCommitted);
2014-06-24 00:42:43 +02:00
2014-07-07 01:14:32 +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.
2014-10-10 19:26:43 +02:00
var onTabUpdated = function(tabId, changeInfo, tab) {
2014-06-24 00:42:43 +02:00
if ( !tab.url || tab.url === '' ) {
return;
}
2014-08-02 17:40:27 +02:00
if ( !changeInfo.url ) {
2014-07-14 17:24:59 +02:00
return;
}
2014-10-02 22:45:26 +02:00
µb.bindTabToPageStats(tabId, changeInfo.url, 'tabUpdated');
2014-10-10 19:26:43 +02:00
};
2014-06-24 00:42:43 +02:00
chrome.tabs.onUpdated.addListener(onTabUpdated);
2014-10-10 19:26:43 +02:00
var onTabRemoved = function(tabId) {
2014-07-07 02:57:23 +02:00
if ( tabId < 0 ) {
return;
}
2014-09-08 19:45:03 +02:00
µb.unbindTabFromPageStats(tabId);
2014-10-10 19:26:43 +02:00
};
2014-07-07 02:57:23 +02:00
chrome.tabs.onRemoved.addListener(onTabRemoved);
2014-10-10 19:26:43 +02:00
// https://github.com/gorhill/uBlock/issues/297
var onCreatedNavigationTarget = function(details) {
var pageStore = µb.pageStoreFromTabId(details.sourceTabId);
if ( !pageStore ) {
return;
}
var requestURL = details.url;
2014-10-15 19:14:25 +02:00
var result = '';
// https://github.com/gorhill/uBlock/issues/323
// If popup URL is whitelisted, do not block it
if ( µb.getNetFilteringSwitch(requestURL) ) {
result = µb.netFilteringEngine.matchStringExactType(pageStore, requestURL, 'popup');
}
2014-10-10 19:26:43 +02:00
// https://github.com/gorhill/uBlock/issues/91
if ( result !== '' ) {
pageStore.recordResult('popup', requestURL, result);
}
2014-10-15 19:14:25 +02:00
2014-10-10 19:26:43 +02:00
// Not blocked
if ( pageStore.boolFromResult(result) === false ) {
return;
}
2014-10-15 19:14:25 +02:00
2014-10-10 19:26:43 +02:00
// Blocked
// It is a popup, block and remove the tab.
µb.unbindTabFromPageStats(details.tabId);
µb.XAL.destroyTab(details.tabId);
};
chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
2014-09-08 19:45:03 +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.
µBlock.normalizePageURL = function(pageURL) {
var uri = this.URI.set(pageURL);
if ( uri.scheme === 'https' || uri.scheme === 'http' ) {
return uri.normalizedURI();
}
return '';
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.
2014-08-02 17:40:27 +02:00
µBlock.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
2014-06-24 00:42:43 +02:00
pageURL = this.normalizePageURL(pageURL);
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;
}
2014-07-14 17:24:59 +02: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
2014-07-14 17:24:59 +02:00
if ( pageStore ) {
2014-08-02 17:40:27 +02:00
if ( pageURL !== pageStore.pageURL || context === 'beforeRequest' ) {
2014-10-02 22:45:26 +02:00
pageStore.reuse(pageURL, context);
2014-08-02 17:40:27 +02:00
}
2014-07-14 17:24:59 +02:00
} else {
2014-08-02 17:40:27 +02:00
pageStore = this.pageStores[tabId] = this.PageStore.factory(tabId, pageURL);
2014-06-24 00:42:43 +02:00
}
return pageStore;
};
µBlock.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
};
/******************************************************************************/
µBlock.pageUrlFromTabId = function(tabId) {
var pageStore = this.pageStores[tabId];
return pageStore ? pageStore.pageURL : '';
};
µBlock.pageUrlFromPageStats = function(pageStats) {
if ( pageStats ) {
return pageStats.pageURL;
}
return '';
};
µBlock.pageStoreFromTabId = function(tabId) {
return this.pageStores[tabId];
};
/******************************************************************************/
µBlock.forceReload = function(pageURL) {
var tabId = this.tabIdFromPageUrl(pageURL);
if ( tabId ) {
chrome.tabs.reload(tabId, { bypassCache: true });
}
};