2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-07-19 00:00:55 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +02: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
|
|
|
|
*/
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-11-16 14:09:28 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-23 15:42:27 +02:00
|
|
|
import publicSuffixList from '../lib/publicsuffixlist/publicsuffixlist.js';
|
2021-08-22 18:03:59 +02:00
|
|
|
import punycode from '../lib/punycode.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import cacheStorage from './cachestorage.js';
|
|
|
|
import cosmeticFilteringEngine from './cosmetic-filtering.js';
|
2021-11-11 18:49:55 +01:00
|
|
|
import htmlFilteringEngine from './html-filtering.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
import logger from './logger.js';
|
|
|
|
import lz4Codec from './lz4.js';
|
|
|
|
import io from './assets.js';
|
|
|
|
import scriptletFilteringEngine from './scriptlet-filtering.js';
|
|
|
|
import staticExtFilteringEngine from './static-ext-filtering.js';
|
|
|
|
import staticFilteringReverseLookup from './reverselookup.js';
|
|
|
|
import staticNetFilteringEngine from './static-net-filtering.js';
|
|
|
|
import µb from './background.js';
|
2021-10-07 20:41:29 +02:00
|
|
|
import webRequest from './traffic.js';
|
2021-08-03 20:03:00 +02:00
|
|
|
import { denseBase64 } from './base64-custom.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
import { redirectEngine } from './redirect-engine.js';
|
|
|
|
import { StaticFilteringParser } from './static-filtering-parser.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
permanentFirewall,
|
|
|
|
sessionFirewall,
|
|
|
|
permanentSwitches,
|
|
|
|
sessionSwitches,
|
2021-08-03 18:19:25 +02:00
|
|
|
permanentURLFiltering,
|
|
|
|
sessionURLFiltering,
|
|
|
|
} from './filtering-engines.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
domainFromHostname,
|
|
|
|
domainFromURI,
|
|
|
|
entityFromDomain,
|
|
|
|
hostnameFromURI,
|
|
|
|
isNetworkURI,
|
|
|
|
} from './uri-utils.js';
|
|
|
|
|
2021-07-29 14:44:15 +02:00
|
|
|
import './benchmarks.js';
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
/******************************************************************************/
|
2016-08-06 18:05:01 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/710
|
|
|
|
// Listeners have a name and a "privileged" status.
|
|
|
|
// The nameless default handler is always deemed "privileged".
|
|
|
|
// Messages from privileged ports must never relayed to listeners
|
|
|
|
// which are not privileged.
|
2014-11-16 15:49:55 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
/******************************************************************************/
|
2014-11-16 15:49:55 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Default handler
|
2022-03-12 19:50:46 +01:00
|
|
|
// privileged
|
2015-06-07 00:31:38 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
|
|
|
|
2020-10-09 19:50:54 +02:00
|
|
|
const clickToLoad = function(request, sender) {
|
2020-11-12 18:14:59 +01:00
|
|
|
const { tabId, frameId } = sender;
|
2020-10-09 19:50:54 +02:00
|
|
|
if ( tabId === undefined || frameId === undefined ) { return false; }
|
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore === null ) { return false; }
|
|
|
|
pageStore.clickToLoad(frameId, request.frameURL);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-05-18 20:20:05 +02:00
|
|
|
const getDomainNames = function(targets) {
|
2019-05-19 23:00:49 +02:00
|
|
|
return targets.map(target => {
|
|
|
|
if ( typeof target !== 'string' ) { return ''; }
|
|
|
|
return target.indexOf('/') !== -1
|
2021-07-25 16:55:35 +02:00
|
|
|
? domainFromURI(target) || ''
|
|
|
|
: domainFromHostname(target) || target;
|
2019-05-19 23:00:49 +02:00
|
|
|
});
|
2015-06-07 00:31:38 +02:00
|
|
|
};
|
|
|
|
|
2019-01-08 13:37:50 +01:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2014-11-16 15:49:55 +01:00
|
|
|
// Async
|
2014-10-17 21:44:19 +02:00
|
|
|
switch ( request.what ) {
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'getAssetContent':
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/417
|
2021-07-29 01:48:38 +02:00
|
|
|
io.get(request.url, {
|
2020-09-13 14:01:53 +02:00
|
|
|
dontCache: true,
|
|
|
|
needSourceURL: true,
|
|
|
|
}).then(result => {
|
2019-09-15 13:58:28 +02:00
|
|
|
callback(result);
|
|
|
|
});
|
2015-04-25 13:28:35 +02:00
|
|
|
return;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2015-06-13 17:21:55 +02:00
|
|
|
case 'listsFromNetFilter':
|
2021-07-29 01:48:38 +02:00
|
|
|
staticFilteringReverseLookup.fromNetFilter(
|
Expand bidi-trie usage in static network filtering engine
Related issues:
- https://github.com/uBlockOrigin/uBlock-issues/issues/761
- https://github.com/uBlockOrigin/uBlock-issues/issues/528
The previous bidi-trie code could only hold filters which
are plain pattern, i.e. no wildcard characters, and which
had no origin option (`domain=`), right and/or left anchor,
and no `csp=` option.
Example of filters that could be moved into a bidi-trie
data structure:
&ad_box_
/w/d/capu.php?z=$script,third-party
||liveonlinetv247.com/images/muvixx-150x50-watch-now-in-hd-play-btn.gif
Examples of filters that could NOT be moved to a bidi-trie:
-adap.$domain=~l-adap.org
/tsc.php?*&ses=
||ibsrv.net/*forumsponsor$domain=[...]
@@||imgspice.com/jquery.cookie.js|$script
||view.atdmt.com^*/iview/$third-party
||postimg.cc/image/$csp=[...]
Ideally the filters above should be able to be moved to a
bidi-trie since they are basically plain patterns, or at
least partially moved to a bidi-trie when there is only a
single wildcard (i.e. made of two plain patterns).
Also, there were two distinct bidi-tries in which
plain-pattern filters can be moved to: one for patterns
without hostname anchoring and another one for patterns
with hostname-anchoring. This was required because the
hostname-anchored patterns have an extra condition which
is outside the bidi-trie knowledge.
This commit expands the number of filters which can be
stored in the bidi-trie, and also remove the need to
use two distinct bidi-tries.
- Added ability to associate a pattern with an integer
in the bidi-trie [1].
- The bidi-trie match code passes this externally
provided integer when calling an externally
provided method used for testing extra conditions
that may be present for a plain pattern found to
be matching in the bidi-trie.
- Decomposed existing filters into smaller logical units:
- FilterPlainLeftAnchored =>
FilterPatternPlain +
FilterAnchorLeft
- FilterPlainRightAnchored =>
FilterPatternPlain +
FilterAnchorRight
- FilterExactMatch =>
FilterPatternPlain +
FilterAnchorLeft +
FilterAnchorRight
- FilterPlainHnAnchored =>
FilterPatternPlain +
FilterAnchorHn
- FilterWildcard1 =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
]
- FilterWildcard1HnAnchored =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
] +
FilterAnchorHn
- FilterGenericHnAnchored =>
FilterPatternGeneric +
FilterAnchorHn
- FilterGenericHnAndRightAnchored =>
FilterPatternGeneric +
FilterAnchorRight +
FilterAnchorHn
- FilterOriginMixedSet =>
FilterOriginMissSet +
FilterOriginHitSet
- Instances of FilterOrigin[...], FilterDataHolder
can also be added to a composite filter to
represent `domain=` and `csp=` options.
- Added a new filter class, FilterComposite, for
filters which are a combination of two or more
logical units. A FilterComposite instance is a
match when *all* filters composing it are a
match.
Since filters are now encoded into combination of
smaller units, it becomes possible to extract the
FilterPatternPlain component and store it in the
bidi-trie, and use the integer as a handle for the
remaining extra conditions, if any.
Since a single pattern in the bidi-trie may be a
component for different filters, the associated
integer points to a sequence of extra conditions,
and a match occurs as soon as one of the extra
conditions (which may itself be a sequence of
conditions) is fulfilled.
Decomposing filters which are currently single
instance into sequences of smaller logical filters
means increasing the storage and CPU overhead when
evaluating such filters. The CPU overhead is
compensated by the fact that more filters can now
moved into the bidi-trie, where the first match is
efficiently evaluated. The extra conditions have to
be evaluated if and only if there is a match in the
bidi-trie.
The storage overhead is compensated by the
bidi-trie's intrinsic nature of merging similar
patterns.
Furthermore, the storage overhead is reduced by no
longer using JavaScript array to store collection
of filters (which is what FilterComposite is):
the same technique used in [2] is imported to store
sequences of filters.
A sequence of filters is a sequence of integer pairs
where the first integer is an index to an actual
filter instance stored in a global array of filters
(`filterUnits`), while the second integer is an index
to the next pair in the sequence -- which means all
sequences of filters are encoded in one single array
of integers (`filterSequences` => Uint32Array). As
a result, a sequence of filters can be represented by
one single integer -- an index to the first pair --
regardless of the number of filters in the sequence.
This representation is further leveraged to replace
the use of JavaScript array in FilterBucket [3],
which used a JavaScript array to store collection
of filters. Doing so means there is no more need for
FilterPair [4], which purpose was to be a lightweight
representation when there was only two filters in a
collection.
As a result of the above changes, the map of `token`
(integer) => filter instance (object) used to
associate tokens to filters or collections of filters
is replaced with a more efficient map of `token`
(integer) to filter unit index (integer) to lookup a
filter object from the global `filterUnits` array.
Another consequence of using one single global
array to store all filter instances means we can reuse
existing instances when a logical filter instance is
parameter-less, which is the case for FilterAnchorLeft,
FilterAnchorRight, FilterAnchorHn, the index to these
single instances is reused where needed.
`urlTokenizer` now stores the character codes of the
scanned URL into a bidi-trie buffer, for reuse when
string matching methods are called.
New method: `tokenHistogram()`, used to generate
histograms of occurrences of token extracted from URLs
in built-in benchmark. The top results of the "miss"
histogram are used as "bad tokens", i.e. tokens to
avoid if possible when compiling filter lists.
All plain pattern strings are now stored in the
bidi-trie memory buffer, regardless of whether they
will be used in the trie proper or not.
Three methods have been added to the bidi-trie to test
stored string against the URL which is also stored in
then bidi-trie.
FilterParser is now instanciated on demand and
released when no longer used.
***
[1] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/strie.js#L120
[2] https://github.com/gorhill/uBlock/commit/e94024d350b066e4e04a772b0a3dbc69daab3fb7
[3] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1630
[4] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1566
2019-10-21 14:15:58 +02:00
|
|
|
request.rawFilter
|
|
|
|
).then(response => {
|
|
|
|
callback(response);
|
|
|
|
});
|
2015-06-13 17:21:55 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 'listsFromCosmeticFilter':
|
2022-04-25 15:49:31 +02:00
|
|
|
staticFilteringReverseLookup.fromExtendedFilter(
|
Expand bidi-trie usage in static network filtering engine
Related issues:
- https://github.com/uBlockOrigin/uBlock-issues/issues/761
- https://github.com/uBlockOrigin/uBlock-issues/issues/528
The previous bidi-trie code could only hold filters which
are plain pattern, i.e. no wildcard characters, and which
had no origin option (`domain=`), right and/or left anchor,
and no `csp=` option.
Example of filters that could be moved into a bidi-trie
data structure:
&ad_box_
/w/d/capu.php?z=$script,third-party
||liveonlinetv247.com/images/muvixx-150x50-watch-now-in-hd-play-btn.gif
Examples of filters that could NOT be moved to a bidi-trie:
-adap.$domain=~l-adap.org
/tsc.php?*&ses=
||ibsrv.net/*forumsponsor$domain=[...]
@@||imgspice.com/jquery.cookie.js|$script
||view.atdmt.com^*/iview/$third-party
||postimg.cc/image/$csp=[...]
Ideally the filters above should be able to be moved to a
bidi-trie since they are basically plain patterns, or at
least partially moved to a bidi-trie when there is only a
single wildcard (i.e. made of two plain patterns).
Also, there were two distinct bidi-tries in which
plain-pattern filters can be moved to: one for patterns
without hostname anchoring and another one for patterns
with hostname-anchoring. This was required because the
hostname-anchored patterns have an extra condition which
is outside the bidi-trie knowledge.
This commit expands the number of filters which can be
stored in the bidi-trie, and also remove the need to
use two distinct bidi-tries.
- Added ability to associate a pattern with an integer
in the bidi-trie [1].
- The bidi-trie match code passes this externally
provided integer when calling an externally
provided method used for testing extra conditions
that may be present for a plain pattern found to
be matching in the bidi-trie.
- Decomposed existing filters into smaller logical units:
- FilterPlainLeftAnchored =>
FilterPatternPlain +
FilterAnchorLeft
- FilterPlainRightAnchored =>
FilterPatternPlain +
FilterAnchorRight
- FilterExactMatch =>
FilterPatternPlain +
FilterAnchorLeft +
FilterAnchorRight
- FilterPlainHnAnchored =>
FilterPatternPlain +
FilterAnchorHn
- FilterWildcard1 =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
]
- FilterWildcard1HnAnchored =>
FilterPatternPlain + [
FilterPatternLeft or
FilterPatternRight
] +
FilterAnchorHn
- FilterGenericHnAnchored =>
FilterPatternGeneric +
FilterAnchorHn
- FilterGenericHnAndRightAnchored =>
FilterPatternGeneric +
FilterAnchorRight +
FilterAnchorHn
- FilterOriginMixedSet =>
FilterOriginMissSet +
FilterOriginHitSet
- Instances of FilterOrigin[...], FilterDataHolder
can also be added to a composite filter to
represent `domain=` and `csp=` options.
- Added a new filter class, FilterComposite, for
filters which are a combination of two or more
logical units. A FilterComposite instance is a
match when *all* filters composing it are a
match.
Since filters are now encoded into combination of
smaller units, it becomes possible to extract the
FilterPatternPlain component and store it in the
bidi-trie, and use the integer as a handle for the
remaining extra conditions, if any.
Since a single pattern in the bidi-trie may be a
component for different filters, the associated
integer points to a sequence of extra conditions,
and a match occurs as soon as one of the extra
conditions (which may itself be a sequence of
conditions) is fulfilled.
Decomposing filters which are currently single
instance into sequences of smaller logical filters
means increasing the storage and CPU overhead when
evaluating such filters. The CPU overhead is
compensated by the fact that more filters can now
moved into the bidi-trie, where the first match is
efficiently evaluated. The extra conditions have to
be evaluated if and only if there is a match in the
bidi-trie.
The storage overhead is compensated by the
bidi-trie's intrinsic nature of merging similar
patterns.
Furthermore, the storage overhead is reduced by no
longer using JavaScript array to store collection
of filters (which is what FilterComposite is):
the same technique used in [2] is imported to store
sequences of filters.
A sequence of filters is a sequence of integer pairs
where the first integer is an index to an actual
filter instance stored in a global array of filters
(`filterUnits`), while the second integer is an index
to the next pair in the sequence -- which means all
sequences of filters are encoded in one single array
of integers (`filterSequences` => Uint32Array). As
a result, a sequence of filters can be represented by
one single integer -- an index to the first pair --
regardless of the number of filters in the sequence.
This representation is further leveraged to replace
the use of JavaScript array in FilterBucket [3],
which used a JavaScript array to store collection
of filters. Doing so means there is no more need for
FilterPair [4], which purpose was to be a lightweight
representation when there was only two filters in a
collection.
As a result of the above changes, the map of `token`
(integer) => filter instance (object) used to
associate tokens to filters or collections of filters
is replaced with a more efficient map of `token`
(integer) to filter unit index (integer) to lookup a
filter object from the global `filterUnits` array.
Another consequence of using one single global
array to store all filter instances means we can reuse
existing instances when a logical filter instance is
parameter-less, which is the case for FilterAnchorLeft,
FilterAnchorRight, FilterAnchorHn, the index to these
single instances is reused where needed.
`urlTokenizer` now stores the character codes of the
scanned URL into a bidi-trie buffer, for reuse when
string matching methods are called.
New method: `tokenHistogram()`, used to generate
histograms of occurrences of token extracted from URLs
in built-in benchmark. The top results of the "miss"
histogram are used as "bad tokens", i.e. tokens to
avoid if possible when compiling filter lists.
All plain pattern strings are now stored in the
bidi-trie memory buffer, regardless of whether they
will be used in the trie proper or not.
Three methods have been added to the bidi-trie to test
stored string against the URL which is also stored in
then bidi-trie.
FilterParser is now instanciated on demand and
released when no longer used.
***
[1] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/strie.js#L120
[2] https://github.com/gorhill/uBlock/commit/e94024d350b066e4e04a772b0a3dbc69daab3fb7
[3] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1630
[4] https://github.com/gorhill/uBlock/blob/135a45a878f5b93bc538f822981e3a42b1e9073f/src/js/static-net-filtering.js#L1566
2019-10-21 14:15:58 +02:00
|
|
|
request
|
|
|
|
).then(response => {
|
|
|
|
callback(response);
|
|
|
|
});
|
2015-06-11 18:12:23 +02:00
|
|
|
return;
|
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
case 'reloadAllFilters':
|
2019-09-18 14:34:55 +02:00
|
|
|
µb.loadFilterLists().then(( ) => { callback(); });
|
2015-06-26 06:08:41 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
case 'scriptlet':
|
2021-10-07 20:41:29 +02:00
|
|
|
vAPI.tabs.executeScript(request.tabId, {
|
|
|
|
file: `/js/scriptlets/${request.scriptlet}.js`
|
|
|
|
}).then(result => {
|
|
|
|
callback(result);
|
|
|
|
});
|
2015-06-26 06:08:41 +02:00
|
|
|
return;
|
|
|
|
|
2021-12-13 14:30:12 +01:00
|
|
|
case 'snfeBenchmark':
|
2021-07-29 14:44:15 +02:00
|
|
|
µb.benchmarkStaticNetFiltering({ redirectEngine }).then(result => {
|
|
|
|
callback(result);
|
2020-10-27 17:59:31 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-11-16 15:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2020-10-09 19:50:54 +02:00
|
|
|
let response;
|
2014-11-16 15:49:55 +01:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2017-01-22 22:05:16 +01:00
|
|
|
case 'applyFilterListSelection':
|
|
|
|
response = µb.applyFilterListSelection(request);
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2020-10-09 19:50:54 +02:00
|
|
|
case 'clickToLoad':
|
|
|
|
response = clickToLoad(request, sender);
|
|
|
|
break;
|
|
|
|
|
2015-06-07 00:31:38 +02:00
|
|
|
case 'createUserFilter':
|
2019-09-01 18:43:12 +02:00
|
|
|
µb.createUserFilters(request);
|
2015-06-07 00:31:38 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'forceUpdateAssets':
|
2017-01-18 19:17:47 +01:00
|
|
|
µb.scheduleAssetUpdater(0);
|
2021-07-29 01:48:38 +02:00
|
|
|
io.updateStart({
|
2017-12-30 23:38:07 +01:00
|
|
|
delay: µb.hiddenSettings.manualUpdateAssetFetchPeriod
|
|
|
|
});
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-12-01 17:25:33 +01:00
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'getAppData':
|
2018-05-17 18:45:57 +02:00
|
|
|
response = {
|
2019-09-21 18:06:57 +02:00
|
|
|
name: browser.runtime.getManifest().name,
|
2020-10-27 17:59:31 +01:00
|
|
|
version: vAPI.app.version,
|
|
|
|
canBenchmark: µb.hiddenSettings.benchmarkDatasetURL !== 'unset',
|
2018-05-17 18:45:57 +02:00
|
|
|
};
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2015-06-07 00:31:38 +02:00
|
|
|
case 'getDomainNames':
|
|
|
|
response = getDomainNames(request.targets);
|
|
|
|
break;
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'getWhitelist':
|
2018-03-12 13:28:07 +01:00
|
|
|
response = {
|
2019-05-18 20:20:05 +02:00
|
|
|
whitelist: µb.arrayFromWhitelist(µb.netWhitelist),
|
|
|
|
whitelistDefault: µb.netWhitelistDefault,
|
2018-03-12 13:28:07 +01:00
|
|
|
reBadHostname: µb.reWhitelistBadHostname.source,
|
|
|
|
reHostnameExtractor: µb.reWhitelistHostnameExtractor.source
|
|
|
|
};
|
2016-03-06 16:51:06 +01:00
|
|
|
break;
|
|
|
|
|
2015-05-25 00:50:09 +02:00
|
|
|
case 'launchElementPicker':
|
|
|
|
// Launched from some auxiliary pages, clear context menu coords.
|
2019-09-18 18:17:45 +02:00
|
|
|
µb.epickerArgs.mouse = false;
|
2020-12-05 21:26:29 +01:00
|
|
|
µb.elementPickerExec(request.tabId, 0, request.targetURL, request.zap);
|
2015-05-25 00:50:09 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'gotoURL':
|
2016-09-16 23:41:17 +02:00
|
|
|
µb.openNewTab(request.details);
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'reloadTab':
|
|
|
|
if ( vAPI.isBehindTheSceneTabId(request.tabId) === false ) {
|
2018-01-07 23:53:33 +01:00
|
|
|
vAPI.tabs.reload(request.tabId, request.bypassCache === true);
|
2015-04-25 13:28:35 +02:00
|
|
|
if ( request.select && vAPI.tabs.select ) {
|
|
|
|
vAPI.tabs.select(request.tabId);
|
2015-01-20 00:42:58 +01:00
|
|
|
}
|
2015-04-25 13:28:35 +02:00
|
|
|
}
|
|
|
|
break;
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'setWhitelist':
|
|
|
|
µb.netWhitelist = µb.whitelistFromString(request.whitelist);
|
|
|
|
µb.saveWhitelist();
|
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'toggleHostnameSwitch':
|
|
|
|
µb.toggleHostnameSwitch(request);
|
|
|
|
break;
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2022-02-03 12:14:04 +01:00
|
|
|
case 'uiAccentStylesheet':
|
|
|
|
µb.uiAccentStylesheet = request.stylesheet;
|
|
|
|
break;
|
|
|
|
|
2020-05-19 16:33:36 +02:00
|
|
|
case 'uiStyles':
|
2020-10-03 13:13:40 +02:00
|
|
|
response = {
|
2022-02-02 21:40:47 +01:00
|
|
|
uiAccentCustom: µb.userSettings.uiAccentCustom,
|
|
|
|
uiAccentCustom0: µb.userSettings.uiAccentCustom0,
|
2022-02-03 12:14:04 +01:00
|
|
|
uiAccentStylesheet: µb.uiAccentStylesheet,
|
2020-10-03 13:13:40 +02:00
|
|
|
uiStyles: µb.hiddenSettings.uiStyles,
|
2022-02-02 21:40:47 +01:00
|
|
|
uiTheme: µb.userSettings.uiTheme,
|
2020-10-03 13:13:40 +02:00
|
|
|
};
|
2020-05-19 16:33:36 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'userSettings':
|
|
|
|
response = µb.changeUserSettings(request.name, request.value);
|
2021-09-15 14:28:10 +02:00
|
|
|
if ( response instanceof Object ) {
|
|
|
|
if ( vAPI.net.canUncloakCnames !== true ) {
|
|
|
|
response.cnameUncloakEnabled = undefined;
|
|
|
|
}
|
|
|
|
response.canLeakLocalIPAddresses =
|
|
|
|
vAPI.browserSettings.canLeakLocalIPAddresses === true;
|
2021-03-02 19:00:56 +01:00
|
|
|
}
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2021-12-13 14:30:12 +01:00
|
|
|
case 'snfeDump':
|
2021-12-12 16:32:49 +01:00
|
|
|
response = staticNetFilteringEngine.dump();
|
2021-12-13 14:30:12 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'cfeDump':
|
|
|
|
response = cosmeticFilteringEngine.dump();
|
2021-12-12 16:32:49 +01:00
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2014-10-17 21:44:19 +02:00
|
|
|
}
|
2014-11-16 15:49:55 +01:00
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
|
|
|
vAPI.messaging.setup(onMessage);
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// popupPanel
|
|
|
|
// privileged
|
2014-09-28 18:05:46 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2021-02-15 12:52:31 +01:00
|
|
|
const createCounts = ( ) => {
|
|
|
|
return {
|
|
|
|
blocked: { any: 0, frame: 0, script: 0 },
|
|
|
|
allowed: { any: 0, frame: 0, script: 0 },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getHostnameDict = function(hostnameDetailsMap, out) {
|
2019-12-31 22:36:51 +01:00
|
|
|
const hnDict = Object.create(null);
|
2020-02-06 16:18:15 +01:00
|
|
|
const cnMap = [];
|
2021-02-15 12:52:31 +01:00
|
|
|
|
|
|
|
const createDictEntry = (domain, hostname, details) => {
|
|
|
|
const cname = vAPI.net.canonicalNameFromHostname(hostname);
|
|
|
|
if ( cname !== undefined ) {
|
|
|
|
cnMap.push([ cname, hostname ]);
|
|
|
|
}
|
|
|
|
hnDict[hostname] = { domain, counts: details.counts };
|
|
|
|
};
|
|
|
|
|
|
|
|
for ( const hnDetails of hostnameDetailsMap.values() ) {
|
|
|
|
const hostname = hnDetails.hostname;
|
2019-12-31 22:36:51 +01:00
|
|
|
if ( hnDict[hostname] !== undefined ) { continue; }
|
2021-07-25 16:55:35 +02:00
|
|
|
const domain = domainFromHostname(hostname) || hostname;
|
2021-02-15 12:52:31 +01:00
|
|
|
const dnDetails =
|
|
|
|
hostnameDetailsMap.get(domain) || { counts: createCounts() };
|
2019-12-31 22:36:51 +01:00
|
|
|
if ( hnDict[domain] === undefined ) {
|
2021-02-15 12:52:31 +01:00
|
|
|
createDictEntry(domain, domain, dnDetails);
|
2015-02-17 03:15:09 +01:00
|
|
|
}
|
2017-05-19 16:12:55 +02:00
|
|
|
if ( hostname === domain ) { continue; }
|
2021-02-15 12:52:31 +01:00
|
|
|
createDictEntry(domain, hostname, hnDetails);
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
2021-02-15 12:52:31 +01:00
|
|
|
|
2019-12-31 22:36:51 +01:00
|
|
|
out.hostnameDict = hnDict;
|
2020-02-06 16:18:15 +01:00
|
|
|
out.cnameMap = cnMap;
|
2014-12-30 22:36:29 +01:00
|
|
|
};
|
|
|
|
|
2021-02-15 12:52:31 +01:00
|
|
|
const firewallRuleTypes = [
|
|
|
|
'*',
|
|
|
|
'image',
|
|
|
|
'3p',
|
|
|
|
'inline-script',
|
|
|
|
'1p-script',
|
|
|
|
'3p-script',
|
|
|
|
'3p-frame',
|
|
|
|
];
|
|
|
|
|
|
|
|
const getFirewallRules = function(src, out) {
|
2021-02-21 13:41:09 +01:00
|
|
|
const ruleset = out.firewallRules = {};
|
2021-07-29 01:48:38 +02:00
|
|
|
const df = sessionFirewall;
|
2021-02-15 12:52:31 +01:00
|
|
|
|
|
|
|
for ( const type of firewallRuleTypes ) {
|
2021-02-21 13:41:09 +01:00
|
|
|
const r = df.lookupRuleData('*', '*', type);
|
2021-02-15 12:52:31 +01:00
|
|
|
if ( r === undefined ) { continue; }
|
|
|
|
ruleset[`/ * ${type}`] = r;
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
2021-02-21 13:41:09 +01:00
|
|
|
if ( typeof src !== 'string' ) { return; }
|
2021-02-15 12:52:31 +01:00
|
|
|
|
|
|
|
for ( const type of firewallRuleTypes ) {
|
2021-02-21 13:41:09 +01:00
|
|
|
const r = df.lookupRuleData(src, '*', type);
|
2021-02-15 12:52:31 +01:00
|
|
|
if ( r === undefined ) { continue; }
|
|
|
|
ruleset[`. * ${type}`] = r;
|
|
|
|
}
|
|
|
|
|
2021-02-21 13:41:09 +01:00
|
|
|
const { hostnameDict } = out;
|
2021-02-15 12:52:31 +01:00
|
|
|
for ( const des in hostnameDict ) {
|
|
|
|
let r = df.lookupRuleData('*', des, '*');
|
|
|
|
if ( r !== undefined ) { ruleset[`/ ${des} *`] = r; }
|
|
|
|
r = df.lookupRuleData(src, des, '*');
|
|
|
|
if ( r !== undefined ) { ruleset[`. ${des} *`] = r; }
|
|
|
|
}
|
2014-10-06 20:02:44 +02:00
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
const popupDataFromTabId = function(tabId, tabTitle) {
|
|
|
|
const tabContext = µb.tabContextManager.mustLookup(tabId);
|
|
|
|
const rootHostname = tabContext.rootHostname;
|
2020-04-30 12:54:51 +02:00
|
|
|
const µbus = µb.userSettings;
|
2020-05-22 17:35:44 +02:00
|
|
|
const µbhs = µb.hiddenSettings;
|
2019-09-01 18:43:12 +02:00
|
|
|
const r = {
|
2020-04-30 12:54:51 +02:00
|
|
|
advancedUserEnabled: µbus.advancedUserEnabled,
|
2014-12-01 17:25:33 +01:00
|
|
|
appName: vAPI.app.name,
|
|
|
|
appVersion: vAPI.app.version,
|
2020-04-30 12:54:51 +02:00
|
|
|
colorBlindFriendly: µbus.colorBlindFriendly,
|
2015-01-06 17:44:06 +01:00
|
|
|
cosmeticFilteringSwitch: false,
|
2020-04-30 12:54:51 +02:00
|
|
|
firewallPaneMinimized: µbus.firewallPaneMinimized,
|
2014-06-24 00:42:43 +02:00
|
|
|
globalAllowedRequestCount: µb.localSettings.allowedRequestCount,
|
2015-01-06 17:44:06 +01:00
|
|
|
globalBlockedRequestCount: µb.localSettings.blockedRequestCount,
|
2020-05-22 17:35:44 +02:00
|
|
|
fontSize: µbhs.popupFontSize,
|
2020-05-25 13:08:02 +02:00
|
|
|
godMode: µbhs.filterAuthorMode,
|
2015-01-06 17:44:06 +01:00
|
|
|
netFilteringSwitch: false,
|
2015-04-09 00:46:08 +02:00
|
|
|
rawURL: tabContext.rawURL,
|
|
|
|
pageURL: tabContext.normalURL,
|
2016-01-17 19:30:43 +01:00
|
|
|
pageHostname: rootHostname,
|
2015-04-09 00:46:08 +02:00
|
|
|
pageDomain: tabContext.rootDomain,
|
2016-01-04 16:48:28 +01:00
|
|
|
popupBlockedCount: 0,
|
2020-04-30 12:54:51 +02:00
|
|
|
popupPanelSections: µbus.popupPanelSections,
|
2020-05-22 17:35:44 +02:00
|
|
|
popupPanelDisabledSections: µbhs.popupPanelDisabledSections,
|
|
|
|
popupPanelLockedSections: µbhs.popupPanelLockedSections,
|
|
|
|
popupPanelHeightMode: µbhs.popupPanelHeightMode,
|
2015-03-02 19:49:34 +01:00
|
|
|
tabId: tabId,
|
2015-12-13 06:33:53 +01:00
|
|
|
tabTitle: tabTitle,
|
2020-04-30 12:54:51 +02:00
|
|
|
tooltipsDisabled: µbus.tooltipsDisabled
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
2015-04-09 00:46:08 +02:00
|
|
|
|
2021-11-28 16:35:50 +01:00
|
|
|
if ( µbhs.uiPopupConfig !== 'unset' ) {
|
2020-05-22 17:35:44 +02:00
|
|
|
r.uiPopupConfig = µbhs.uiPopupConfig;
|
2020-04-27 16:33:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
2014-06-24 00:42:43 +02:00
|
|
|
if ( pageStore ) {
|
2021-02-15 12:52:31 +01:00
|
|
|
r.pageCounts = pageStore.counts;
|
2014-08-02 17:40:27 +02:00
|
|
|
r.netFilteringSwitch = pageStore.getNetFilteringSwitch();
|
2021-02-15 12:52:31 +01:00
|
|
|
getHostnameDict(pageStore.getAllHostnameDetails(), r);
|
2015-01-10 17:23:28 +01:00
|
|
|
r.contentLastModified = pageStore.contentLastModified;
|
2021-02-15 12:52:31 +01:00
|
|
|
getFirewallRules(rootHostname, r);
|
2021-07-25 16:55:35 +02:00
|
|
|
r.canElementPicker = isNetworkURI(r.rawURL);
|
2021-07-29 01:48:38 +02:00
|
|
|
r.noPopups = sessionSwitches.evaluateZ(
|
2019-09-01 18:43:12 +02:00
|
|
|
'no-popups',
|
|
|
|
rootHostname
|
|
|
|
);
|
2016-01-04 16:48:28 +01:00
|
|
|
r.popupBlockedCount = pageStore.popupBlockedCount;
|
2021-07-29 01:48:38 +02:00
|
|
|
r.noCosmeticFiltering = sessionSwitches.evaluateZ(
|
2019-09-01 18:43:12 +02:00
|
|
|
'no-cosmetic-filtering',
|
|
|
|
rootHostname
|
|
|
|
);
|
2021-07-29 01:48:38 +02:00
|
|
|
r.noLargeMedia = sessionSwitches.evaluateZ(
|
2019-09-01 18:43:12 +02:00
|
|
|
'no-large-media',
|
|
|
|
rootHostname
|
|
|
|
);
|
2016-01-17 19:30:43 +01:00
|
|
|
r.largeMediaCount = pageStore.largeMediaCount;
|
2021-07-29 01:48:38 +02:00
|
|
|
r.noRemoteFonts = sessionSwitches.evaluateZ(
|
2019-09-01 18:43:12 +02:00
|
|
|
'no-remote-fonts',
|
|
|
|
rootHostname
|
|
|
|
);
|
2016-01-17 19:30:43 +01:00
|
|
|
r.remoteFontCount = pageStore.remoteFontCount;
|
2021-07-29 01:48:38 +02:00
|
|
|
r.noScripting = sessionSwitches.evaluateZ(
|
2019-09-01 18:43:12 +02:00
|
|
|
'no-scripting',
|
|
|
|
rootHostname
|
|
|
|
);
|
2014-12-30 22:36:29 +01:00
|
|
|
} else {
|
|
|
|
r.hostnameDict = {};
|
2021-02-21 13:41:09 +01:00
|
|
|
getFirewallRules(undefined, r);
|
2015-02-11 06:26:45 +01:00
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
r.matrixIsDirty = sessionFirewall.hasSameRules(
|
|
|
|
permanentFirewall,
|
2016-01-17 19:30:43 +01:00
|
|
|
rootHostname,
|
2015-04-09 00:46:08 +02:00
|
|
|
r.hostnameDict
|
2018-09-03 20:06:49 +02:00
|
|
|
) === false;
|
|
|
|
if ( r.matrixIsDirty === false ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
r.matrixIsDirty = sessionSwitches.hasSameRules(
|
|
|
|
permanentSwitches,
|
2018-09-03 20:06:49 +02:00
|
|
|
rootHostname
|
|
|
|
) === false;
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
return r;
|
|
|
|
};
|
|
|
|
|
2019-09-16 15:45:17 +02:00
|
|
|
const popupDataFromRequest = async function(request) {
|
2015-05-18 16:39:20 +02:00
|
|
|
if ( request.tabId ) {
|
2019-09-16 15:45:17 +02:00
|
|
|
return popupDataFromTabId(request.tabId, '');
|
2015-01-20 00:42:58 +01:00
|
|
|
}
|
2015-01-21 17:13:32 +01:00
|
|
|
|
2015-05-18 16:39:20 +02:00
|
|
|
// Still no target tab id? Use currently selected tab.
|
2019-09-16 15:45:17 +02:00
|
|
|
const tab = await vAPI.tabs.getCurrent();
|
|
|
|
let tabId = '';
|
|
|
|
let tabTitle = '';
|
|
|
|
if ( tab instanceof Object ) {
|
|
|
|
tabId = tab.id;
|
|
|
|
tabTitle = tab.title || '';
|
|
|
|
}
|
|
|
|
return popupDataFromTabId(tabId, tabTitle);
|
2015-01-20 00:42:58 +01:00
|
|
|
};
|
|
|
|
|
2019-11-01 17:32:34 +01:00
|
|
|
const getElementCount = async function(tabId, what) {
|
2019-09-17 21:15:01 +02:00
|
|
|
const results = await vAPI.tabs.executeScript(tabId, {
|
|
|
|
allFrames: true,
|
2019-11-01 17:32:34 +01:00
|
|
|
file: `/js/scriptlets/dom-survey-${what}.js`,
|
2019-09-17 21:15:01 +02:00
|
|
|
runAt: 'document_end',
|
|
|
|
});
|
|
|
|
|
2019-11-01 17:32:34 +01:00
|
|
|
let total = 0;
|
2019-11-02 14:16:23 +01:00
|
|
|
for ( const count of results ) {
|
|
|
|
if ( typeof count !== 'number' ) { continue; }
|
|
|
|
if ( count === -1 ) { return -1; }
|
2019-11-01 17:32:34 +01:00
|
|
|
total += count;
|
2019-11-02 14:16:23 +01:00
|
|
|
}
|
2019-09-17 21:15:01 +02:00
|
|
|
|
2019-11-01 17:32:34 +01:00
|
|
|
return total;
|
2019-09-17 21:15:01 +02:00
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2014-06-24 00:42:43 +02:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
2019-11-01 17:32:34 +01:00
|
|
|
case 'getHiddenElementCount':
|
2019-11-03 15:38:36 +01:00
|
|
|
getElementCount(request.tabId, 'elements').then(count => {
|
2019-11-01 17:32:34 +01:00
|
|
|
callback(count);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'getScriptCount':
|
|
|
|
getElementCount(request.tabId, 'scripts').then(count => {
|
|
|
|
callback(count);
|
2019-09-17 21:15:01 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
2015-04-24 14:36:19 +02:00
|
|
|
case 'getPopupData':
|
2019-09-16 15:45:17 +02:00
|
|
|
popupDataFromRequest(request).then(popupData => {
|
|
|
|
callback(popupData);
|
|
|
|
});
|
2015-04-24 14:36:19 +02:00
|
|
|
return;
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2015-04-24 14:36:19 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2019-09-01 18:43:12 +02:00
|
|
|
let response;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2021-11-11 18:49:55 +01:00
|
|
|
case 'hasPopupContentChanged': {
|
|
|
|
const pageStore = µb.pageStoreFromTabId(request.tabId);
|
|
|
|
const lastModified = pageStore ? pageStore.contentLastModified : 0;
|
2015-04-24 14:36:19 +02:00
|
|
|
response = lastModified !== request.contentLastModified;
|
|
|
|
break;
|
2021-11-11 18:49:55 +01:00
|
|
|
}
|
|
|
|
case 'launchReporter': {
|
|
|
|
const pageStore = µb.pageStoreFromTabId(request.tabId);
|
|
|
|
if ( pageStore === null ) { break; }
|
|
|
|
const supportURL = new URL(vAPI.getURL('support.html'));
|
2021-11-27 17:58:32 +01:00
|
|
|
supportURL.searchParams.set('pageURL', request.pageURL);
|
|
|
|
supportURL.searchParams.set('popupPanel', request.popupPanel);
|
2021-11-11 18:49:55 +01:00
|
|
|
µb.openNewTab({ url: supportURL.href, select: true, index: -1 });
|
|
|
|
break;
|
|
|
|
}
|
2015-04-24 14:36:19 +02:00
|
|
|
case 'revertFirewallRules':
|
2021-08-03 18:19:25 +02:00
|
|
|
// TODO: use Set() to message around sets of hostnames
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionFirewall.copyRules(
|
|
|
|
permanentFirewall,
|
2015-04-24 14:36:19 +02:00
|
|
|
request.srcHostname,
|
2021-08-03 18:19:25 +02:00
|
|
|
Object.assign(Object.create(null), request.desHostnames)
|
2015-04-24 14:36:19 +02:00
|
|
|
);
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionSwitches.copyRules(
|
|
|
|
permanentSwitches,
|
2018-09-03 20:06:49 +02:00
|
|
|
request.srcHostname
|
|
|
|
);
|
2015-05-09 14:25:05 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/188
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.removeFromSelectorCache(
|
2018-09-03 20:06:49 +02:00
|
|
|
request.srcHostname,
|
|
|
|
'net'
|
|
|
|
);
|
2019-08-10 16:57:24 +02:00
|
|
|
µb.updateToolbarIcon(request.tabId, 0b100);
|
2015-05-18 16:39:20 +02:00
|
|
|
response = popupDataFromTabId(request.tabId);
|
2015-04-24 14:36:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'saveFirewallRules':
|
2021-08-03 18:19:25 +02:00
|
|
|
// TODO: use Set() to message around sets of hostnames
|
2018-09-03 20:06:49 +02:00
|
|
|
if (
|
2021-07-29 01:48:38 +02:00
|
|
|
permanentFirewall.copyRules(
|
|
|
|
sessionFirewall,
|
2018-09-03 20:06:49 +02:00
|
|
|
request.srcHostname,
|
2021-08-03 18:19:25 +02:00
|
|
|
Object.assign(Object.create(null), request.desHostnames)
|
2018-09-03 20:06:49 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
µb.savePermanentFirewallRules();
|
|
|
|
}
|
|
|
|
if (
|
2021-07-29 01:48:38 +02:00
|
|
|
permanentSwitches.copyRules(
|
|
|
|
sessionSwitches,
|
2018-09-03 20:06:49 +02:00
|
|
|
request.srcHostname
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
µb.saveHostnameSwitches();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'toggleHostnameSwitch':
|
|
|
|
µb.toggleHostnameSwitch(request);
|
|
|
|
response = popupDataFromTabId(request.tabId);
|
2015-04-24 14:36:19 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'toggleFirewallRule':
|
|
|
|
µb.toggleFirewallRule(request);
|
2015-05-18 16:39:20 +02:00
|
|
|
response = popupDataFromTabId(request.tabId);
|
2015-04-24 14:36:19 +02:00
|
|
|
break;
|
|
|
|
|
2021-11-11 18:49:55 +01:00
|
|
|
case 'toggleNetFiltering': {
|
|
|
|
const pageStore = µb.pageStoreFromTabId(request.tabId);
|
2015-04-24 14:36:19 +02:00
|
|
|
if ( pageStore ) {
|
2019-09-01 18:43:12 +02:00
|
|
|
pageStore.toggleNetFilteringSwitch(
|
|
|
|
request.url,
|
|
|
|
request.scope,
|
|
|
|
request.state
|
|
|
|
);
|
2019-08-10 16:57:24 +02:00
|
|
|
µb.updateToolbarIcon(request.tabId, 0b111);
|
2015-04-24 14:36:19 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-11-11 18:49:55 +01:00
|
|
|
}
|
2015-04-24 14:36:19 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'popupPanel',
|
|
|
|
listener: onMessage,
|
|
|
|
privileged: true,
|
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// contentscript
|
|
|
|
// unprivileged
|
2014-06-27 23:06:42 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2014-06-27 23:06:42 +02:00
|
|
|
|
2021-02-17 15:12:00 +01:00
|
|
|
const retrieveContentScriptParameters = async function(sender, request) {
|
2020-04-04 17:34:43 +02:00
|
|
|
if ( µb.readyToFilter !== true ) { return; }
|
2020-11-13 14:32:51 +01:00
|
|
|
const { tabId, frameId } = sender;
|
|
|
|
if ( tabId === undefined || frameId === undefined ) { return; }
|
|
|
|
|
2019-09-21 17:30:38 +02:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore === null || pageStore.getNetFilteringSwitch() === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:56:27 +01:00
|
|
|
// A content script may not always be able to successfully look up the
|
|
|
|
// effective context, hence in such case we try again to look up here
|
|
|
|
// using cached information about embedded frames.
|
|
|
|
if ( frameId !== 0 && request.url.startsWith('about:') ) {
|
|
|
|
request.url = pageStore.getEffectiveFrameURL(sender);
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:55:58 +02:00
|
|
|
const noSpecificCosmeticFiltering =
|
|
|
|
pageStore.shouldApplySpecificCosmeticFilters(frameId) === false;
|
|
|
|
const noGenericCosmeticFiltering =
|
|
|
|
pageStore.shouldApplyGenericCosmeticFilters(frameId) === false;
|
2019-09-21 17:30:38 +02:00
|
|
|
|
|
|
|
const response = {
|
|
|
|
collapseBlocked: µb.userSettings.collapseBlocked,
|
2021-07-12 17:55:58 +02:00
|
|
|
noGenericCosmeticFiltering,
|
|
|
|
noSpecificCosmeticFiltering,
|
2019-09-21 17:30:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
request.tabId = tabId;
|
|
|
|
request.frameId = frameId;
|
2021-07-25 16:55:35 +02:00
|
|
|
request.hostname = hostnameFromURI(request.url);
|
|
|
|
request.domain = domainFromHostname(request.hostname);
|
|
|
|
request.entity = entityFromDomain(request.domain);
|
2019-09-21 17:30:38 +02:00
|
|
|
|
2022-07-24 17:41:08 +02:00
|
|
|
const scf = response.specificCosmeticFilters =
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.retrieveSpecificSelectors(request, response);
|
2019-09-21 17:30:38 +02:00
|
|
|
|
2021-02-17 15:12:00 +01:00
|
|
|
// The procedural filterer's code is loaded only when needed and must be
|
|
|
|
// present before returning response to caller.
|
|
|
|
if (
|
2022-07-24 17:41:08 +02:00
|
|
|
scf.proceduralFilters.length !== 0 || (
|
|
|
|
logger.enabled && (
|
|
|
|
scf.convertedProceduralFilters.length !== 0 ||
|
|
|
|
scf.exceptedFilters.length !== 0
|
|
|
|
)
|
2021-02-17 15:12:00 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
await vAPI.tabs.executeScript(tabId, {
|
|
|
|
allFrames: false,
|
|
|
|
file: '/js/contentscript-extra.js',
|
|
|
|
frameId,
|
|
|
|
matchAboutBlank: true,
|
|
|
|
runAt: 'document_start',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-18 18:07:08 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/688#issuecomment-748179731
|
|
|
|
// For non-network URIs, scriptlet injection is deferred to here. The
|
|
|
|
// effective URL is available here in `request.url`.
|
2021-08-27 00:36:18 +02:00
|
|
|
if (
|
|
|
|
µb.canInjectScriptletsNow === false ||
|
|
|
|
isNetworkURI(sender.frameURL) === false
|
|
|
|
) {
|
2022-03-31 17:50:45 +02:00
|
|
|
scriptletFilteringEngine.injectNow(request);
|
2019-09-21 17:30:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:12:00 +01:00
|
|
|
// https://github.com/NanoMeow/QuickReports/issues/6#issuecomment-414516623
|
|
|
|
// Inject as early as possible to make the cosmetic logger code less
|
|
|
|
// sensitive to the removal of DOM nodes which may match injected
|
|
|
|
// cosmetic filters.
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2021-07-12 17:55:58 +02:00
|
|
|
if (
|
|
|
|
noSpecificCosmeticFiltering === false ||
|
|
|
|
noGenericCosmeticFiltering === false
|
|
|
|
) {
|
|
|
|
vAPI.tabs.executeScript(tabId, {
|
|
|
|
allFrames: false,
|
|
|
|
file: '/js/scriptlets/cosmetic-logger.js',
|
|
|
|
frameId,
|
|
|
|
matchAboutBlank: true,
|
|
|
|
runAt: 'document_start',
|
|
|
|
});
|
|
|
|
}
|
2019-09-21 17:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2014-06-27 23:06:42 +02:00
|
|
|
// Async
|
2015-04-25 13:28:35 +02:00
|
|
|
switch ( request.what ) {
|
2021-02-17 15:12:00 +01:00
|
|
|
case 'retrieveContentScriptParameters':
|
|
|
|
return retrieveContentScriptParameters(
|
|
|
|
sender,
|
|
|
|
request
|
|
|
|
).then(response => {
|
|
|
|
callback(response);
|
|
|
|
});
|
2015-04-25 13:28:35 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-06-27 23:06:42 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 18:14:59 +01:00
|
|
|
const pageStore = µb.pageStoreFromTabId(sender.tabId);
|
2019-09-21 17:30:38 +02:00
|
|
|
|
2014-06-27 23:06:42 +02:00
|
|
|
// Sync
|
2019-09-21 17:30:38 +02:00
|
|
|
let response;
|
2014-06-27 23:06:42 +02:00
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
switch ( request.what ) {
|
2019-09-01 18:43:12 +02:00
|
|
|
case 'cosmeticFiltersInjected':
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.addToSelectorCache(request);
|
2019-09-01 18:43:12 +02:00
|
|
|
break;
|
|
|
|
|
2017-08-03 16:18:05 +02:00
|
|
|
case 'getCollapsibleBlockedRequests':
|
|
|
|
response = {
|
|
|
|
id: request.id,
|
|
|
|
hash: request.hash,
|
2017-10-21 19:43:46 +02:00
|
|
|
netSelectorCacheCountMax:
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.netSelectorCacheCountMax,
|
2017-08-03 16:18:05 +02:00
|
|
|
};
|
|
|
|
if (
|
|
|
|
µb.userSettings.collapseBlocked &&
|
2019-09-21 17:30:38 +02:00
|
|
|
pageStore && pageStore.getNetFilteringSwitch()
|
2017-08-03 16:18:05 +02:00
|
|
|
) {
|
|
|
|
pageStore.getBlockedResources(request, response);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-18 18:17:45 +02:00
|
|
|
case 'maybeGoodPopup':
|
2020-11-12 18:14:59 +01:00
|
|
|
µb.maybeGoodPopup.tabId = sender.tabId;
|
2019-09-18 18:17:45 +02:00
|
|
|
µb.maybeGoodPopup.url = request.url;
|
2019-09-01 18:43:12 +02:00
|
|
|
break;
|
|
|
|
|
2018-09-01 00:47:02 +02:00
|
|
|
case 'shouldRenderNoscriptTags':
|
|
|
|
if ( pageStore === null ) { break; }
|
2020-11-12 18:14:59 +01:00
|
|
|
const fctxt = µb.filteringContext.fromTabId(sender.tabId);
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( pageStore.filterScripting(fctxt, undefined) ) {
|
2020-11-12 18:14:59 +01:00
|
|
|
vAPI.tabs.executeScript(sender.tabId, {
|
2019-09-16 22:17:48 +02:00
|
|
|
file: '/js/scriptlets/noscript-spoof.js',
|
2020-11-12 18:14:59 +01:00
|
|
|
frameId: sender.frameId,
|
2019-09-21 17:30:38 +02:00
|
|
|
runAt: 'document_end',
|
2019-09-16 22:17:48 +02:00
|
|
|
});
|
2018-09-01 00:47:02 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
case 'retrieveGenericCosmeticSelectors':
|
2020-11-12 18:14:59 +01:00
|
|
|
request.tabId = sender.tabId;
|
|
|
|
request.frameId = sender.frameId;
|
2019-06-03 12:37:39 +02:00
|
|
|
response = {
|
2021-07-29 01:48:38 +02:00
|
|
|
result: cosmeticFilteringEngine.retrieveGenericSelectors(request),
|
2019-06-03 12:37:39 +02:00
|
|
|
};
|
2015-04-25 13:28:35 +02:00
|
|
|
break;
|
2014-06-27 23:06:42 +02:00
|
|
|
|
2015-04-25 13:28:35 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2014-10-20 13:26:02 +02:00
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-10-20 13:26:02 +02:00
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'contentscript',
|
|
|
|
listener: onMessage,
|
|
|
|
});
|
2014-10-20 13:26:02 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-10-20 13:26:02 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// elementPicker
|
|
|
|
// unprivileged
|
2014-10-20 13:26:02 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2014-09-28 18:05:46 +02:00
|
|
|
|
2019-01-08 13:37:50 +01:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2014-10-20 13:26:02 +02:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
2021-02-17 15:12:00 +01:00
|
|
|
// The procedural filterer must be present in case the user wants to
|
|
|
|
// type-in custom filters.
|
|
|
|
case 'elementPickerArguments':
|
|
|
|
return vAPI.tabs.executeScript(sender.tabId, {
|
|
|
|
allFrames: false,
|
|
|
|
file: '/js/contentscript-extra.js',
|
|
|
|
frameId: sender.frameId,
|
|
|
|
matchAboutBlank: true,
|
|
|
|
runAt: 'document_start',
|
|
|
|
}).then(( ) => {
|
|
|
|
callback({
|
|
|
|
target: µb.epickerArgs.target,
|
|
|
|
mouse: µb.epickerArgs.mouse,
|
|
|
|
zap: µb.epickerArgs.zap,
|
|
|
|
eprom: µb.epickerArgs.eprom,
|
|
|
|
pickerURL: vAPI.getURL(`/web_accessible_resources/epicker-ui.html?secret=${vAPI.warSecret()}`),
|
|
|
|
});
|
|
|
|
µb.epickerArgs.target = '';
|
|
|
|
});
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-08 19:34:28 +01:00
|
|
|
}
|
2014-07-13 02:32:44 +02:00
|
|
|
|
2015-02-08 19:34:28 +01:00
|
|
|
// Sync
|
2019-01-08 13:37:50 +01:00
|
|
|
let response;
|
2015-02-08 19:34:28 +01:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'elementPickerEprom':
|
2019-09-18 18:17:45 +02:00
|
|
|
µb.epickerArgs.eprom = request;
|
2015-05-19 19:59:53 +02:00
|
|
|
break;
|
2015-03-20 16:39:20 +01:00
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2014-07-13 02:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'elementPicker',
|
|
|
|
listener: onMessage,
|
|
|
|
});
|
2014-09-28 18:05:46 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// cloudWidget
|
|
|
|
// privileged
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2020-08-16 17:53:55 +02:00
|
|
|
const fromBase64 = function(encoded) {
|
|
|
|
if ( typeof encoded !== 'string' ) {
|
|
|
|
return Promise.resolve(encoded);
|
|
|
|
}
|
|
|
|
let u8array;
|
|
|
|
try {
|
2021-08-03 20:03:00 +02:00
|
|
|
u8array = denseBase64.decode(encoded);
|
2020-08-16 17:53:55 +02:00
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
return Promise.resolve(u8array !== undefined ? u8array : encoded);
|
|
|
|
};
|
|
|
|
|
|
|
|
const toBase64 = function(data) {
|
|
|
|
const value = data instanceof Uint8Array
|
2021-08-03 20:03:00 +02:00
|
|
|
? denseBase64.encode(data)
|
2020-08-16 17:53:55 +02:00
|
|
|
: data;
|
|
|
|
return Promise.resolve(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const compress = function(json) {
|
2021-07-29 01:48:38 +02:00
|
|
|
return lz4Codec.encode(json, toBase64);
|
2020-08-16 17:53:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const decompress = function(encoded) {
|
2021-07-29 01:48:38 +02:00
|
|
|
return lz4Codec.decode(encoded, fromBase64);
|
2020-08-16 17:53:55 +02:00
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2016-10-16 19:04:31 +02:00
|
|
|
// Cloud storage support is optional.
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.cloudStorageSupported !== true ) {
|
2016-10-16 19:04:31 +02:00
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:29:14 +02:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
|
|
|
case 'cloudGetOptions':
|
|
|
|
vAPI.cloud.getOptions(function(options) {
|
2021-07-29 01:48:38 +02:00
|
|
|
options.enabled = µb.userSettings.cloudStorageEnabled === true;
|
2015-08-11 21:29:14 +02:00
|
|
|
callback(options);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'cloudSetOptions':
|
|
|
|
vAPI.cloud.setOptions(request.options, callback);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 'cloudPull':
|
2020-08-16 17:53:55 +02:00
|
|
|
request.decode = decompress;
|
|
|
|
return vAPI.cloud.pull(request).then(result => {
|
2019-09-18 14:34:55 +02:00
|
|
|
callback(result);
|
|
|
|
});
|
2015-08-11 21:29:14 +02:00
|
|
|
|
|
|
|
case 'cloudPush':
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.hiddenSettings.cloudStorageCompression ) {
|
2020-08-16 17:53:55 +02:00
|
|
|
request.encode = compress;
|
|
|
|
}
|
|
|
|
return vAPI.cloud.push(request).then(result => {
|
2019-09-18 14:34:55 +02:00
|
|
|
callback(result);
|
|
|
|
});
|
2015-08-11 21:29:14 +02:00
|
|
|
|
2020-08-14 21:29:25 +02:00
|
|
|
case 'cloudUsed':
|
|
|
|
return vAPI.cloud.used(request.datakey).then(result => {
|
|
|
|
callback(result);
|
|
|
|
});
|
|
|
|
|
2015-08-11 21:29:14 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2019-09-01 18:43:12 +02:00
|
|
|
let response;
|
2015-08-11 21:29:14 +02:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
|
|
|
// For when cloud storage is disabled.
|
|
|
|
case 'cloudPull':
|
|
|
|
// fallthrough
|
|
|
|
case 'cloudPush':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'cloudWidget',
|
|
|
|
listener: onMessage,
|
|
|
|
privileged: true,
|
|
|
|
});
|
2014-12-31 23:26:17 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-12-31 23:26:17 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// dashboard
|
|
|
|
// privileged
|
2014-07-17 16:52:43 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2014-07-17 16:52:43 +02:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
// Settings
|
2019-09-15 13:58:28 +02:00
|
|
|
const getLocalData = async function() {
|
|
|
|
const data = Object.assign({}, µb.restoreBackupSettings);
|
|
|
|
data.storageUsed = await µb.getBytesInUse();
|
2019-09-17 21:15:01 +02:00
|
|
|
data.cloudStorageSupported = µb.cloudStorageSupported;
|
|
|
|
data.privacySettingsSupported = µb.privacySettingsSupported;
|
2019-09-15 13:58:28 +02:00
|
|
|
return data;
|
2015-03-07 05:36:09 +01:00
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const backupUserData = async function() {
|
2019-09-17 21:15:01 +02:00
|
|
|
const userFilters = await µb.loadUserFilters();
|
2019-09-15 13:58:28 +02:00
|
|
|
|
2019-06-25 17:57:14 +02:00
|
|
|
const userData = {
|
2015-04-16 18:14:43 +02:00
|
|
|
timeStamp: Date.now(),
|
|
|
|
version: vAPI.app.version,
|
2021-01-16 16:35:56 +01:00
|
|
|
userSettings:
|
|
|
|
µb.getModifiedSettings(µb.userSettings, µb.userSettingsDefault),
|
2017-01-26 16:17:38 +01:00
|
|
|
selectedFilterLists: µb.selectedFilterLists,
|
2021-01-16 16:35:56 +01:00
|
|
|
hiddenSettings:
|
|
|
|
µb.getModifiedSettings(µb.hiddenSettings, µb.hiddenSettingsDefault),
|
2019-06-25 17:57:14 +02:00
|
|
|
whitelist: µb.arrayFromWhitelist(µb.netWhitelist),
|
2021-07-29 01:48:38 +02:00
|
|
|
dynamicFilteringString: permanentFirewall.toString(),
|
|
|
|
urlFilteringString: permanentURLFiltering.toString(),
|
|
|
|
hostnameSwitchesString: permanentSwitches.toString(),
|
2019-09-15 13:58:28 +02:00
|
|
|
userFilters: userFilters.content,
|
2017-01-26 16:17:38 +01:00
|
|
|
};
|
2015-04-16 18:14:43 +02:00
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const filename = vAPI.i18n('aboutBackupFilename')
|
|
|
|
.replace('{{datetime}}', µb.dateNowToSensibleString())
|
|
|
|
.replace(/ +/g, '_');
|
|
|
|
µb.restoreBackupSettings.lastBackupFile = filename;
|
|
|
|
µb.restoreBackupSettings.lastBackupTime = Date.now();
|
|
|
|
vAPI.storage.set(µb.restoreBackupSettings);
|
2015-03-07 05:36:09 +01:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const localData = await getLocalData();
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
return { localData, userData };
|
2014-10-07 14:59:35 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 15:36:50 +02:00
|
|
|
const restoreUserData = async function(request) {
|
2019-03-28 14:17:47 +01:00
|
|
|
const userData = request.userData;
|
2014-10-11 22:44:56 +02:00
|
|
|
|
2020-12-17 14:12:06 +01:00
|
|
|
// https://github.com/LiCybora/NanoDefenderFirefox/issues/196
|
|
|
|
// Backup data could be from Chromium platform or from an older
|
|
|
|
// Firefox version.
|
|
|
|
if (
|
|
|
|
vAPI.webextFlavor.soup.has('firefox') &&
|
|
|
|
vAPI.app.intFromVersion(userData.version) <= 1031003011
|
|
|
|
) {
|
|
|
|
userData.hostnameSwitchesString += '\nno-csp-reports: * true';
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:30:12 +01:00
|
|
|
// List of external lists is meant to be a string.
|
|
|
|
if ( Array.isArray(userData.externalLists) ) {
|
|
|
|
userData.externalLists = userData.externalLists.join('\n');
|
2021-01-08 15:18:26 +01:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1102
|
2019-09-15 15:36:50 +02:00
|
|
|
// Ensure all currently cached assets are flushed from storage AND memory.
|
2021-07-29 01:48:38 +02:00
|
|
|
io.rmrf();
|
2015-03-27 14:50:31 +01:00
|
|
|
|
2014-10-07 16:46:10 +02:00
|
|
|
// If we are going to restore all, might as well wipe out clean local
|
2019-09-15 15:36:50 +02:00
|
|
|
// storages
|
|
|
|
await Promise.all([
|
2021-07-29 01:48:38 +02:00
|
|
|
cacheStorage.clear(),
|
2019-09-15 15:36:50 +02:00
|
|
|
vAPI.storage.clear(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Restore block stats
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.saveLocalSettings();
|
2019-09-15 15:36:50 +02:00
|
|
|
|
|
|
|
// Restore user data
|
|
|
|
vAPI.storage.set(userData.userSettings);
|
2020-10-03 18:34:21 +02:00
|
|
|
|
|
|
|
// Restore advanced settings.
|
2019-09-15 15:36:50 +02:00
|
|
|
let hiddenSettings = userData.hiddenSettings;
|
|
|
|
if ( hiddenSettings instanceof Object === false ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
hiddenSettings = µb.hiddenSettingsFromString(
|
2019-09-15 15:36:50 +02:00
|
|
|
userData.hiddenSettingsString || ''
|
|
|
|
);
|
|
|
|
}
|
2020-10-03 18:34:21 +02:00
|
|
|
// Discard unknown setting or setting with default value.
|
|
|
|
for ( const key in hiddenSettings ) {
|
|
|
|
if (
|
2020-10-07 03:02:51 +02:00
|
|
|
µb.hiddenSettingsDefault.hasOwnProperty(key) === false ||
|
|
|
|
hiddenSettings[key] === µb.hiddenSettingsDefault[key]
|
2020-10-03 18:34:21 +02:00
|
|
|
) {
|
|
|
|
delete hiddenSettings[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:36:50 +02:00
|
|
|
// Whitelist directives can be represented as an array or as a
|
|
|
|
// (eventually to be deprecated) string.
|
|
|
|
let whitelist = userData.whitelist;
|
|
|
|
if (
|
|
|
|
Array.isArray(whitelist) === false &&
|
|
|
|
typeof userData.netWhitelist === 'string' &&
|
|
|
|
userData.netWhitelist !== ''
|
|
|
|
) {
|
|
|
|
whitelist = userData.netWhitelist.split('\n');
|
|
|
|
}
|
|
|
|
vAPI.storage.set({
|
2020-10-03 18:34:21 +02:00
|
|
|
hiddenSettings,
|
2019-09-15 15:36:50 +02:00
|
|
|
netWhitelist: whitelist || [],
|
|
|
|
dynamicFilteringString: userData.dynamicFilteringString || '',
|
|
|
|
urlFilteringString: userData.urlFilteringString || '',
|
|
|
|
hostnameSwitchesString: userData.hostnameSwitchesString || '',
|
|
|
|
lastRestoreFile: request.file || '',
|
|
|
|
lastRestoreTime: Date.now(),
|
|
|
|
lastBackupFile: '',
|
|
|
|
lastBackupTime: 0
|
|
|
|
});
|
|
|
|
µb.saveUserFilters(userData.userFilters);
|
|
|
|
if ( Array.isArray(userData.selectedFilterLists) ) {
|
|
|
|
await µb.saveSelectedFilterLists(userData.selectedFilterLists);
|
|
|
|
}
|
|
|
|
|
|
|
|
vAPI.app.restart();
|
2014-10-07 14:59:35 +02:00
|
|
|
};
|
|
|
|
|
2018-07-24 15:17:18 +02:00
|
|
|
// Remove all stored data but keep global counts, people can become
|
|
|
|
// quite attached to numbers
|
2019-09-15 13:58:28 +02:00
|
|
|
const resetUserData = async function() {
|
|
|
|
await Promise.all([
|
2021-07-29 01:48:38 +02:00
|
|
|
cacheStorage.clear(),
|
2019-09-15 13:58:28 +02:00
|
|
|
vAPI.storage.clear(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
await µb.saveLocalSettings();
|
|
|
|
|
|
|
|
vAPI.app.restart();
|
2014-10-07 14:59:35 +02:00
|
|
|
};
|
|
|
|
|
2020-11-21 15:57:54 +01:00
|
|
|
// Filter lists
|
2019-05-20 00:31:12 +02:00
|
|
|
const prepListEntries = function(entries) {
|
|
|
|
for ( const k in entries ) {
|
2017-01-18 19:17:47 +01:00
|
|
|
if ( entries.hasOwnProperty(k) === false ) { continue; }
|
2019-05-20 00:31:12 +02:00
|
|
|
const entry = entries[k];
|
2016-03-06 16:51:06 +01:00
|
|
|
if ( typeof entry.supportURL === 'string' && entry.supportURL !== '' ) {
|
2021-07-25 16:55:35 +02:00
|
|
|
entry.supportName = hostnameFromURI(entry.supportURL);
|
2016-03-06 16:51:06 +01:00
|
|
|
} else if ( typeof entry.homeURL === 'string' && entry.homeURL !== '' ) {
|
2021-07-25 16:55:35 +02:00
|
|
|
const hn = hostnameFromURI(entry.homeURL);
|
2019-05-20 00:31:12 +02:00
|
|
|
entry.supportURL = `http://${hn}/`;
|
2021-07-25 16:55:35 +02:00
|
|
|
entry.supportName = domainFromHostname(hn);
|
2016-03-06 16:51:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-15 13:58:28 +02:00
|
|
|
const getLists = async function(callback) {
|
2019-05-20 00:31:12 +02:00
|
|
|
const r = {
|
2016-03-06 16:51:06 +01:00
|
|
|
autoUpdate: µb.userSettings.autoUpdate,
|
|
|
|
available: null,
|
|
|
|
cache: null,
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilterCount: cosmeticFilteringEngine.getFilterCount(),
|
2017-01-18 19:17:47 +01:00
|
|
|
current: µb.availableFilterLists,
|
2016-08-13 22:42:58 +02:00
|
|
|
ignoreGenericCosmeticFilters: µb.userSettings.ignoreGenericCosmeticFilters,
|
2021-07-29 01:48:38 +02:00
|
|
|
isUpdating: io.isUpdating(),
|
|
|
|
netFilterCount: staticNetFilteringEngine.getFilterCount(),
|
2017-01-22 22:05:16 +01:00
|
|
|
parseCosmeticFilters: µb.userSettings.parseAllABPHideFilters,
|
2021-12-30 15:24:38 +01:00
|
|
|
suspendUntilListsAreLoaded: µb.userSettings.suspendUntilListsAreLoaded,
|
2017-12-14 22:42:54 +01:00
|
|
|
userFiltersPath: µb.userFiltersPath
|
2016-03-06 16:51:06 +01:00
|
|
|
};
|
2019-09-15 13:58:28 +02:00
|
|
|
const [ lists, metadata ] = await Promise.all([
|
|
|
|
µb.getAvailableLists(),
|
2021-07-29 01:48:38 +02:00
|
|
|
io.metadata(),
|
2019-09-15 13:58:28 +02:00
|
|
|
]);
|
|
|
|
r.available = lists;
|
|
|
|
prepListEntries(r.available);
|
|
|
|
r.cache = metadata;
|
|
|
|
prepListEntries(r.cache);
|
|
|
|
callback(r);
|
2016-03-06 16:51:06 +01:00
|
|
|
};
|
|
|
|
|
2020-11-21 15:57:54 +01:00
|
|
|
// My filters
|
|
|
|
|
|
|
|
// TODO: also return origin of embedded frames?
|
|
|
|
const getOriginHints = function() {
|
2020-12-14 17:26:04 +01:00
|
|
|
const out = new Set();
|
2020-11-21 15:57:54 +01:00
|
|
|
for ( const tabId of µb.pageStores.keys() ) {
|
|
|
|
if ( tabId === -1 ) { continue; }
|
|
|
|
const tabContext = µb.tabContextManager.lookup(tabId);
|
|
|
|
if ( tabContext === null ) { continue; }
|
|
|
|
let { rootDomain, rootHostname } = tabContext;
|
|
|
|
if ( rootDomain.endsWith('-scheme') ) { continue; }
|
|
|
|
const isPunycode = rootHostname.includes('xn--');
|
2020-12-14 17:26:04 +01:00
|
|
|
out.add(isPunycode ? punycode.toUnicode(rootDomain) : rootDomain);
|
2020-11-21 15:57:54 +01:00
|
|
|
if ( rootHostname === rootDomain ) { continue; }
|
2020-12-14 17:26:04 +01:00
|
|
|
out.add(isPunycode ? punycode.toUnicode(rootHostname) : rootHostname);
|
2020-11-21 15:57:54 +01:00
|
|
|
}
|
2020-12-14 17:26:04 +01:00
|
|
|
return Array.from(out);
|
2020-11-21 15:57:54 +01:00
|
|
|
};
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
// My rules
|
2019-05-20 00:31:12 +02:00
|
|
|
const getRules = function() {
|
2016-03-06 16:51:06 +01:00
|
|
|
return {
|
2018-09-03 20:06:49 +02:00
|
|
|
permanentRules:
|
2021-07-29 01:48:38 +02:00
|
|
|
permanentFirewall.toArray().concat(
|
|
|
|
permanentSwitches.toArray(),
|
|
|
|
permanentURLFiltering.toArray()
|
2018-09-03 20:06:49 +02:00
|
|
|
),
|
|
|
|
sessionRules:
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionFirewall.toArray().concat(
|
|
|
|
sessionSwitches.toArray(),
|
|
|
|
sessionURLFiltering.toArray()
|
2020-11-18 14:01:00 +01:00
|
|
|
),
|
2021-08-23 15:42:27 +02:00
|
|
|
pslSelfie: publicSuffixList.toSelfie(),
|
2016-03-06 16:51:06 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-20 00:31:12 +02:00
|
|
|
const modifyRuleset = function(details) {
|
2018-09-03 20:06:49 +02:00
|
|
|
let swRuleset, hnRuleset, urlRuleset;
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( details.permanent ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
swRuleset = permanentSwitches;
|
|
|
|
hnRuleset = permanentFirewall;
|
|
|
|
urlRuleset = permanentURLFiltering;
|
2018-03-11 15:59:39 +01:00
|
|
|
} else {
|
2021-07-29 01:48:38 +02:00
|
|
|
swRuleset = sessionSwitches;
|
|
|
|
hnRuleset = sessionFirewall;
|
|
|
|
urlRuleset = sessionURLFiltering;
|
2018-03-11 15:59:39 +01:00
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
let toRemove = new Set(details.toRemove.trim().split(/\s*[\n\r]+\s*/));
|
|
|
|
for ( let rule of toRemove ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( rule === '' ) { continue; }
|
2018-09-03 20:06:49 +02:00
|
|
|
let parts = rule.split(/\s+/);
|
|
|
|
if ( hnRuleset.removeFromRuleParts(parts) === false ) {
|
|
|
|
if ( swRuleset.removeFromRuleParts(parts) === false ) {
|
|
|
|
urlRuleset.removeFromRuleParts(parts);
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
}
|
2018-09-03 20:06:49 +02:00
|
|
|
let toAdd = new Set(details.toAdd.trim().split(/\s*[\n\r]+\s*/));
|
|
|
|
for ( let rule of toAdd ) {
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( rule === '' ) { continue; }
|
2018-09-03 20:06:49 +02:00
|
|
|
let parts = rule.split(/\s+/);
|
|
|
|
if ( hnRuleset.addFromRuleParts(parts) === false ) {
|
|
|
|
if ( swRuleset.addFromRuleParts(parts) === false ) {
|
|
|
|
urlRuleset.addFromRuleParts(parts);
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
}
|
|
|
|
if ( details.permanent ) {
|
2018-09-03 20:06:49 +02:00
|
|
|
if ( swRuleset.changed ) {
|
|
|
|
µb.saveHostnameSwitches();
|
|
|
|
swRuleset.changed = false;
|
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( hnRuleset.changed ) {
|
|
|
|
µb.savePermanentFirewallRules();
|
|
|
|
hnRuleset.changed = false;
|
2016-03-06 16:51:06 +01:00
|
|
|
}
|
2018-03-11 15:59:39 +01:00
|
|
|
if ( urlRuleset.changed ) {
|
|
|
|
µb.savePermanentURLFilteringRules();
|
|
|
|
urlRuleset.changed = false;
|
2016-03-06 16:51:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-12 17:19:56 +02:00
|
|
|
// Shortcuts
|
2019-05-20 00:31:12 +02:00
|
|
|
const getShortcuts = function(callback) {
|
2018-07-20 19:52:14 +02:00
|
|
|
if ( µb.canUseShortcuts === false ) {
|
|
|
|
return callback([]);
|
2018-07-19 00:00:55 +02:00
|
|
|
}
|
2018-07-20 19:52:14 +02:00
|
|
|
|
|
|
|
vAPI.commands.getAll(commands => {
|
|
|
|
let response = [];
|
|
|
|
for ( let command of commands ) {
|
|
|
|
let desc = command.description;
|
|
|
|
let match = /^__MSG_(.+?)__$/.exec(desc);
|
|
|
|
if ( match !== null ) {
|
|
|
|
desc = vAPI.i18n(match[1]);
|
|
|
|
}
|
|
|
|
if ( desc === '' ) { continue; }
|
|
|
|
command.description = desc;
|
|
|
|
response.push(command);
|
|
|
|
}
|
|
|
|
callback(response);
|
|
|
|
});
|
2018-07-19 00:00:55 +02:00
|
|
|
};
|
|
|
|
|
2019-05-20 00:31:12 +02:00
|
|
|
const setShortcut = function(details) {
|
2018-07-20 19:52:14 +02:00
|
|
|
if ( µb.canUpdateShortcuts === false ) { return; }
|
2018-07-19 00:00:55 +02:00
|
|
|
if ( details.shortcut === undefined ) {
|
|
|
|
vAPI.commands.reset(details.name);
|
|
|
|
µb.commandShortcuts.delete(details.name);
|
|
|
|
} else {
|
|
|
|
vAPI.commands.update({ name: details.name, shortcut: details.shortcut });
|
|
|
|
µb.commandShortcuts.set(details.name, details.shortcut);
|
|
|
|
}
|
|
|
|
vAPI.storage.set({ commandShortcuts: Array.from(µb.commandShortcuts) });
|
|
|
|
};
|
|
|
|
|
2021-10-12 17:19:56 +02:00
|
|
|
// Support
|
|
|
|
const getSupportData = async function() {
|
|
|
|
const diffArrays = function(modified, original) {
|
|
|
|
const modifiedSet = new Set(modified);
|
|
|
|
const originalSet = new Set(original);
|
|
|
|
let added = [];
|
|
|
|
let removed = [];
|
|
|
|
for ( const item of modifiedSet ) {
|
|
|
|
if ( originalSet.has(item) ) { continue; }
|
|
|
|
added.push(item);
|
|
|
|
}
|
|
|
|
for ( const item of originalSet ) {
|
|
|
|
if ( modifiedSet.has(item) ) { continue; }
|
|
|
|
removed.push(item);
|
|
|
|
}
|
|
|
|
if ( added.length === 0 ) {
|
|
|
|
added = undefined;
|
|
|
|
}
|
|
|
|
if ( removed.length === 0 ) {
|
|
|
|
removed = undefined;
|
|
|
|
}
|
|
|
|
if ( added !== undefined || removed !== undefined ) {
|
|
|
|
return { added, removed };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 20:50:34 +02:00
|
|
|
const modifiedUserSettings = µb.getModifiedSettings(
|
2021-10-12 17:19:56 +02:00
|
|
|
µb.userSettings,
|
|
|
|
µb.userSettingsDefault
|
|
|
|
);
|
|
|
|
|
2021-10-15 20:50:34 +02:00
|
|
|
const modifiedHiddenSettings = µb.getModifiedSettings(
|
2021-10-12 17:19:56 +02:00
|
|
|
µb.hiddenSettings,
|
|
|
|
µb.hiddenSettingsDefault
|
|
|
|
);
|
|
|
|
|
|
|
|
let filterset = [];
|
|
|
|
const userFilters = await µb.loadUserFilters();
|
|
|
|
for ( const line of userFilters.content.split(/\s*\n+\s*/) ) {
|
|
|
|
if ( /^($|![^#])/.test(line) ) { continue; }
|
|
|
|
filterset.push(line);
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:54:38 +02:00
|
|
|
const lists = µb.availableFilterLists;
|
2021-10-12 17:19:56 +02:00
|
|
|
let defaultListset = {};
|
|
|
|
let addedListset = {};
|
|
|
|
let removedListset = {};
|
|
|
|
for ( const listKey in lists ) {
|
|
|
|
if ( lists.hasOwnProperty(listKey) === false ) { continue; }
|
|
|
|
const list = lists[listKey];
|
|
|
|
if ( list.content !== 'filters' ) { continue; }
|
|
|
|
const used = µb.selectedFilterLists.includes(listKey);
|
2021-10-22 20:59:41 +02:00
|
|
|
const listDetails = [];
|
2021-10-14 14:54:38 +02:00
|
|
|
if ( used ) {
|
|
|
|
if ( typeof list.entryCount === 'number' ) {
|
2021-10-22 20:59:41 +02:00
|
|
|
listDetails.push(`${list.entryCount}-${list.entryCount-list.entryUsedCount}`);
|
2021-10-14 14:54:38 +02:00
|
|
|
}
|
2021-11-15 16:46:29 +01:00
|
|
|
if ( typeof list.writeTime !== 'number' || list.writeTime === 0 ) {
|
|
|
|
listDetails.push('never');
|
|
|
|
} else {
|
2021-10-22 20:59:41 +02:00
|
|
|
const delta = (Date.now() - list.writeTime) / 1000 | 0;
|
|
|
|
const days = (delta / 86400) | 0;
|
|
|
|
const hours = (delta % 86400) / 3600 | 0;
|
|
|
|
const minutes = (delta % 3600) / 60 | 0;
|
|
|
|
const parts = [];
|
|
|
|
if ( days > 0 ) { parts.push(`${days}d`); }
|
|
|
|
if ( hours > 0 ) { parts.push(`${hours}h`); }
|
|
|
|
if ( minutes > 0 ) { parts.push(`${minutes}m`); }
|
|
|
|
if ( parts.length === 0 ) { parts.push('now'); }
|
|
|
|
listDetails.push(parts.join('.'));
|
2021-10-14 14:54:38 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-11 13:55:37 +01:00
|
|
|
if ( list.isDefault || listKey === µb.userFiltersPath ) {
|
2021-10-12 17:19:56 +02:00
|
|
|
if ( used ) {
|
2021-10-22 20:59:41 +02:00
|
|
|
defaultListset[listKey] = listDetails.join(', ');
|
2021-10-12 17:19:56 +02:00
|
|
|
} else {
|
|
|
|
removedListset[listKey] = null;
|
|
|
|
}
|
|
|
|
} else if ( used ) {
|
2021-10-22 20:59:41 +02:00
|
|
|
addedListset[listKey] = listDetails.join(', ');
|
2021-10-12 17:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( Object.keys(defaultListset).length === 0 ) {
|
|
|
|
defaultListset = undefined;
|
|
|
|
}
|
|
|
|
if ( Object.keys(addedListset).length === 0 ) {
|
|
|
|
addedListset = undefined;
|
2021-12-12 17:31:31 +01:00
|
|
|
} else if ( Object.keys(addedListset).length > 20 ) {
|
|
|
|
const added = Object.keys(addedListset);
|
|
|
|
const truncated = added.slice(20);
|
|
|
|
for ( const key of truncated ) {
|
|
|
|
delete addedListset[key];
|
|
|
|
}
|
|
|
|
addedListset[`[${truncated.length} lists not shown]`] = '[too many]';
|
2021-10-12 17:19:56 +02:00
|
|
|
}
|
|
|
|
if ( Object.keys(removedListset).length === 0 ) {
|
|
|
|
removedListset = undefined;
|
|
|
|
}
|
|
|
|
|
2021-11-14 14:13:43 +01:00
|
|
|
let browserFamily = (( ) => {
|
|
|
|
if ( vAPI.webextFlavor.soup.has('firefox') ) { return 'Firefox'; }
|
|
|
|
if ( vAPI.webextFlavor.soup.has('chromium') ) { return 'Chromium'; }
|
|
|
|
return 'Unknown';
|
|
|
|
})();
|
|
|
|
if ( vAPI.webextFlavor.soup.has('mobile') ) {
|
|
|
|
browserFamily += ' Mobile';
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:19:56 +02:00
|
|
|
return {
|
2021-11-14 14:13:43 +01:00
|
|
|
[`${vAPI.app.name}`]: `${vAPI.app.version}`,
|
|
|
|
[`${browserFamily}`]: `${vAPI.webextFlavor.major}`,
|
2021-11-11 18:49:55 +01:00
|
|
|
'filterset (summary)': {
|
|
|
|
network: staticNetFilteringEngine.getFilterCount(),
|
|
|
|
cosmetic: cosmeticFilteringEngine.getFilterCount(),
|
|
|
|
scriptlet: scriptletFilteringEngine.getFilterCount(),
|
|
|
|
html: htmlFilteringEngine.getFilterCount(),
|
|
|
|
},
|
2021-10-22 20:59:41 +02:00
|
|
|
'listset (total-discarded, last updated)': {
|
2021-10-12 17:19:56 +02:00
|
|
|
removed: removedListset,
|
2021-10-15 20:50:34 +02:00
|
|
|
added: addedListset,
|
|
|
|
default: defaultListset,
|
2021-10-12 17:19:56 +02:00
|
|
|
},
|
2021-11-11 18:49:55 +01:00
|
|
|
'filterset (user)': filterset,
|
2021-10-12 17:19:56 +02:00
|
|
|
trustedset: diffArrays(
|
|
|
|
µb.arrayFromWhitelist(µb.netWhitelist),
|
|
|
|
µb.netWhitelistDefault
|
|
|
|
),
|
|
|
|
switchRuleset: diffArrays(
|
|
|
|
sessionSwitches.toArray(),
|
|
|
|
µb.hostnameSwitchesDefault
|
|
|
|
),
|
2021-10-15 20:50:34 +02:00
|
|
|
hostRuleset: diffArrays(
|
|
|
|
sessionFirewall.toArray(),
|
|
|
|
µb.dynamicFilteringDefault
|
|
|
|
),
|
2021-10-12 17:19:56 +02:00
|
|
|
urlRuleset: diffArrays(
|
|
|
|
sessionURLFiltering.toArray(),
|
|
|
|
[]
|
|
|
|
),
|
2021-11-14 14:13:43 +01:00
|
|
|
modifiedUserSettings,
|
|
|
|
modifiedHiddenSettings,
|
2021-11-15 16:46:29 +01:00
|
|
|
supportStats: µb.supportStats,
|
2021-10-12 17:19:56 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-05-20 00:31:12 +02:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2014-06-24 00:42:43 +02:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'backupUserData':
|
2019-09-15 13:58:28 +02:00
|
|
|
return backupUserData().then(data => {
|
|
|
|
callback(data);
|
|
|
|
});
|
2015-03-07 05:36:09 +01:00
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'getLists':
|
|
|
|
return getLists(callback);
|
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'getLocalData':
|
2019-09-15 13:58:28 +02:00
|
|
|
return getLocalData().then(localData => {
|
|
|
|
callback(localData);
|
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2018-07-19 00:00:55 +02:00
|
|
|
case 'getShortcuts':
|
|
|
|
return getShortcuts(callback);
|
|
|
|
|
2021-10-12 17:19:56 +02:00
|
|
|
case 'getSupportData': {
|
|
|
|
getSupportData().then(response => {
|
|
|
|
callback(response);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'readUserFilters':
|
2019-09-15 13:58:28 +02:00
|
|
|
return µb.loadUserFilters().then(result => {
|
|
|
|
callback(result);
|
|
|
|
});
|
2016-03-06 16:51:06 +01:00
|
|
|
|
|
|
|
case 'writeUserFilters':
|
2019-09-15 13:58:28 +02:00
|
|
|
return µb.saveUserFilters(request.content).then(result => {
|
|
|
|
callback(result);
|
|
|
|
});
|
2016-03-06 16:51:06 +01:00
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
break;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2019-09-15 13:58:28 +02:00
|
|
|
let response;
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2021-01-06 17:39:24 +01:00
|
|
|
case 'dashboardConfig':
|
|
|
|
response = {
|
|
|
|
canUpdateShortcuts: µb.canUpdateShortcuts,
|
|
|
|
noDashboard: µb.noDashboard,
|
|
|
|
};
|
2018-07-19 00:00:55 +02:00
|
|
|
break;
|
|
|
|
|
2020-07-08 15:52:27 +02:00
|
|
|
case 'getAutoCompleteDetails':
|
2020-12-02 15:09:28 +01:00
|
|
|
response = {};
|
2020-12-02 19:07:29 +01:00
|
|
|
if ( (request.hintUpdateToken || 0) === 0 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
response.redirectResources = redirectEngine.getResourceDetails();
|
2020-12-02 15:09:28 +01:00
|
|
|
response.preparseDirectiveTokens = µb.preparseDirectives.getTokens();
|
|
|
|
response.preparseDirectiveHints = µb.preparseDirectives.getHints();
|
|
|
|
response.expertMode = µb.hiddenSettings.filterAuthorMode;
|
|
|
|
}
|
|
|
|
if ( request.hintUpdateToken !== µb.pageStoresToken ) {
|
|
|
|
response.originHints = getOriginHints();
|
|
|
|
response.hintUpdateToken = µb.pageStoresToken;
|
|
|
|
}
|
2020-06-15 15:15:13 +02:00
|
|
|
break;
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'getRules':
|
|
|
|
response = getRules();
|
|
|
|
break;
|
|
|
|
|
2018-03-11 15:59:39 +01:00
|
|
|
case 'modifyRuleset':
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/772
|
2021-07-29 01:48:38 +02:00
|
|
|
cosmeticFilteringEngine.removeFromSelectorCache('*');
|
2018-03-11 15:59:39 +01:00
|
|
|
modifyRuleset(request);
|
|
|
|
response = getRules();
|
|
|
|
break;
|
|
|
|
|
2017-01-18 19:17:47 +01:00
|
|
|
case 'purgeAllCaches':
|
|
|
|
if ( request.hard ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
io.remove(/./);
|
2017-01-18 19:17:47 +01:00
|
|
|
} else {
|
2021-07-29 01:48:38 +02:00
|
|
|
io.purge(/./, 'public_suffix_list.dat');
|
2017-01-18 19:17:47 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-03-06 16:51:06 +01:00
|
|
|
case 'purgeCache':
|
2021-07-29 01:48:38 +02:00
|
|
|
io.purge(request.assetKey);
|
|
|
|
io.remove('compiled/' + request.assetKey);
|
2016-03-06 16:51:06 +01:00
|
|
|
break;
|
|
|
|
|
2016-11-03 16:20:47 +01:00
|
|
|
case 'readHiddenSettings':
|
2020-04-28 17:07:00 +02:00
|
|
|
response = {
|
2021-01-05 18:16:50 +01:00
|
|
|
'default': µb.hiddenSettingsDefault,
|
|
|
|
'admin': µb.hiddenSettingsAdmin,
|
|
|
|
'current': µb.hiddenSettings,
|
2020-04-28 17:07:00 +02:00
|
|
|
};
|
2016-11-03 16:20:47 +01:00
|
|
|
break;
|
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'restoreUserData':
|
|
|
|
restoreUserData(request);
|
|
|
|
break;
|
2014-10-07 14:59:35 +02:00
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'resetUserData':
|
|
|
|
resetUserData();
|
|
|
|
break;
|
2016-03-06 16:51:06 +01:00
|
|
|
|
2018-07-19 00:00:55 +02:00
|
|
|
case 'setShortcut':
|
|
|
|
setShortcut(request);
|
|
|
|
break;
|
|
|
|
|
2016-11-03 16:20:47 +01:00
|
|
|
case 'writeHiddenSettings':
|
2018-03-19 15:25:03 +01:00
|
|
|
µb.changeHiddenSettings(µb.hiddenSettingsFromString(request.content));
|
2016-11-03 16:20:47 +01:00
|
|
|
break;
|
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'dashboard',
|
|
|
|
listener: onMessage,
|
|
|
|
privileged: true,
|
|
|
|
});
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2014-06-24 00:42:43 +02:00
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// loggerUI
|
|
|
|
// privileged
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2018-12-14 17:01:21 +01:00
|
|
|
const extensionOriginURL = vAPI.getURL('');
|
2021-02-19 14:38:50 +01:00
|
|
|
const documentBlockedURL = vAPI.getURL('document-blocked.html');
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2019-09-16 15:45:17 +02:00
|
|
|
const getLoggerData = async function(details, activeTabId, callback) {
|
2018-12-14 17:01:21 +01:00
|
|
|
const response = {
|
2019-09-24 23:05:03 +02:00
|
|
|
activeTabId,
|
2018-01-09 14:08:17 +01:00
|
|
|
colorBlind: µb.userSettings.colorBlindFriendly,
|
2021-07-29 01:48:38 +02:00
|
|
|
entries: logger.readAll(details.ownerId),
|
2019-09-24 23:05:03 +02:00
|
|
|
filterAuthorMode: µb.hiddenSettings.filterAuthorMode,
|
2018-12-17 19:54:17 +01:00
|
|
|
tabIdsToken: µb.pageStoresToken,
|
|
|
|
tooltips: µb.userSettings.tooltipsDisabled === false
|
2018-05-27 14:31:17 +02:00
|
|
|
};
|
|
|
|
if ( µb.pageStoresToken !== details.tabIdsToken ) {
|
2018-12-14 17:01:21 +01:00
|
|
|
const tabIds = new Map();
|
2021-02-19 14:38:50 +01:00
|
|
|
for ( const [ tabId, pageStore ] of µb.pageStores ) {
|
|
|
|
const { rawURL } = pageStore;
|
|
|
|
if (
|
|
|
|
rawURL.startsWith(extensionOriginURL) === false ||
|
|
|
|
rawURL.startsWith(documentBlockedURL)
|
|
|
|
) {
|
|
|
|
tabIds.set(tabId, pageStore.title);
|
|
|
|
}
|
2018-05-27 14:31:17 +02:00
|
|
|
}
|
|
|
|
response.tabIds = Array.from(tabIds);
|
|
|
|
}
|
|
|
|
if ( activeTabId ) {
|
2018-12-14 17:01:21 +01:00
|
|
|
const pageStore = µb.pageStoreFromTabId(activeTabId);
|
2021-02-19 14:38:50 +01:00
|
|
|
const rawURL = pageStore && pageStore.rawURL;
|
2018-05-27 14:31:17 +02:00
|
|
|
if (
|
2021-02-19 14:38:50 +01:00
|
|
|
rawURL === null ||
|
|
|
|
rawURL.startsWith(extensionOriginURL) &&
|
|
|
|
rawURL.startsWith(documentBlockedURL) === false
|
2018-05-27 14:31:17 +02:00
|
|
|
) {
|
|
|
|
response.activeTabId = undefined;
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 15:45:17 +02:00
|
|
|
if ( details.popupLoggerBoxChanged && vAPI.windows instanceof Object ) {
|
|
|
|
const tabs = await vAPI.tabs.query({
|
|
|
|
url: vAPI.getURL('/logger-ui.html?popup=1')
|
|
|
|
});
|
|
|
|
if ( tabs.length !== 0 ) {
|
|
|
|
const win = await vAPI.windows.get(tabs[0].windowId);
|
|
|
|
if ( win === null ) { return; }
|
|
|
|
vAPI.localStorage.setItem('popupLoggerBox', JSON.stringify({
|
|
|
|
left: win.left,
|
|
|
|
top: win.top,
|
|
|
|
width: win.width,
|
|
|
|
height: win.height,
|
|
|
|
}));
|
|
|
|
}
|
2018-12-14 17:01:21 +01:00
|
|
|
}
|
2018-05-27 14:31:17 +02:00
|
|
|
callback(response);
|
2018-01-09 14:08:17 +01:00
|
|
|
};
|
|
|
|
|
2018-12-14 17:01:21 +01:00
|
|
|
const getURLFilteringData = function(details) {
|
|
|
|
const colors = {};
|
|
|
|
const response = {
|
2015-05-21 20:15:17 +02:00
|
|
|
dirty: false,
|
|
|
|
colors: colors
|
|
|
|
};
|
2021-07-29 01:48:38 +02:00
|
|
|
const suf = sessionURLFiltering;
|
|
|
|
const puf = permanentURLFiltering;
|
2018-12-14 17:01:21 +01:00
|
|
|
const urls = details.urls;
|
|
|
|
const context = details.context;
|
|
|
|
const type = details.type;
|
|
|
|
for ( const url of urls ) {
|
|
|
|
const colorEntry = colors[url] = { r: 0, own: false };
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( suf.evaluateZ(context, url, type).r !== 0 ) {
|
|
|
|
colorEntry.r = suf.r;
|
2018-12-14 17:01:21 +01:00
|
|
|
colorEntry.own = suf.r !== 0 &&
|
|
|
|
suf.context === context &&
|
|
|
|
suf.url === url &&
|
|
|
|
suf.type === type;
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
2018-12-14 17:01:21 +01:00
|
|
|
if ( response.dirty ) { continue; }
|
2015-05-21 20:15:17 +02:00
|
|
|
puf.evaluateZ(context, url, type);
|
2018-12-14 17:01:21 +01:00
|
|
|
response.dirty = colorEntry.own !== (
|
|
|
|
puf.r !== 0 &&
|
|
|
|
puf.context === context &&
|
|
|
|
puf.url === url &&
|
|
|
|
puf.type === type
|
|
|
|
);
|
2015-05-21 20:15:17 +02:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2019-09-24 23:05:03 +02:00
|
|
|
const compileTemporaryException = function(filter) {
|
2021-07-25 16:55:35 +02:00
|
|
|
const parser = new StaticFilteringParser();
|
2020-06-13 14:48:56 +02:00
|
|
|
parser.analyze(filter);
|
2021-03-13 14:53:34 +01:00
|
|
|
if ( parser.shouldDiscard() ) { return; }
|
2021-07-29 01:48:38 +02:00
|
|
|
return staticExtFilteringEngine.compileTemporary(parser);
|
2019-09-24 23:05:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const toggleTemporaryException = function(details) {
|
2021-03-13 14:53:34 +01:00
|
|
|
const result = compileTemporaryException(details.filter);
|
|
|
|
if ( result === undefined ) { return false; }
|
|
|
|
const { session, selector } = result;
|
2019-09-24 23:05:03 +02:00
|
|
|
if ( session.has(1, selector) ) {
|
|
|
|
session.remove(1, selector);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
session.add(1, selector);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasTemporaryException = function(details) {
|
2021-03-13 14:53:34 +01:00
|
|
|
const result = compileTemporaryException(details.filter);
|
|
|
|
if ( result === undefined ) { return false; }
|
|
|
|
const { session, selector } = result;
|
2019-09-24 23:05:03 +02:00
|
|
|
return session && session.has(1, selector);
|
|
|
|
};
|
|
|
|
|
2018-12-14 17:01:21 +01:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2015-01-06 14:01:15 +01:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'readAll':
|
2018-01-08 20:29:39 +01:00
|
|
|
if (
|
2021-07-29 01:48:38 +02:00
|
|
|
logger.ownerId !== undefined &&
|
|
|
|
logger.ownerId !== request.ownerId
|
2018-01-08 20:29:39 +01:00
|
|
|
) {
|
2019-09-16 15:45:17 +02:00
|
|
|
return callback({ unavailable: true });
|
2018-01-08 20:29:39 +01:00
|
|
|
}
|
2019-09-16 15:45:17 +02:00
|
|
|
vAPI.tabs.getCurrent().then(tab => {
|
2018-05-27 14:31:17 +02:00
|
|
|
getLoggerData(request, tab && tab.id, callback);
|
2018-01-09 14:08:17 +01:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
2015-05-19 19:59:53 +02:00
|
|
|
break;
|
2018-01-09 14:08:17 +01:00
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2018-01-09 14:08:17 +01:00
|
|
|
// Sync
|
2018-12-14 17:01:21 +01:00
|
|
|
let response;
|
2018-01-09 14:08:17 +01:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2019-09-24 23:05:03 +02:00
|
|
|
case 'hasTemporaryException':
|
|
|
|
response = hasTemporaryException(request);
|
|
|
|
break;
|
|
|
|
|
2018-01-08 20:29:39 +01:00
|
|
|
case 'releaseView':
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( request.ownerId === logger.ownerId ) {
|
|
|
|
logger.ownerId = undefined;
|
2018-01-08 20:29:39 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-21 20:15:17 +02:00
|
|
|
case 'saveURLFilteringRules':
|
2021-07-29 01:48:38 +02:00
|
|
|
response = permanentURLFiltering.copyRules(
|
|
|
|
sessionURLFiltering,
|
2015-05-22 14:05:55 +02:00
|
|
|
request.context,
|
|
|
|
request.urls,
|
|
|
|
request.type
|
|
|
|
);
|
|
|
|
if ( response ) {
|
2015-05-21 20:15:17 +02:00
|
|
|
µb.savePermanentURLFilteringRules();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'setURLFilteringRule':
|
|
|
|
µb.toggleURLFilteringRule(request);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'getURLFilteringData':
|
|
|
|
response = getURLFilteringData(request);
|
|
|
|
break;
|
|
|
|
|
2019-09-24 23:05:03 +02:00
|
|
|
case 'toggleTemporaryException':
|
|
|
|
response = toggleTemporaryException(request);
|
|
|
|
break;
|
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'loggerUI',
|
|
|
|
listener: onMessage,
|
|
|
|
privileged: true,
|
|
|
|
});
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2014-06-24 00:42:43 +02:00
|
|
|
/******************************************************************************/
|
2015-03-09 17:57:52 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// documentBlocked
|
|
|
|
// privileged
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2019-05-22 00:56:59 +02:00
|
|
|
const onMessage = function(request, sender, callback) {
|
2020-11-12 18:14:59 +01:00
|
|
|
const tabId = sender.tabId || 0;
|
2019-05-22 00:56:59 +02:00
|
|
|
|
2015-03-26 00:28:22 +01:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
break;
|
2015-03-26 00:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2018-10-29 13:56:51 +01:00
|
|
|
let response;
|
2015-03-26 00:28:22 +01:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2019-05-22 00:56:59 +02:00
|
|
|
case 'closeThisTab':
|
|
|
|
vAPI.tabs.remove(tabId);
|
|
|
|
break;
|
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
case 'temporarilyWhitelistDocument':
|
2021-07-29 01:48:38 +02:00
|
|
|
webRequest.strictBlockBypass(request.hostname);
|
2015-05-19 19:59:53 +02:00
|
|
|
break;
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2015-05-19 19:59:53 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
2015-03-26 00:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'documentBlocked',
|
|
|
|
listener: onMessage,
|
|
|
|
privileged: true,
|
|
|
|
});
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2015-03-09 17:57:52 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2015-05-25 00:50:09 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// Channel:
|
|
|
|
// scriptlets
|
|
|
|
// unprivileged
|
2015-05-25 00:50:09 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
{
|
|
|
|
// >>>>> start of local scope
|
2015-05-25 00:50:09 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const logCosmeticFilters = function(tabId, details) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled === false ) { return; }
|
2018-12-13 18:30:54 +01:00
|
|
|
|
|
|
|
const filter = { source: 'cosmetic', raw: '' };
|
|
|
|
const fctxt = µb.filteringContext.duplicate();
|
|
|
|
fctxt.fromTabId(tabId)
|
|
|
|
.setRealm('cosmetic')
|
|
|
|
.setType('dom')
|
|
|
|
.setURL(details.frameURL)
|
|
|
|
.setDocOriginFromURL(details.frameURL)
|
|
|
|
.setFilter(filter);
|
|
|
|
for ( const selector of details.matchedSelectors.sort() ) {
|
2019-05-16 19:44:49 +02:00
|
|
|
filter.raw = selector;
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.toLogger();
|
2015-05-25 00:50:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-11 16:40:34 +02:00
|
|
|
const logCSPViolations = function(pageStore, request) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled === false || pageStore === null ) {
|
2019-05-11 16:40:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( request.violations.length === 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fctxt = µb.filteringContext.duplicate();
|
|
|
|
fctxt.fromTabId(pageStore.tabId)
|
|
|
|
.setRealm('network')
|
|
|
|
.setDocOriginFromURL(request.docURL)
|
|
|
|
.setURL(request.docURL);
|
|
|
|
|
|
|
|
let cspData = pageStore.extraData.get('cspData');
|
|
|
|
if ( cspData === undefined ) {
|
|
|
|
cspData = new Map();
|
|
|
|
|
2019-09-28 17:30:26 +02:00
|
|
|
const staticDirectives =
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'csp');
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
if ( staticDirectives !== undefined ) {
|
|
|
|
for ( const directive of staticDirectives ) {
|
|
|
|
if ( directive.result !== 1 ) { continue; }
|
|
|
|
cspData.set(directive.value, directive.logData());
|
|
|
|
}
|
2019-05-11 16:40:34 +02:00
|
|
|
}
|
2019-09-28 17:30:26 +02:00
|
|
|
|
2019-05-11 16:40:34 +02:00
|
|
|
fctxt.type = 'inline-script';
|
|
|
|
fctxt.filter = undefined;
|
|
|
|
if ( pageStore.filterRequest(fctxt) === 1 ) {
|
|
|
|
cspData.set(µb.cspNoInlineScript, fctxt.filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctxt.type = 'script';
|
|
|
|
fctxt.filter = undefined;
|
|
|
|
if ( pageStore.filterScripting(fctxt, true) === 1 ) {
|
|
|
|
cspData.set(µb.cspNoScripting, fctxt.filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctxt.type = 'inline-font';
|
|
|
|
fctxt.filter = undefined;
|
|
|
|
if ( pageStore.filterRequest(fctxt) === 1 ) {
|
|
|
|
cspData.set(µb.cspNoInlineFont, fctxt.filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( cspData.size === 0 ) { return false; }
|
|
|
|
|
|
|
|
pageStore.extraData.set('cspData', cspData);
|
|
|
|
}
|
|
|
|
|
|
|
|
const typeMap = logCSPViolations.policyDirectiveToTypeMap;
|
|
|
|
for ( const json of request.violations ) {
|
|
|
|
const violation = JSON.parse(json);
|
|
|
|
let type = typeMap.get(violation.directive);
|
|
|
|
if ( type === undefined ) { continue; }
|
|
|
|
const logData = cspData.get(violation.policy);
|
|
|
|
if ( logData === undefined ) { continue; }
|
|
|
|
if ( /^[\w.+-]+:\/\//.test(violation.url) === false ) {
|
|
|
|
violation.url = request.docURL;
|
|
|
|
if ( type === 'script' ) { type = 'inline-script'; }
|
|
|
|
else if ( type === 'font' ) { type = 'inline-font'; }
|
|
|
|
}
|
2020-11-20 13:14:02 +01:00
|
|
|
// The resource was blocked as a result of applying a CSP directive
|
|
|
|
// elsewhere rather than to the resource itself.
|
|
|
|
logData.modifier = undefined;
|
2019-05-11 16:40:34 +02:00
|
|
|
fctxt.setURL(violation.url)
|
|
|
|
.setType(type)
|
|
|
|
.setFilter(logData)
|
|
|
|
.toLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
logCSPViolations.policyDirectiveToTypeMap = new Map([
|
|
|
|
[ 'img-src', 'image' ],
|
|
|
|
[ 'connect-src', 'xmlhttprequest' ],
|
|
|
|
[ 'font-src', 'font' ],
|
|
|
|
[ 'frame-src', 'sub_frame' ],
|
|
|
|
[ 'media-src', 'media' ],
|
|
|
|
[ 'object-src', 'object' ],
|
|
|
|
[ 'script-src', 'script' ],
|
|
|
|
[ 'script-src-attr', 'script' ],
|
|
|
|
[ 'script-src-elem', 'script' ],
|
|
|
|
[ 'style-src', 'stylesheet' ],
|
|
|
|
[ 'style-src-attr', 'stylesheet' ],
|
|
|
|
[ 'style-src-elem', 'stylesheet' ],
|
|
|
|
]);
|
|
|
|
|
|
|
|
const onMessage = function(request, sender, callback) {
|
2020-11-12 18:14:59 +01:00
|
|
|
const tabId = sender.tabId || 0;
|
2018-12-21 20:16:17 +01:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
2015-06-26 06:08:41 +02:00
|
|
|
|
2015-05-25 00:50:09 +02:00
|
|
|
// Async
|
|
|
|
switch ( request.what ) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync
|
2019-05-11 16:40:34 +02:00
|
|
|
let response;
|
2015-05-25 00:50:09 +02:00
|
|
|
|
|
|
|
switch ( request.what ) {
|
2018-12-21 20:16:17 +01:00
|
|
|
case 'inlinescriptFound':
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled && pageStore !== null ) {
|
2018-12-21 20:16:17 +01:00
|
|
|
const fctxt = µb.filteringContext.duplicate();
|
|
|
|
fctxt.fromTabId(tabId)
|
|
|
|
.setType('inline-script')
|
|
|
|
.setURL(request.docURL)
|
|
|
|
.setDocOriginFromURL(request.docURL);
|
|
|
|
if ( pageStore.filterRequest(fctxt) === 0 ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
2018-12-21 20:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-25 00:50:09 +02:00
|
|
|
case 'logCosmeticFilteringData':
|
|
|
|
logCosmeticFilters(tabId, request);
|
|
|
|
break;
|
|
|
|
|
2019-05-11 16:40:34 +02:00
|
|
|
case 'securityPolicyViolation':
|
|
|
|
response = logCSPViolations(pageStore, request);
|
|
|
|
break;
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
case 'temporarilyAllowLargeMediaElement':
|
|
|
|
if ( pageStore !== null ) {
|
2020-10-18 16:07:46 +02:00
|
|
|
pageStore.allowLargeMediaElementsUntil = Date.now() + 5000;
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-09-13 14:01:53 +02:00
|
|
|
case 'subscribeTo':
|
2021-11-10 14:49:28 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1797
|
|
|
|
if ( /^(file|https?):\/\//.test(request.location) === false ) { break; }
|
2020-09-13 14:01:53 +02:00
|
|
|
const url = encodeURIComponent(request.location);
|
|
|
|
const title = encodeURIComponent(request.title);
|
2020-09-13 17:44:42 +02:00
|
|
|
const hash = µb.selectedFilterLists.indexOf(request.location) !== -1
|
2020-09-13 14:01:53 +02:00
|
|
|
? '#subscribed'
|
|
|
|
: '';
|
|
|
|
vAPI.tabs.open({
|
|
|
|
url: `/asset-viewer.html?url=${url}&title=${title}&subscribe=1${hash}`,
|
|
|
|
select: true,
|
|
|
|
});
|
2016-01-17 19:30:43 +01:00
|
|
|
break;
|
|
|
|
|
2015-05-25 00:50:09 +02:00
|
|
|
default:
|
|
|
|
return vAPI.messaging.UNHANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(response);
|
|
|
|
};
|
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
vAPI.messaging.listen({
|
|
|
|
name: 'scriptlets',
|
|
|
|
listener: onMessage,
|
|
|
|
});
|
2015-05-25 00:50:09 +02:00
|
|
|
|
2019-09-01 18:43:12 +02:00
|
|
|
// <<<<< end of local scope
|
|
|
|
}
|
2015-05-25 00:50:09 +02:00
|
|
|
|
2015-06-26 06:08:41 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|