2014-06-23 16:42:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-02-26 07:48:43 -07:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-09-03 12:06:49 -06:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-23 16:42:43 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2014-10-19 05:11:27 -06:00
|
|
|
'use strict';
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
import contextMenu from './contextmenu.js';
|
|
|
|
import logger from './logger.js';
|
|
|
|
import scriptletFilteringEngine from './scriptlet-filtering.js';
|
|
|
|
import staticNetFilteringEngine from './static-net-filtering.js';
|
|
|
|
import µb from './background.js';
|
2021-10-07 12:41:29 -06:00
|
|
|
import webext from './webext.js';
|
2021-07-28 17:48:38 -06:00
|
|
|
import { PageStore } from './pagestore.js';
|
2021-08-03 10:19:25 -06:00
|
|
|
|
|
|
|
import {
|
|
|
|
sessionFirewall,
|
|
|
|
sessionSwitches,
|
|
|
|
sessionURLFiltering,
|
|
|
|
} from './filtering-engines.js';
|
2021-07-28 17:48:38 -06:00
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
import {
|
|
|
|
domainFromHostname,
|
|
|
|
hostnameFromURI,
|
|
|
|
isNetworkURI,
|
|
|
|
originFromURI,
|
|
|
|
} from './uri-utils.js';
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
2019-07-05 15:44:08 -06:00
|
|
|
/******************************************************************************/
|
2015-01-02 11:42:35 -07:00
|
|
|
|
2015-04-08 16:46:08 -06:00
|
|
|
// https://github.com/gorhill/httpswitchboard/issues/303
|
|
|
|
// 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.
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.normalizeTabURL = (( ) => {
|
2021-07-25 08:55:35 -06:00
|
|
|
const tabURLNormalizer = new URL('about:blank');
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
return (tabId, tabURL) => {
|
|
|
|
if ( tabId < 0 ) {
|
|
|
|
return 'http://behind-the-scene/';
|
|
|
|
}
|
2021-10-18 07:43:41 -06:00
|
|
|
try {
|
|
|
|
tabURLNormalizer.href = tabURL;
|
|
|
|
} catch(ex) {
|
|
|
|
return tabURL;
|
|
|
|
}
|
2021-07-25 08:55:35 -06:00
|
|
|
const protocol = tabURLNormalizer.protocol.slice(0, -1);
|
|
|
|
if ( protocol === 'https' || protocol === 'http' ) {
|
|
|
|
return tabURLNormalizer.href;
|
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
let fakeHostname = protocol + '-scheme';
|
|
|
|
|
|
|
|
if ( tabURLNormalizer.hostname !== '' ) {
|
|
|
|
fakeHostname = tabURLNormalizer.hostname + '.' + fakeHostname;
|
|
|
|
} else if ( protocol === 'about' && protocol.pathname !== '' ) {
|
|
|
|
fakeHostname = tabURLNormalizer.pathname + '.' + fakeHostname;
|
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
return `http://${fakeHostname}/`;
|
|
|
|
};
|
|
|
|
})();
|
2015-04-08 16:46:08 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/99
|
|
|
|
// https://github.com/gorhill/uBlock/issues/991
|
|
|
|
//
|
|
|
|
// popup:
|
|
|
|
// Test/close target URL
|
|
|
|
// popunder:
|
|
|
|
// Test/close opener URL
|
|
|
|
//
|
|
|
|
// popup filter match:
|
|
|
|
// 0 = false
|
|
|
|
// 1 = true
|
|
|
|
//
|
|
|
|
// opener: 0 0 1 1
|
|
|
|
// target: 0 1 0 1
|
|
|
|
// ---- ---- ---- ----
|
|
|
|
// result: a b c d
|
|
|
|
//
|
|
|
|
// a: do nothing
|
|
|
|
// b: close target
|
|
|
|
// c: close opener
|
|
|
|
// d: close target
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
const onPopupUpdated = (( ) => {
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/commit/1d448b85b2931412508aa01bf899e0b6f0033626#commitcomment-14944764
|
|
|
|
// See if two URLs are different, disregarding scheme -- because the
|
|
|
|
// scheme can be unilaterally changed by the browser.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1378
|
|
|
|
// Maybe no link element was clicked.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3287
|
|
|
|
// Do not bail out if the target URL has no hostname.
|
|
|
|
const areDifferentURLs = function(a, b) {
|
|
|
|
if ( b === '' ) { return true; }
|
|
|
|
if ( b.startsWith('about:') ) { return false; }
|
|
|
|
let pos = a.indexOf('://');
|
|
|
|
if ( pos === -1 ) { return false; }
|
|
|
|
a = a.slice(pos);
|
|
|
|
pos = b.indexOf('://');
|
|
|
|
if ( pos !== -1 ) {
|
|
|
|
b = b.slice(pos);
|
|
|
|
}
|
|
|
|
return b !== a;
|
|
|
|
};
|
2019-07-05 15:44:08 -06:00
|
|
|
|
2019-09-22 07:19:57 -06:00
|
|
|
const popupMatch = function(
|
|
|
|
fctxt,
|
2019-09-23 06:25:23 -06:00
|
|
|
rootOpenerURL,
|
|
|
|
localOpenerURL,
|
2019-09-22 07:19:57 -06:00
|
|
|
targetURL,
|
2019-09-23 06:25:23 -06:00
|
|
|
popupType = 'popup'
|
2019-09-22 07:19:57 -06:00
|
|
|
) {
|
2021-06-24 09:02:06 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/323
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1142
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1616
|
|
|
|
// Don't block if uBO is turned off in popup's context
|
|
|
|
if (
|
|
|
|
µb.getNetFilteringSwitch(targetURL) === false ||
|
2021-07-25 08:55:35 -06:00
|
|
|
µb.getNetFilteringSwitch(µb.normalizeTabURL(0, targetURL)) === false
|
2021-06-24 09:02:06 -06:00
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-23 06:25:23 -06:00
|
|
|
fctxt.setTabOriginFromURL(rootOpenerURL)
|
|
|
|
.setDocOriginFromURL(localOpenerURL || rootOpenerURL)
|
2019-07-21 08:48:44 -06:00
|
|
|
.setURL(targetURL)
|
|
|
|
.setType('popup');
|
2019-07-05 15:44:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1735
|
|
|
|
// Do not bail out on `data:` URI, they are commonly used for popups.
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/255
|
|
|
|
// Do not bail out on `about:blank`: an `about:blank` popup can be
|
|
|
|
// opened, with the sole purpose to serve as an intermediary in
|
|
|
|
// a sequence of chained popups.
|
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/263#issuecomment-272615772
|
|
|
|
// Do not bail out, period: the static filtering engine must be
|
|
|
|
// able to examine all sorts of URLs for popup filtering purpose.
|
2015-12-01 13:07:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Dynamic filtering makes sense only when we have a valid opener
|
|
|
|
// hostname.
|
|
|
|
// https://github.com/gorhill/uBlock/commit/1d448b85b2931412508aa01bf899e0b6f0033626#commitcomment-14944764
|
|
|
|
// Ignore bad target URL. On Firefox, an `about:blank` tab may be
|
|
|
|
// opened for a new tab before it is filled in with the real target
|
|
|
|
// URL.
|
|
|
|
if ( fctxt.getTabHostname() !== '' && targetURL !== 'about:blank' ) {
|
|
|
|
// Check per-site switch first
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3060
|
|
|
|
// - The no-popups switch must apply only to popups, not to
|
|
|
|
// popunders.
|
|
|
|
if (
|
|
|
|
popupType === 'popup' &&
|
2021-07-28 17:48:38 -06:00
|
|
|
sessionSwitches.evaluateZ(
|
2019-07-21 08:48:44 -06:00
|
|
|
'no-popups',
|
|
|
|
fctxt.getTabHostname()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
fctxt.filter = {
|
2021-07-28 17:48:38 -06:00
|
|
|
raw: 'no-popups: ' + sessionSwitches.z + ' true',
|
2019-07-21 08:48:44 -06:00
|
|
|
result: 1,
|
|
|
|
source: 'switch'
|
|
|
|
};
|
|
|
|
return 1;
|
2019-07-05 15:44:08 -06:00
|
|
|
}
|
2015-12-01 13:07:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/581
|
|
|
|
// Take into account popup-specific rules in dynamic URL
|
|
|
|
// filtering, OR generic allow rules.
|
2021-07-28 17:48:38 -06:00
|
|
|
let result = sessionURLFiltering.evaluateZ(
|
2019-07-21 08:48:44 -06:00
|
|
|
fctxt.getTabHostname(),
|
|
|
|
targetURL,
|
|
|
|
popupType
|
2019-07-05 15:44:08 -06:00
|
|
|
);
|
2018-02-27 04:54:48 -07:00
|
|
|
if (
|
2021-07-28 17:48:38 -06:00
|
|
|
result === 1 && sessionURLFiltering.type === popupType ||
|
2019-07-21 08:48:44 -06:00
|
|
|
result === 2
|
2018-02-27 04:54:48 -07:00
|
|
|
) {
|
2021-07-28 17:48:38 -06:00
|
|
|
fctxt.filter = sessionURLFiltering.toLogData();
|
2019-07-21 08:48:44 -06:00
|
|
|
return result;
|
2015-12-01 13:07:22 -07:00
|
|
|
}
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/581
|
|
|
|
// Take into account `allow` rules in dynamic filtering: `block`
|
|
|
|
// rules are ignored, as block rules are not meant to block
|
|
|
|
// specific types like `popup` (just like with static filters).
|
2021-07-28 17:48:38 -06:00
|
|
|
result = sessionFirewall.evaluateCellZY(
|
2019-07-21 08:48:44 -06:00
|
|
|
fctxt.getTabHostname(),
|
|
|
|
fctxt.getHostname(),
|
|
|
|
popupType
|
2018-02-27 04:54:48 -07:00
|
|
|
);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( result === 2 ) {
|
2021-07-28 17:48:38 -06:00
|
|
|
fctxt.filter = sessionFirewall.toLogData();
|
2019-07-21 08:48:44 -06:00
|
|
|
return 2;
|
|
|
|
}
|
2015-12-01 13:07:22 -07:00
|
|
|
}
|
|
|
|
|
2021-06-24 09:02:06 -06:00
|
|
|
fctxt.type = popupType;
|
2021-07-28 17:48:38 -06:00
|
|
|
const result = staticNetFilteringEngine.matchRequest(fctxt, 0b0001);
|
2021-06-24 09:02:06 -06:00
|
|
|
if ( result !== 0 ) {
|
2021-07-28 17:48:38 -06:00
|
|
|
fctxt.filter = staticNetFilteringEngine.toLogData();
|
2021-06-24 09:02:06 -06:00
|
|
|
return result;
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
return 0;
|
2015-05-28 12:47:19 -06:00
|
|
|
};
|
|
|
|
|
2019-09-22 07:19:57 -06:00
|
|
|
const mapPopunderResult = function(
|
|
|
|
fctxt,
|
|
|
|
popunderURL,
|
|
|
|
popunderHostname,
|
|
|
|
result
|
|
|
|
) {
|
2021-08-04 13:14:48 -06:00
|
|
|
if ( fctxt.filter === undefined || fctxt.filter !== 'static' ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ( fctxt.filter.isUntokenized() ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
return 0;
|
2015-04-08 16:46:08 -06:00
|
|
|
}
|
2021-08-04 13:14:48 -06:00
|
|
|
if ( fctxt.filter.isPureHostname() ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
return result;
|
2015-04-08 16:46:08 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
const re = new RegExp(fctxt.filter.regex, 'i');
|
|
|
|
const matches = re.exec(popunderURL);
|
|
|
|
if ( matches === null ) { return 0; }
|
|
|
|
const beg = matches.index;
|
|
|
|
const end = beg + matches[0].length;
|
|
|
|
const pos = popunderURL.indexOf(popunderHostname);
|
|
|
|
if ( pos === -1 ) { return 0; }
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1471
|
|
|
|
// We test whether the opener hostname as at least one character
|
|
|
|
// within matched portion of URL.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1903
|
|
|
|
// Ignore filters which cause a match before the start of the
|
|
|
|
// hostname in the URL.
|
|
|
|
return beg >= pos && beg < pos + popunderHostname.length && end > pos
|
|
|
|
? result
|
|
|
|
: 0;
|
2015-04-08 16:46:08 -06:00
|
|
|
};
|
|
|
|
|
2019-09-22 07:19:57 -06:00
|
|
|
const popunderMatch = function(
|
|
|
|
fctxt,
|
2019-09-23 06:25:23 -06:00
|
|
|
rootOpenerURL,
|
|
|
|
localOpenerURL,
|
2019-09-22 07:19:57 -06:00
|
|
|
targetURL
|
|
|
|
) {
|
2019-09-23 06:25:23 -06:00
|
|
|
let result = popupMatch(
|
|
|
|
fctxt,
|
|
|
|
targetURL,
|
|
|
|
undefined,
|
|
|
|
rootOpenerURL,
|
|
|
|
'popunder'
|
|
|
|
);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( result === 1 ) { return result; }
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1010#issuecomment-186824878
|
|
|
|
// Check the opener tab as if it were the newly opened tab: if there
|
|
|
|
// is a hit against a popup filter, and if the matching filter is not
|
|
|
|
// a broad one, we will consider the opener tab to be a popunder tab.
|
|
|
|
// For now, a "broad" filter is one which does not touch any part of
|
|
|
|
// the hostname part of the opener URL.
|
2019-09-23 06:25:23 -06:00
|
|
|
let popunderURL = rootOpenerURL,
|
2021-07-25 08:55:35 -06:00
|
|
|
popunderHostname = hostnameFromURI(popunderURL);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( popunderHostname === '' ) { return 0; }
|
|
|
|
|
|
|
|
result = mapPopunderResult(
|
2019-09-22 07:19:57 -06:00
|
|
|
fctxt,
|
2019-07-21 08:48:44 -06:00
|
|
|
popunderURL,
|
|
|
|
popunderHostname,
|
2019-09-23 06:25:23 -06:00
|
|
|
popupMatch(fctxt, targetURL, undefined, popunderURL)
|
2019-07-21 08:48:44 -06:00
|
|
|
);
|
|
|
|
if ( result !== 0 ) { return result; }
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1598
|
|
|
|
// Try to find a match against origin part of the opener URL.
|
2021-07-25 08:55:35 -06:00
|
|
|
popunderURL = originFromURI(popunderURL);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( popunderURL === '' ) { return 0; }
|
|
|
|
|
|
|
|
return mapPopunderResult(
|
2019-09-22 07:19:57 -06:00
|
|
|
fctxt,
|
2019-07-21 08:48:44 -06:00
|
|
|
popunderURL,
|
|
|
|
popunderHostname,
|
2019-09-23 06:25:23 -06:00
|
|
|
popupMatch(fctxt, targetURL, undefined, popunderURL)
|
2019-07-21 08:48:44 -06:00
|
|
|
);
|
2015-05-28 12:47:19 -06:00
|
|
|
};
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
return function(targetTabId, openerDetails) {
|
|
|
|
// Opener details.
|
|
|
|
const openerTabId = openerDetails.tabId;
|
|
|
|
let tabContext = µb.tabContextManager.lookup(openerTabId);
|
|
|
|
if ( tabContext === null ) { return; }
|
2019-09-23 06:25:23 -06:00
|
|
|
const rootOpenerURL = tabContext.rawURL;
|
|
|
|
if ( rootOpenerURL === '' ) { return; }
|
|
|
|
const localOpenerURL = openerDetails.frameId !== 0
|
|
|
|
? openerDetails.frameURL
|
|
|
|
: undefined;
|
2019-07-21 08:48:44 -06:00
|
|
|
|
|
|
|
// Popup details.
|
|
|
|
tabContext = µb.tabContextManager.lookup(targetTabId);
|
|
|
|
if ( tabContext === null ) { return; }
|
|
|
|
let targetURL = tabContext.rawURL;
|
|
|
|
if ( targetURL === '' ) { return; }
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/341
|
2019-09-23 06:25:23 -06:00
|
|
|
// Allow popups if uBlock is turned off in opener's context.
|
|
|
|
if ( µb.getNetFilteringSwitch(rootOpenerURL) === false ) { return; }
|
2019-07-21 08:48:44 -06:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1538
|
|
|
|
if (
|
2019-09-23 06:25:23 -06:00
|
|
|
µb.getNetFilteringSwitch(
|
2021-07-25 08:55:35 -06:00
|
|
|
µb.normalizeTabURL(openerTabId, rootOpenerURL)
|
2019-07-21 08:48:44 -06:00
|
|
|
) === false
|
|
|
|
) {
|
2015-04-08 16:46:08 -06:00
|
|
|
return;
|
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
|
2019-09-23 06:25:23 -06:00
|
|
|
// If the page URL is that of our "blocked page" URL, extract the URL
|
|
|
|
// of the page which was blocked.
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( targetURL.startsWith(vAPI.getURL('document-blocked.html')) ) {
|
|
|
|
const matches = /details=([^&]+)/.exec(targetURL);
|
|
|
|
if ( matches !== null ) {
|
2019-11-04 11:12:40 -07:00
|
|
|
targetURL = JSON.parse(decodeURIComponent(matches[1])).url;
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2015-06-05 11:37:46 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
|
2019-09-22 07:19:57 -06:00
|
|
|
// MUST be reset before code below is called.
|
|
|
|
const fctxt = µb.filteringContext.duplicate();
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Popup test.
|
|
|
|
let popupType = 'popup',
|
|
|
|
result = 0;
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2919
|
|
|
|
// - If the target tab matches a clicked link, assume it's legit.
|
|
|
|
if ( areDifferentURLs(targetURL, openerDetails.trustedURL) ) {
|
2019-09-23 06:25:23 -06:00
|
|
|
result = popupMatch(fctxt, rootOpenerURL, localOpenerURL, targetURL);
|
2015-05-28 12:47:19 -06:00
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Popunder test.
|
|
|
|
if ( result === 0 && openerDetails.popunder ) {
|
2019-09-23 06:25:23 -06:00
|
|
|
result = popunderMatch(fctxt, rootOpenerURL, localOpenerURL, targetURL);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( result === 1 ) {
|
|
|
|
popupType = 'popunder';
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Log only for when there was a hit against an actual filter (allow or block).
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2776
|
2021-07-28 17:48:38 -06:00
|
|
|
if ( logger.enabled ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
fctxt.setRealm('network').setType(popupType);
|
|
|
|
if ( popupType === 'popup' ) {
|
|
|
|
fctxt.setURL(targetURL)
|
|
|
|
.setTabId(openerTabId)
|
2019-09-23 06:25:23 -06:00
|
|
|
.setTabOriginFromURL(rootOpenerURL)
|
2020-12-26 08:15:07 -07:00
|
|
|
.setDocOriginFromURL(localOpenerURL || rootOpenerURL);
|
2019-07-21 08:48:44 -06:00
|
|
|
} else {
|
2019-09-23 06:25:23 -06:00
|
|
|
fctxt.setURL(rootOpenerURL)
|
2019-07-21 08:48:44 -06:00
|
|
|
.setTabId(targetTabId)
|
|
|
|
.setTabOriginFromURL(targetURL)
|
|
|
|
.setDocOriginFromURL(targetURL);
|
|
|
|
}
|
|
|
|
fctxt.toLogger();
|
2015-04-08 16:46:08 -06:00
|
|
|
}
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Not blocked
|
2019-09-22 07:19:57 -06:00
|
|
|
if ( result !== 1 ) { return; }
|
2019-07-21 08:48:44 -06:00
|
|
|
|
|
|
|
// Only if a popup was blocked do we report it in the dynamic
|
|
|
|
// filtering pane.
|
|
|
|
const pageStore = µb.pageStoreFromTabId(openerTabId);
|
|
|
|
if ( pageStore ) {
|
2021-02-15 04:52:31 -07:00
|
|
|
pageStore.journalAddRequest(fctxt, result);
|
2019-07-21 08:48:44 -06:00
|
|
|
pageStore.popupBlockedCount += 1;
|
2015-04-29 08:29:23 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
|
|
|
|
// Blocked
|
|
|
|
if ( µb.userSettings.showIconBadge ) {
|
2019-08-10 08:57:24 -06:00
|
|
|
µb.updateToolbarIcon(openerTabId, 0b010);
|
2015-05-28 12:47:19 -06:00
|
|
|
}
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// It is a popup, block and remove the tab.
|
|
|
|
if ( popupType === 'popup' ) {
|
2020-12-12 06:19:40 -07:00
|
|
|
µb.unbindTabFromPageStore(targetTabId);
|
2019-07-21 08:48:44 -06:00
|
|
|
vAPI.tabs.remove(targetTabId, false);
|
|
|
|
} else {
|
2020-12-12 06:19:40 -07:00
|
|
|
µb.unbindTabFromPageStore(openerTabId);
|
2019-07-21 08:48:44 -06:00
|
|
|
vAPI.tabs.remove(openerTabId, true);
|
2015-04-08 16:46:08 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
|
2017-10-23 20:27:39 -06:00
|
|
|
return true;
|
2015-04-08 16:46:08 -06:00
|
|
|
};
|
2019-07-21 08:48:44 -06:00
|
|
|
})();
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************
|
2015-10-22 07:45:21 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
To keep track from which context *exactly* network requests are made. This is
|
|
|
|
often tricky for various reasons, and the challenge is not specific to one
|
|
|
|
browser.
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
The time at which a URL is assigned to a tab and the time when a network
|
|
|
|
request for a root document is made must be assumed to be unrelated: it's all
|
|
|
|
asynchronous. There is no guaranteed order in which the two events are fired.
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
Also, other "anomalies" can occur:
|
2015-12-01 22:59:51 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
- a network request for a root document is fired without the corresponding
|
|
|
|
tab being really assigned a new URL
|
|
|
|
<https://github.com/chrisaljoudi/uBlock/issues/516>
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
- a network request for a secondary resource is labeled with a tab id for
|
|
|
|
which no root document was pulled for that tab.
|
|
|
|
<https://github.com/chrisaljoudi/uBlock/issues/1001>
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
- a network request for a secondary resource is made without the root
|
|
|
|
document to which it belongs being formally bound yet to the proper tab id,
|
|
|
|
causing a bad scope to be used for filtering purpose.
|
|
|
|
<https://github.com/chrisaljoudi/uBlock/issues/1205>
|
|
|
|
<https://github.com/chrisaljoudi/uBlock/issues/1140>
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
So the solution here is to keep a lightweight data structure which only
|
|
|
|
purpose is to keep track as accurately as possible of which root document
|
|
|
|
belongs to which tab. That's the only purpose, and because of this, there are
|
|
|
|
no restrictions for when the URL of a root document can be associated to a tab.
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
Before, the PageStore object was trying to deal with this, but it had to
|
|
|
|
enforce some restrictions so as to not descend into one of the above issues, or
|
|
|
|
other issues. The PageStore object can only be associated with a tab for which
|
|
|
|
a definitive navigation event occurred, because it collects information about
|
|
|
|
what occurred in the tab (for example, the number of requests blocked for a
|
|
|
|
page).
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
The TabContext objects do not suffer this restriction, and as a result they
|
|
|
|
offer the most reliable picture of which root document URL is really associated
|
|
|
|
to which tab. Moreover, the TabObject can undo an association from a root
|
|
|
|
document, and automatically re-associate with the next most recent. This takes
|
|
|
|
care of <https://github.com/chrisaljoudi/uBlock/issues/516>.
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
The PageStore object no longer cache the various information about which
|
|
|
|
root document it is currently bound. When it needs to find out, it will always
|
|
|
|
defer to the TabContext object, which will provide the real answer. This takes
|
|
|
|
case of <https://github.com/chrisaljoudi/uBlock/issues/1205>. In effect, the
|
|
|
|
master switch and dynamic filtering rules can be evaluated now properly even
|
|
|
|
in the absence of a PageStore object, this was not the case before.
|
2015-04-08 16:46:08 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
Also, the TabContext object will try its best to find a good candidate root
|
|
|
|
document URL for when none exists. This takes care of
|
|
|
|
<https://github.com/chrisaljoudi/uBlock/issues/1001>.
|
2015-01-02 11:42:35 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
The TabContext manager is self-contained, and it takes care to properly
|
|
|
|
housekeep itself.
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
*/
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.tabContextManager = (( ) => {
|
2019-07-21 08:48:44 -06:00
|
|
|
const tabContexts = new Map();
|
|
|
|
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1001
|
|
|
|
// This is to be used as last-resort fallback in case a tab is found to not
|
|
|
|
// be bound while network requests are fired for the tab.
|
|
|
|
let mostRecentRootDocURL = '';
|
|
|
|
let mostRecentRootDocURLTimestamp = 0;
|
|
|
|
|
|
|
|
const popupCandidates = new Map();
|
|
|
|
|
|
|
|
const PopupCandidate = class {
|
2019-09-23 06:25:23 -06:00
|
|
|
constructor(createDetails, openerDetails) {
|
|
|
|
this.targetTabId = createDetails.tabId;
|
2019-07-21 08:48:44 -06:00
|
|
|
this.opener = {
|
2019-09-23 06:25:23 -06:00
|
|
|
tabId: createDetails.sourceTabId,
|
|
|
|
tabURL: openerDetails[0].url,
|
|
|
|
frameId: createDetails.sourceFrameId,
|
|
|
|
frameURL: openerDetails[1].url,
|
2019-07-21 08:48:44 -06:00
|
|
|
popunder: false,
|
2019-11-04 11:34:44 -07:00
|
|
|
trustedURL: createDetails.sourceTabId === µb.maybeGoodPopup.tabId
|
2019-09-18 10:17:45 -06:00
|
|
|
? µb.maybeGoodPopup.url
|
2019-07-21 08:48:44 -06:00
|
|
|
: ''
|
|
|
|
};
|
|
|
|
this.selfDestructionTimer = null;
|
|
|
|
this.launchSelfDestruction();
|
2018-05-17 05:33:21 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if ( this.selfDestructionTimer !== null ) {
|
|
|
|
clearTimeout(this.selfDestructionTimer);
|
|
|
|
}
|
|
|
|
popupCandidates.delete(this.targetTabId);
|
2018-05-22 07:26:49 -06:00
|
|
|
}
|
2014-09-08 11:45:03 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
launchSelfDestruction() {
|
|
|
|
if ( this.selfDestructionTimer !== null ) {
|
|
|
|
clearTimeout(this.selfDestructionTimer);
|
|
|
|
}
|
|
|
|
this.selfDestructionTimer = vAPI.setTimeout(
|
|
|
|
( ) => this.destroy(),
|
|
|
|
10000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2019-09-23 06:25:23 -06:00
|
|
|
const popupCandidateTest = async function(targetTabId) {
|
|
|
|
for ( const [ tabId, candidate ] of popupCandidates ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
if (
|
|
|
|
targetTabId !== tabId &&
|
|
|
|
targetTabId !== candidate.opener.tabId
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/3129
|
|
|
|
// If the trigger is a change in the opener's URL, mark the entry
|
|
|
|
// as candidate for popunder filtering.
|
|
|
|
if ( targetTabId === candidate.opener.tabId ) {
|
|
|
|
candidate.opener.popunder = true;
|
|
|
|
}
|
2021-07-28 17:48:38 -06:00
|
|
|
const result = onPopupUpdated(tabId, candidate.opener);
|
2019-09-23 06:25:23 -06:00
|
|
|
if ( result === true ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
candidate.destroy();
|
|
|
|
} else {
|
|
|
|
candidate.launchSelfDestruction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2020-07-29 05:13:08 -06:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1184
|
|
|
|
// Do not consider a tab opened from `about:newtab` to be a popup
|
|
|
|
// candidate.
|
|
|
|
|
2019-09-23 06:25:23 -06:00
|
|
|
const onTabCreated = async function(createDetails) {
|
|
|
|
const { sourceTabId, sourceFrameId, tabId } = createDetails;
|
|
|
|
const popup = popupCandidates.get(tabId);
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( popup === undefined ) {
|
2019-09-23 06:25:23 -06:00
|
|
|
let openerDetails;
|
|
|
|
try {
|
|
|
|
openerDetails = await Promise.all([
|
|
|
|
webext.webNavigation.getFrame({
|
|
|
|
tabId: createDetails.sourceTabId,
|
|
|
|
frameId: 0,
|
|
|
|
}),
|
|
|
|
webext.webNavigation.getFrame({
|
|
|
|
tabId: sourceTabId,
|
|
|
|
frameId: sourceFrameId,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
catch (reason) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-29 05:13:08 -06:00
|
|
|
if (
|
|
|
|
Array.isArray(openerDetails) === false ||
|
|
|
|
openerDetails.length !== 2 ||
|
2021-07-15 09:34:37 -06:00
|
|
|
openerDetails[1] === null ||
|
2020-07-29 05:13:08 -06:00
|
|
|
openerDetails[1].url === 'about:newtab'
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
popupCandidates.set(
|
2019-09-23 06:25:23 -06:00
|
|
|
tabId,
|
|
|
|
new PopupCandidate(createDetails, openerDetails)
|
2019-07-21 08:48:44 -06:00
|
|
|
);
|
|
|
|
}
|
2019-09-23 06:25:23 -06:00
|
|
|
popupCandidateTest(tabId);
|
2019-07-21 08:48:44 -06:00
|
|
|
};
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
const gcPeriod = 10 * 60 * 1000;
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// A pushed entry is removed from the stack unless it is committed with
|
|
|
|
// a set time.
|
|
|
|
const StackEntry = function(url, commit) {
|
|
|
|
this.url = url;
|
|
|
|
this.committed = commit;
|
|
|
|
this.tstamp = Date.now();
|
|
|
|
};
|
2014-10-15 11:14:25 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
const TabContext = function(tabId) {
|
|
|
|
this.tabId = tabId;
|
|
|
|
this.stack = [];
|
|
|
|
this.rawURL =
|
|
|
|
this.normalURL =
|
|
|
|
this.origin =
|
|
|
|
this.rootHostname =
|
|
|
|
this.rootDomain = '';
|
|
|
|
this.commitTimer = null;
|
|
|
|
this.gcTimer = null;
|
|
|
|
this.onGCBarrier = false;
|
|
|
|
this.netFiltering = true;
|
|
|
|
this.netFilteringReadTime = 0;
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
tabContexts.set(tabId, this);
|
|
|
|
};
|
2015-11-30 16:50:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
TabContext.prototype.destroy = function() {
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) { return; }
|
|
|
|
if ( this.gcTimer !== null ) {
|
|
|
|
clearTimeout(this.gcTimer);
|
|
|
|
this.gcTimer = null;
|
|
|
|
}
|
|
|
|
tabContexts.delete(this.tabId);
|
|
|
|
};
|
2015-11-30 16:50:22 -07:00
|
|
|
|
2019-09-16 07:45:17 -06:00
|
|
|
TabContext.prototype.onGC = async function() {
|
2019-07-21 08:48:44 -06:00
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) { return; }
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1713
|
2020-07-19 15:41:13 -06:00
|
|
|
// For unknown reasons, Firefox's setTimeout() will sometimes
|
|
|
|
// causes the callback function to be called immediately, bypassing
|
|
|
|
// the main event loop. For now this should prevent uBO from
|
|
|
|
// crashing as a result of the bad setTimeout() behavior.
|
2019-09-16 07:45:17 -06:00
|
|
|
if ( this.onGCBarrier ) { return; }
|
2019-07-21 08:48:44 -06:00
|
|
|
this.onGCBarrier = true;
|
|
|
|
this.gcTimer = null;
|
2019-09-16 07:45:17 -06:00
|
|
|
const tab = await vAPI.tabs.get(this.tabId);
|
2020-07-19 15:41:13 -06:00
|
|
|
if ( tab instanceof Object === false || tab.discarded === true ) {
|
|
|
|
this.destroy();
|
|
|
|
} else {
|
|
|
|
this.gcTimer = vAPI.setTimeout(( ) => this.onGC(), gcPeriod);
|
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
this.onGCBarrier = false;
|
|
|
|
};
|
2015-11-30 16:50:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/248
|
|
|
|
// Stack entries have to be committed to stick. Non-committed stack
|
|
|
|
// entries are removed after a set delay.
|
|
|
|
TabContext.prototype.onCommit = function() {
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.commitTimer = null;
|
|
|
|
// Remove uncommitted entries at the top of the stack.
|
|
|
|
let i = this.stack.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
if ( this.stack[i].committed ) { break; }
|
|
|
|
}
|
|
|
|
// https://github.com/gorhill/uBlock/issues/300
|
|
|
|
// If no committed entry was found, fall back on the bottom-most one
|
|
|
|
// as being the committed one by default.
|
|
|
|
if ( i === -1 && this.stack.length !== 0 ) {
|
|
|
|
this.stack[0].committed = true;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
if ( i < this.stack.length ) {
|
|
|
|
this.stack.length = i;
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
};
|
2015-12-13 11:46:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// This takes care of orphanized tab contexts. Can't be started for all
|
|
|
|
// contexts, as the behind-the-scene context is permanent -- so we do not
|
|
|
|
// want to flush it.
|
|
|
|
TabContext.prototype.autodestroy = function() {
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) { return; }
|
|
|
|
this.gcTimer = vAPI.setTimeout(( ) => this.onGC(), gcPeriod);
|
|
|
|
};
|
2015-06-14 05:46:58 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Update just force all properties to be updated to match the most recent
|
|
|
|
// root URL.
|
|
|
|
TabContext.prototype.update = function() {
|
|
|
|
this.netFilteringReadTime = 0;
|
|
|
|
if ( this.stack.length === 0 ) {
|
|
|
|
this.rawURL =
|
|
|
|
this.normalURL =
|
|
|
|
this.origin =
|
|
|
|
this.rootHostname =
|
|
|
|
this.rootDomain = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const stackEntry = this.stack[this.stack.length - 1];
|
|
|
|
this.rawURL = stackEntry.url;
|
2021-07-25 08:55:35 -06:00
|
|
|
this.normalURL = µb.normalizeTabURL(this.tabId, this.rawURL);
|
|
|
|
this.origin = originFromURI(this.normalURL);
|
|
|
|
this.rootHostname = hostnameFromURI(this.origin);
|
2019-07-21 08:48:44 -06:00
|
|
|
this.rootDomain =
|
2021-07-25 08:55:35 -06:00
|
|
|
domainFromHostname(this.rootHostname) ||
|
2019-07-21 08:48:44 -06:00
|
|
|
this.rootHostname;
|
|
|
|
};
|
2015-03-08 09:06:36 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Called whenever a candidate root URL is spotted for the tab.
|
|
|
|
TabContext.prototype.push = function(url) {
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
|
|
|
|
return;
|
2015-11-30 16:50:22 -07:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
const count = this.stack.length;
|
|
|
|
if ( count !== 0 && this.stack[count - 1].url === url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.stack.push(new StackEntry(url));
|
|
|
|
this.update();
|
|
|
|
popupCandidateTest(this.tabId);
|
|
|
|
if ( this.commitTimer !== null ) {
|
|
|
|
clearTimeout(this.commitTimer);
|
|
|
|
}
|
|
|
|
this.commitTimer = vAPI.setTimeout(( ) => this.onCommit(), 500);
|
|
|
|
};
|
2015-03-27 11:00:55 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// This tells that the url is definitely the one to be associated with the
|
|
|
|
// tab, there is no longer any ambiguity about which root URL is really
|
|
|
|
// sitting in which tab.
|
|
|
|
TabContext.prototype.commit = function(url) {
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) { return; }
|
|
|
|
if ( this.stack.length !== 0 ) {
|
|
|
|
const top = this.stack[this.stack.length - 1];
|
|
|
|
if ( top.url === url && top.committed ) { return false; }
|
|
|
|
}
|
|
|
|
this.stack = [new StackEntry(url, true)];
|
|
|
|
this.update();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
TabContext.prototype.getNetFilteringSwitch = function() {
|
|
|
|
if ( this.netFilteringReadTime > µb.netWhitelistModifyTime ) {
|
|
|
|
return this.netFiltering;
|
|
|
|
}
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1078
|
|
|
|
// Use both the raw and normalized URLs.
|
|
|
|
this.netFiltering = µb.getNetFilteringSwitch(this.normalURL);
|
|
|
|
if (
|
|
|
|
this.netFiltering &&
|
|
|
|
this.rawURL !== this.normalURL &&
|
|
|
|
this.rawURL !== ''
|
|
|
|
) {
|
|
|
|
this.netFiltering = µb.getNetFilteringSwitch(this.rawURL);
|
2015-03-27 11:00:55 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
this.netFilteringReadTime = Date.now();
|
|
|
|
return this.netFiltering;
|
|
|
|
};
|
2015-03-27 11:00:55 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// These are to be used for the API of the tab context manager.
|
|
|
|
|
|
|
|
const push = function(tabId, url) {
|
|
|
|
let entry = tabContexts.get(tabId);
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = new TabContext(tabId);
|
|
|
|
entry.autodestroy();
|
|
|
|
}
|
|
|
|
entry.push(url);
|
|
|
|
mostRecentRootDocURL = url;
|
|
|
|
mostRecentRootDocURLTimestamp = Date.now();
|
|
|
|
return entry;
|
2015-03-27 12:59:17 -06:00
|
|
|
};
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Find a tab context for a specific tab.
|
|
|
|
const lookup = function(tabId) {
|
|
|
|
return tabContexts.get(tabId) || null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Find a tab context for a specific tab. If none is found, attempt to
|
|
|
|
// fix this. When all fail, the behind-the-scene context is returned.
|
|
|
|
const mustLookup = function(tabId) {
|
|
|
|
const entry = tabContexts.get(tabId);
|
|
|
|
if ( entry !== undefined ) {
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1025
|
|
|
|
// Google Hangout popup opens without a root frame. So for now we will
|
|
|
|
// just discard that best-guess root frame if it is too far in the
|
|
|
|
// future, at which point it ceases to be a "best guess".
|
2017-05-12 08:35:11 -06:00
|
|
|
if (
|
2019-07-21 08:48:44 -06:00
|
|
|
mostRecentRootDocURL !== '' &&
|
|
|
|
mostRecentRootDocURLTimestamp + 500 < Date.now()
|
2017-05-12 08:35:11 -06:00
|
|
|
) {
|
2019-07-21 08:48:44 -06:00
|
|
|
mostRecentRootDocURL = '';
|
2016-02-26 07:48:43 -07:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1001
|
|
|
|
// Not a behind-the-scene request, yet no page store found for the
|
|
|
|
// tab id: we will thus bind the last-seen root document to the
|
|
|
|
// unbound tab. It's a guess, but better than ending up filtering
|
|
|
|
// nothing at all.
|
|
|
|
if ( mostRecentRootDocURL !== '' ) {
|
|
|
|
return push(tabId, mostRecentRootDocURL);
|
2017-05-19 06:45:19 -06:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
// If all else fail at finding a page store, re-categorize the
|
|
|
|
// request as behind-the-scene. At least this ensures that ultimately
|
|
|
|
// the user can still inspect/filter those net requests which were
|
|
|
|
// about to fall through the cracks.
|
|
|
|
// Example: Chromium + case #12 at
|
|
|
|
// http://raymondhill.net/ublock/popup.html
|
|
|
|
return tabContexts.get(vAPI.noTabId);
|
2016-04-28 09:28:08 -06:00
|
|
|
};
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1735
|
|
|
|
// Filter for popups if actually committing.
|
|
|
|
const commit = function(tabId, url) {
|
|
|
|
let entry = tabContexts.get(tabId);
|
|
|
|
if ( entry === undefined ) {
|
|
|
|
entry = push(tabId, url);
|
|
|
|
} else if ( entry.commit(url) ) {
|
|
|
|
popupCandidateTest(tabId);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
2018-12-13 10:30:54 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
const exists = function(tabId) {
|
|
|
|
return tabContexts.get(tabId) !== undefined;
|
|
|
|
};
|
2018-12-13 10:30:54 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Behind-the-scene tab context
|
|
|
|
{
|
|
|
|
const entry = new TabContext(vAPI.noTabId);
|
|
|
|
entry.stack.push(new StackEntry('', true));
|
|
|
|
entry.rawURL = '';
|
2021-07-25 08:55:35 -06:00
|
|
|
entry.normalURL = µb.normalizeTabURL(entry.tabId);
|
|
|
|
entry.origin = originFromURI(entry.normalURL);
|
|
|
|
entry.rootHostname = hostnameFromURI(entry.origin);
|
|
|
|
entry.rootDomain = domainFromHostname(entry.rootHostname);
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2018-12-13 10:30:54 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// Context object, typically to be used to feed filtering engines.
|
|
|
|
const contextJunkyard = [];
|
|
|
|
const Context = class {
|
|
|
|
constructor(tabId) {
|
|
|
|
this.init(tabId);
|
|
|
|
}
|
|
|
|
init(tabId) {
|
|
|
|
const tabContext = lookup(tabId);
|
|
|
|
this.rootHostname = tabContext.rootHostname;
|
|
|
|
this.rootDomain = tabContext.rootDomain;
|
|
|
|
this.pageHostname =
|
|
|
|
this.pageDomain =
|
|
|
|
this.requestURL =
|
|
|
|
this.origin =
|
|
|
|
this.requestHostname =
|
|
|
|
this.requestDomain = '';
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
dispose() {
|
|
|
|
contextJunkyard.push(this);
|
|
|
|
}
|
2016-02-26 07:48:43 -07:00
|
|
|
};
|
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
const createContext = function(tabId) {
|
|
|
|
if ( contextJunkyard.length ) {
|
|
|
|
return contextJunkyard.pop().init(tabId);
|
|
|
|
}
|
|
|
|
return new Context(tabId);
|
|
|
|
};
|
2015-12-01 13:07:22 -07:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
return {
|
|
|
|
push,
|
|
|
|
commit,
|
|
|
|
lookup,
|
|
|
|
mustLookup,
|
|
|
|
exists,
|
|
|
|
createContext,
|
|
|
|
onTabCreated,
|
|
|
|
};
|
|
|
|
})();
|
2015-03-27 11:00:55 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
2015-08-17 15:10:15 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
vAPI.Tabs = class extends vAPI.Tabs {
|
|
|
|
onActivated(details) {
|
2021-10-18 07:43:41 -06:00
|
|
|
const { tabId } = details;
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/757
|
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore === null ) {
|
|
|
|
this.onNewTab(tabId);
|
|
|
|
return;
|
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
super.onActivated(details);
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/680
|
2021-10-18 07:43:41 -06:00
|
|
|
µb.updateToolbarIcon(tabId);
|
|
|
|
contextMenu.update(tabId);
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2016-04-05 07:59:23 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
onClosed(tabId) {
|
|
|
|
super.onClosed(tabId);
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.unbindTabFromPageStore(tabId);
|
|
|
|
contextMenu.update();
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2015-08-17 15:10:15 -06:00
|
|
|
|
2019-09-23 06:25:23 -06:00
|
|
|
onCreated(details) {
|
|
|
|
super.onCreated(details);
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.tabContextManager.onTabCreated(details);
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2014-10-15 11:14:25 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
// When the DOM content of root frame is loaded, this means the tab
|
|
|
|
// content has changed.
|
|
|
|
//
|
|
|
|
// The webRequest.onBeforeRequest() won't be called for everything
|
|
|
|
// else than http/https. Thus, in such case, we will bind the tab as
|
|
|
|
// early as possible in order to increase the likelihood of a context
|
|
|
|
// properly setup if network requests are fired from within the tab.
|
|
|
|
// Example: Chromium + case #6 at
|
|
|
|
// http://raymondhill.net/ublock/popup.html
|
2020-12-18 10:07:08 -07:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/688#issuecomment-748179731
|
|
|
|
// For non-network URIs, defer scriptlet injection to content script. The
|
|
|
|
// reason for this is that we need the effective URL and this information
|
|
|
|
// is not available at this point.
|
2019-07-21 08:48:44 -06:00
|
|
|
onNavigation(details) {
|
|
|
|
super.onNavigation(details);
|
2021-01-10 09:56:27 -07:00
|
|
|
const { frameId, tabId, url } = details;
|
|
|
|
if ( frameId === 0 ) {
|
|
|
|
µb.tabContextManager.commit(tabId, url);
|
2021-07-16 06:59:30 -06:00
|
|
|
const pageStore = µb.bindTabToPageStore(tabId, 'tabCommitted', details);
|
2021-01-10 09:56:27 -07:00
|
|
|
if ( pageStore !== null ) {
|
|
|
|
pageStore.journalAddRootFrame('committed', url);
|
2016-02-26 07:48:43 -07:00
|
|
|
}
|
2015-11-30 16:50:22 -07:00
|
|
|
}
|
2021-01-10 09:56:27 -07:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore === null ) { return; }
|
|
|
|
pageStore.setFrameURL(details);
|
2021-08-26 16:36:18 -06:00
|
|
|
if (
|
|
|
|
µb.canInjectScriptletsNow &&
|
|
|
|
isNetworkURI(url) &&
|
|
|
|
pageStore.getNetFilteringSwitch()
|
|
|
|
) {
|
2021-07-28 17:48:38 -06:00
|
|
|
scriptletFilteringEngine.injectNow(details);
|
2015-11-30 16:50:22 -07:00
|
|
|
}
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
2014-12-01 12:45:00 -07:00
|
|
|
|
2021-10-18 07:43:41 -06:00
|
|
|
async onNewTab(tabId) {
|
|
|
|
const tab = await vAPI.tabs.get(tabId);
|
|
|
|
if ( tab === null ) { return; }
|
|
|
|
const { id, url = '' } = tab;
|
|
|
|
if ( url === '' ) { return; }
|
|
|
|
µb.tabContextManager.commit(id, url);
|
|
|
|
µb.bindTabToPageStore(id, 'tabUpdated', tab);
|
|
|
|
contextMenu.update(id);
|
|
|
|
}
|
|
|
|
|
2019-07-21 08:48:44 -06: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.
|
|
|
|
onUpdated(tabId, changeInfo, tab) {
|
|
|
|
super.onUpdated(tabId, changeInfo, tab);
|
|
|
|
if ( !tab.url || tab.url === '' ) { return; }
|
|
|
|
if ( !changeInfo.url ) { return; }
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.tabContextManager.commit(tabId, changeInfo.url);
|
|
|
|
µb.bindTabToPageStore(tabId, 'tabUpdated', tab);
|
2019-07-21 08:48:44 -06:00
|
|
|
}
|
|
|
|
};
|
2014-10-17 13:44:19 -06:00
|
|
|
|
2019-07-21 08:48:44 -06:00
|
|
|
vAPI.tabs = new vAPI.Tabs();
|
2014-10-15 11:14:25 -06:00
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Create an entry for the tab if it doesn't exist.
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.bindTabToPageStore = function(tabId, context, details = undefined) {
|
2019-08-10 08:57:24 -06:00
|
|
|
this.updateToolbarIcon(tabId, 0b111);
|
2014-07-06 17:14:32 -06:00
|
|
|
|
2014-07-14 09:24:59 -06:00
|
|
|
// Do not create a page store for URLs which are of no interests
|
2019-07-05 15:44:08 -06:00
|
|
|
if ( this.tabContextManager.exists(tabId) === false ) {
|
2020-12-12 06:19:40 -07:00
|
|
|
this.unbindTabFromPageStore(tabId);
|
2014-06-23 16:42:43 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-17 05:19:48 -07:00
|
|
|
// Reuse page store if one exists: this allows to guess if a tab is a popup
|
2019-07-05 15:44:08 -06:00
|
|
|
let pageStore = this.pageStores.get(tabId);
|
2014-08-02 09:40:27 -06:00
|
|
|
|
2015-01-17 05:19:48 -07:00
|
|
|
// Tab is not bound
|
2018-02-26 11:59:16 -07:00
|
|
|
if ( pageStore === undefined ) {
|
2021-07-28 17:48:38 -06:00
|
|
|
pageStore = PageStore.factory(tabId, details);
|
2018-02-26 11:59:16 -07:00
|
|
|
this.pageStores.set(tabId, pageStore);
|
2018-05-27 06:31:17 -06:00
|
|
|
this.pageStoresToken = Date.now();
|
2018-02-26 11:59:16 -07:00
|
|
|
return pageStore;
|
2015-01-17 05:19:48 -07:00
|
|
|
}
|
|
|
|
|
2015-04-06 19:26:05 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/516
|
2016-10-08 08:15:31 -06:00
|
|
|
// Never rebind behind-the-scene scope.
|
2015-04-05 10:03:14 -06:00
|
|
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
|
|
|
|
return pageStore;
|
|
|
|
}
|
|
|
|
|
2016-10-08 08:15:31 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/516
|
|
|
|
// If context is 'beforeRequest', do not rebind, wait for confirmation.
|
2015-01-17 05:19:48 -07:00
|
|
|
if ( context === 'beforeRequest' ) {
|
2019-09-21 14:42:15 -06:00
|
|
|
pageStore.netFilteringCache.empty();
|
2015-01-17 05:19:48 -07:00
|
|
|
return pageStore;
|
|
|
|
}
|
|
|
|
|
2015-01-24 10:34:36 -07:00
|
|
|
// Rebind according to context. We rebind even if the URL did not change,
|
|
|
|
// as maybe the tab was force-reloaded, in which case the page stats must
|
|
|
|
// be all reset.
|
2021-07-16 06:59:30 -06:00
|
|
|
pageStore.reuse(context, details);
|
2014-06-23 16:42:43 -06:00
|
|
|
|
2015-05-18 06:12:35 -06:00
|
|
|
this.pageStoresToken = Date.now();
|
|
|
|
|
2014-06-23 16:42:43 -06:00
|
|
|
return pageStore;
|
|
|
|
};
|
|
|
|
|
2015-04-08 16:46:08 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.unbindTabFromPageStore = function(tabId) {
|
2019-07-05 15:44:08 -06:00
|
|
|
const pageStore = this.pageStores.get(tabId);
|
|
|
|
if ( pageStore === undefined ) { return; }
|
|
|
|
pageStore.dispose();
|
|
|
|
this.pageStores.delete(tabId);
|
|
|
|
this.pageStoresToken = Date.now();
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.pageStoreFromTabId = function(tabId) {
|
2018-02-26 11:59:16 -07:00
|
|
|
return this.pageStores.get(tabId) || null;
|
2014-06-23 16:42:43 -06:00
|
|
|
};
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.mustPageStoreFromTabId = function(tabId) {
|
2018-02-26 11:59:16 -07:00
|
|
|
return this.pageStores.get(tabId) || this.pageStores.get(vAPI.noTabId);
|
2016-03-22 08:19:41 -06:00
|
|
|
};
|
|
|
|
|
2015-01-19 16:42:58 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Permanent page store for behind-the-scene requests. Must never be removed.
|
2019-07-05 15:44:08 -06:00
|
|
|
//
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/651
|
|
|
|
// The whitelist status of the tabless page store will be determined by
|
|
|
|
// the document context (if present) of the network request.
|
|
|
|
|
|
|
|
{
|
2021-07-28 17:48:38 -06:00
|
|
|
const NoPageStore = class extends PageStore {
|
2019-07-05 15:44:08 -06:00
|
|
|
getNetFilteringSwitch(fctxt) {
|
2021-07-12 09:26:25 -06:00
|
|
|
if ( fctxt ) {
|
2019-07-05 15:44:08 -06:00
|
|
|
const docOrigin = fctxt.getDocOrigin();
|
|
|
|
if ( docOrigin ) {
|
2021-07-28 17:48:38 -06:00
|
|
|
return µb.getNetFilteringSwitch(docOrigin);
|
2019-07-05 15:44:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getNetFilteringSwitch();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const pageStore = new NoPageStore(vAPI.noTabId);
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.pageStores.set(pageStore.tabId, pageStore);
|
2018-02-26 11:59:16 -07:00
|
|
|
pageStore.title = vAPI.i18n('logBehindTheScene');
|
2019-07-05 15:44:08 -06:00
|
|
|
}
|
2015-01-19 16:42:58 -07:00
|
|
|
|
2015-01-02 11:42:35 -07:00
|
|
|
/******************************************************************************/
|
2015-05-16 08:15:02 -06:00
|
|
|
|
2015-11-29 15:06:58 -07:00
|
|
|
// Update visual of extension icon.
|
|
|
|
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.updateToolbarIcon = (( ) => {
|
2019-07-05 15:44:08 -06:00
|
|
|
const tabIdToDetails = new Map();
|
2015-11-29 15:06:58 -07:00
|
|
|
|
2019-08-19 07:00:53 -06:00
|
|
|
const computeBadgeColor = (bits) => {
|
|
|
|
let color = µb.blockingProfileColorCache.get(bits);
|
|
|
|
if ( color !== undefined ) { return color; }
|
|
|
|
let max = 0;
|
|
|
|
for ( const profile of µb.liveBlockingProfiles ) {
|
|
|
|
const v = bits & (profile.bits & ~1);
|
|
|
|
if ( v < max ) { break; }
|
|
|
|
color = profile.color;
|
|
|
|
max = v;
|
|
|
|
}
|
|
|
|
if ( color === undefined ) {
|
|
|
|
color = '#666';
|
|
|
|
}
|
|
|
|
µb.blockingProfileColorCache.set(bits, color);
|
|
|
|
return color;
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateBadge = (tabId) => {
|
2019-09-12 10:26:09 -06:00
|
|
|
let parts = tabIdToDetails.get(tabId);
|
2018-05-08 07:43:25 -06:00
|
|
|
tabIdToDetails.delete(tabId);
|
2015-11-29 15:06:58 -07:00
|
|
|
|
2018-05-08 07:43:25 -06:00
|
|
|
let state = 0;
|
|
|
|
let badge = '';
|
2019-08-11 11:55:39 -06:00
|
|
|
let color = '#666';
|
2015-11-29 15:06:58 -07:00
|
|
|
|
2021-02-15 04:52:31 -07:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
2015-11-29 15:06:58 -07:00
|
|
|
if ( pageStore !== null ) {
|
2018-05-08 07:43:25 -06:00
|
|
|
state = pageStore.getNetFilteringSwitch() ? 1 : 0;
|
2019-09-12 10:26:09 -06:00
|
|
|
if ( state === 1 ) {
|
2021-02-15 04:52:31 -07:00
|
|
|
if ( (parts & 0b0010) !== 0 ) {
|
|
|
|
const blockCount = pageStore.counts.blocked.any;
|
|
|
|
if ( blockCount !== 0 ) {
|
|
|
|
badge = µb.formatCount(blockCount);
|
|
|
|
}
|
2019-08-10 08:57:24 -06:00
|
|
|
}
|
2019-09-12 10:26:09 -06:00
|
|
|
if ( (parts & 0b0100) !== 0 ) {
|
2019-08-19 07:00:53 -06:00
|
|
|
color = computeBadgeColor(
|
|
|
|
µb.blockingModeFromHostname(pageStore.tabHostname)
|
|
|
|
);
|
2019-08-10 08:57:24 -06:00
|
|
|
}
|
2015-11-29 15:06:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:26:09 -06:00
|
|
|
// https://www.reddit.com/r/uBlockOrigin/comments/d33d37/
|
|
|
|
if ( µb.userSettings.showIconBadge === false ) {
|
|
|
|
parts |= 0b1000;
|
|
|
|
}
|
|
|
|
|
2019-08-10 08:57:24 -06:00
|
|
|
vAPI.setIcon(tabId, { parts, state, badge, color });
|
2015-11-29 15:06:58 -07:00
|
|
|
};
|
|
|
|
|
2018-05-08 07:43:25 -06:00
|
|
|
// parts: bit 0 = icon
|
2019-09-12 10:26:09 -06:00
|
|
|
// bit 1 = badge text
|
2019-08-10 08:57:24 -06:00
|
|
|
// bit 2 = badge color
|
2019-09-12 10:26:09 -06:00
|
|
|
// bit 3 = hide badge
|
2018-05-08 07:43:25 -06:00
|
|
|
|
2019-09-12 10:26:09 -06:00
|
|
|
return function(tabId, newParts = 0b0111) {
|
2019-08-10 08:57:24 -06:00
|
|
|
if ( typeof tabId !== 'number' ) { return; }
|
2018-02-26 11:59:16 -07:00
|
|
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
|
2021-02-15 04:52:31 -07:00
|
|
|
const currentParts = tabIdToDetails.get(tabId);
|
2018-05-08 07:43:25 -06:00
|
|
|
if ( currentParts === newParts ) { return; }
|
|
|
|
if ( currentParts === undefined ) {
|
2019-07-21 08:48:44 -06:00
|
|
|
self.requestIdleCallback(
|
|
|
|
( ) => updateBadge(tabId),
|
|
|
|
{ timeout: 701 }
|
|
|
|
);
|
2018-05-08 07:43:25 -06:00
|
|
|
} else {
|
|
|
|
newParts |= currentParts;
|
|
|
|
}
|
|
|
|
tabIdToDetails.set(tabId, newParts);
|
2015-11-29 15:06:58 -07:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-06 19:26:05 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/455
|
2019-07-05 15:44:08 -06:00
|
|
|
// Stale page store entries janitor
|
|
|
|
|
|
|
|
{
|
|
|
|
const pageStoreJanitorPeriod = 15 * 60 * 1000;
|
|
|
|
let pageStoreJanitorSampleAt = 0;
|
|
|
|
let pageStoreJanitorSampleSize = 10;
|
|
|
|
|
2020-07-19 15:41:13 -06:00
|
|
|
const checkTab = async tabId => {
|
|
|
|
const tab = await vAPI.tabs.get(tabId);
|
|
|
|
if ( tab instanceof Object && tab.discarded !== true ) { return; }
|
2021-07-28 17:48:38 -06:00
|
|
|
µb.unbindTabFromPageStore(tabId);
|
2020-07-19 15:41:13 -06:00
|
|
|
};
|
|
|
|
|
2019-07-05 15:44:08 -06:00
|
|
|
const pageStoreJanitor = function() {
|
2021-07-28 17:48:38 -06:00
|
|
|
const tabIds = Array.from(µb.pageStores.keys()).sort();
|
2019-07-05 15:44:08 -06:00
|
|
|
if ( pageStoreJanitorSampleAt >= tabIds.length ) {
|
|
|
|
pageStoreJanitorSampleAt = 0;
|
|
|
|
}
|
|
|
|
const n = Math.min(
|
|
|
|
pageStoreJanitorSampleAt + pageStoreJanitorSampleSize,
|
|
|
|
tabIds.length
|
|
|
|
);
|
|
|
|
for ( let i = pageStoreJanitorSampleAt; i < n; i++ ) {
|
|
|
|
const tabId = tabIds[i];
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(tabId) ) { continue; }
|
|
|
|
checkTab(tabId);
|
|
|
|
}
|
|
|
|
pageStoreJanitorSampleAt = n;
|
2015-01-02 11:42:35 -07:00
|
|
|
|
2019-07-05 15:44:08 -06:00
|
|
|
vAPI.setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
|
2015-01-02 11:42:35 -07:00
|
|
|
};
|
|
|
|
|
2015-05-17 11:02:56 -06:00
|
|
|
vAPI.setTimeout(pageStoreJanitor, pageStoreJanitorPeriod);
|
2019-07-05 15:44:08 -06:00
|
|
|
}
|
2015-01-02 11:42:35 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|