2022-09-13 23:44:24 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2022-present Raymond Hill
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* jshint esversion:11 */
|
|
|
|
|
2022-09-07 16:15:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-20 14:24:01 +02:00
|
|
|
import {
|
|
|
|
browser,
|
|
|
|
dnr,
|
|
|
|
runtime,
|
|
|
|
} from './ext.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CURRENT_CONFIG_BASE_RULE_ID,
|
|
|
|
getRulesetDetails,
|
|
|
|
getDynamicRules,
|
|
|
|
defaultRulesetsFromLanguage,
|
|
|
|
enableRulesets,
|
|
|
|
getEnabledRulesetsStats,
|
|
|
|
updateRegexRules,
|
|
|
|
} from './ruleset-manager.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getInjectableCount,
|
|
|
|
registerInjectable,
|
|
|
|
} from './scripting-manager.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
matchesTrustedSiteDirective,
|
|
|
|
toggleTrustedSiteDirective,
|
|
|
|
} from './trusted-sites.js';
|
2022-09-07 16:15:36 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
const rulesetConfig = {
|
|
|
|
version: '',
|
|
|
|
enabledRulesets: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function getCurrentVersion() {
|
|
|
|
return runtime.getManifest().version;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadRulesetConfig() {
|
2022-09-16 21:56:35 +02:00
|
|
|
const dynamicRuleMap = await getDynamicRules();
|
2022-09-13 23:44:24 +02:00
|
|
|
const configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID);
|
|
|
|
if ( configRule === undefined ) {
|
|
|
|
rulesetConfig.enabledRulesets = await defaultRulesetsFromLanguage();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const match = /^\|\|example.invalid\/([^\/]+)\/(?:([^\/]+)\/)?/.exec(
|
|
|
|
configRule.condition.urlFilter
|
|
|
|
);
|
|
|
|
if ( match === null ) { return; }
|
|
|
|
|
|
|
|
rulesetConfig.version = match[1];
|
|
|
|
if ( match[2] ) {
|
|
|
|
rulesetConfig.enabledRulesets =
|
|
|
|
decodeURIComponent(match[2] || '').split(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveRulesetConfig() {
|
2022-09-16 21:56:35 +02:00
|
|
|
const dynamicRuleMap = await getDynamicRules();
|
2022-09-13 23:44:24 +02:00
|
|
|
let configRule = dynamicRuleMap.get(CURRENT_CONFIG_BASE_RULE_ID);
|
|
|
|
if ( configRule === undefined ) {
|
|
|
|
configRule = {
|
|
|
|
id: CURRENT_CONFIG_BASE_RULE_ID,
|
|
|
|
action: {
|
|
|
|
type: 'allow',
|
|
|
|
},
|
|
|
|
condition: {
|
|
|
|
urlFilter: '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const version = rulesetConfig.version;
|
|
|
|
const enabledRulesets = encodeURIComponent(rulesetConfig.enabledRulesets.join(' '));
|
|
|
|
const urlFilter = `||example.invalid/${version}/${enabledRulesets}/`;
|
|
|
|
if ( urlFilter === configRule.condition.urlFilter ) { return; }
|
|
|
|
configRule.condition.urlFilter = urlFilter;
|
|
|
|
|
|
|
|
return dnr.updateDynamicRules({
|
|
|
|
addRules: [ configRule ],
|
|
|
|
removeRuleIds: [ CURRENT_CONFIG_BASE_RULE_ID ],
|
|
|
|
});
|
|
|
|
}
|
2022-09-07 16:15:36 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
async function hasGreatPowers(origin) {
|
|
|
|
return browser.permissions.contains({
|
|
|
|
origins: [ `${origin}/*` ]
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
2022-09-15 19:14:08 +02:00
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
function grantGreatPowers(hostname) {
|
|
|
|
return browser.permissions.request({
|
|
|
|
origins: [
|
|
|
|
`*://${hostname}/*`,
|
|
|
|
]
|
2022-09-07 16:15:36 +02:00
|
|
|
});
|
2022-09-15 19:14:08 +02:00
|
|
|
}
|
2022-09-07 16:15:36 +02:00
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
function revokeGreatPowers(hostname) {
|
|
|
|
return browser.permissions.remove({
|
|
|
|
origins: [
|
|
|
|
`*://${hostname}/*`,
|
|
|
|
]
|
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
}
|
2022-09-07 16:15:36 +02:00
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
function onMessage(request, sender, callback) {
|
2022-09-13 23:44:24 +02:00
|
|
|
switch ( request.what ) {
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
case 'applyRulesets': {
|
|
|
|
enableRulesets(request.enabledRulesets).then(( ) => {
|
|
|
|
rulesetConfig.enabledRulesets = request.enabledRulesets;
|
2022-09-18 15:31:44 +02:00
|
|
|
return Promise.all([
|
|
|
|
saveRulesetConfig(),
|
|
|
|
registerInjectable(),
|
|
|
|
]);
|
2022-09-15 19:14:08 +02:00
|
|
|
}).then(( ) => {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
case 'getRulesetData': {
|
2022-09-16 21:56:35 +02:00
|
|
|
Promise.all([
|
|
|
|
getRulesetDetails(),
|
|
|
|
dnr.getEnabledRulesets(),
|
|
|
|
]).then(results => {
|
|
|
|
const [ rulesetDetails, enabledRulesets ] = results;
|
2022-09-13 23:44:24 +02:00
|
|
|
callback({
|
|
|
|
enabledRulesets,
|
|
|
|
rulesetDetails: Array.from(rulesetDetails.values()),
|
2022-09-07 16:15:36 +02:00
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
case 'grantGreatPowers':
|
|
|
|
grantGreatPowers(request.hostname).then(granted => {
|
2022-09-16 21:56:35 +02:00
|
|
|
console.info(`Granted uBOL great powers on ${request.hostname}: ${granted}`);
|
2022-09-15 19:14:08 +02:00
|
|
|
callback(granted);
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 'popupPanelData': {
|
|
|
|
Promise.all([
|
|
|
|
matchesTrustedSiteDirective(request),
|
2022-09-15 19:14:08 +02:00
|
|
|
hasGreatPowers(request.origin),
|
2022-09-13 23:44:24 +02:00
|
|
|
getEnabledRulesetsStats(),
|
2022-09-17 14:26:41 +02:00
|
|
|
getInjectableCount(request.origin),
|
2022-09-13 23:44:24 +02:00
|
|
|
]).then(results => {
|
|
|
|
callback({
|
|
|
|
isTrusted: results[0],
|
2022-09-15 19:14:08 +02:00
|
|
|
hasGreatPowers: results[1],
|
|
|
|
rulesetDetails: results[2],
|
2022-09-17 14:26:41 +02:00
|
|
|
injectableCount: results[3],
|
2022-09-07 16:15:36 +02:00
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
case 'revokeGreatPowers':
|
|
|
|
revokeGreatPowers(request.hostname).then(removed => {
|
2022-09-16 21:56:35 +02:00
|
|
|
console.info(`Revoked great powers from uBOL on ${request.hostname}: ${removed}`);
|
2022-09-15 19:14:08 +02:00
|
|
|
callback(removed);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
case 'toggleTrustedSiteDirective': {
|
|
|
|
toggleTrustedSiteDirective(request).then(response => {
|
2022-09-20 14:24:01 +02:00
|
|
|
registerInjectable().then(( ) => {
|
|
|
|
callback(response);
|
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
async function onPermissionsChanged() {
|
2022-09-16 21:56:35 +02:00
|
|
|
await registerInjectable();
|
2022-09-15 19:14:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
async function start() {
|
|
|
|
await loadRulesetConfig();
|
|
|
|
await enableRulesets(rulesetConfig.enabledRulesets);
|
|
|
|
|
|
|
|
// We need to update the regex rules only when ruleset version changes.
|
|
|
|
const currentVersion = getCurrentVersion();
|
|
|
|
if ( currentVersion !== rulesetConfig.version ) {
|
|
|
|
console.log(`Version change: ${rulesetConfig.version} => ${currentVersion}`);
|
2022-09-18 15:31:44 +02:00
|
|
|
updateRegexRules().then(( ) => {
|
|
|
|
rulesetConfig.version = currentVersion;
|
|
|
|
saveRulesetConfig();
|
|
|
|
});
|
2022-09-15 19:14:08 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 15:31:44 +02:00
|
|
|
// Unsure whether the browser remembers correctly registered css/scripts
|
|
|
|
// after we quit the browser. For now uBOL will check unconditionally at
|
|
|
|
// launch time whether content css/scripts are properly registered.
|
|
|
|
registerInjectable();
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
const enabledRulesets = await dnr.getEnabledRulesets();
|
|
|
|
console.log(`Enabled rulesets: ${enabledRulesets}`);
|
|
|
|
|
|
|
|
dnr.getAvailableStaticRuleCount().then(count => {
|
|
|
|
console.log(`Available static rule count: ${count}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
dnr.setExtensionActionOptions({ displayActionCountAsBadgeText: true });
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
(async ( ) => {
|
|
|
|
await start();
|
|
|
|
|
2022-09-15 19:14:08 +02:00
|
|
|
runtime.onMessage.addListener(onMessage);
|
|
|
|
|
|
|
|
browser.permissions.onAdded.addListener(onPermissionsChanged);
|
|
|
|
browser.permissions.onRemoved.addListener(onPermissionsChanged);
|
2022-09-07 16:15:36 +02:00
|
|
|
})();
|