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,
|
2022-09-30 01:51:33 +02:00
|
|
|
getEnabledRulesetsDetails,
|
|
|
|
updateDynamicRules,
|
2022-09-20 14:24:01 +02:00
|
|
|
} from './ruleset-manager.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getInjectableCount,
|
2022-09-28 01:51:38 +02:00
|
|
|
registerInjectables,
|
2022-09-20 14:24:01 +02:00
|
|
|
} from './scripting-manager.js';
|
|
|
|
|
|
|
|
import {
|
2022-10-10 18:28:24 +02:00
|
|
|
getFilteringMode,
|
|
|
|
setFilteringMode,
|
|
|
|
getDefaultFilteringMode,
|
|
|
|
setDefaultFilteringMode,
|
|
|
|
syncWithDemotedOrigins,
|
|
|
|
} from './mode-manager.js';
|
2022-09-07 16:15:36 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
const rulesetConfig = {
|
|
|
|
version: '',
|
2022-10-10 18:28:24 +02:00
|
|
|
enabledRulesets: [ 'default' ],
|
|
|
|
autoReload: 1,
|
2022-09-30 15:18:52 +02:00
|
|
|
firstRun: false,
|
2022-09-13 23:44:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
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();
|
2022-09-30 15:18:52 +02:00
|
|
|
rulesetConfig.firstRun = true;
|
2022-09-13 23:44:24 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-10 18:28:24 +02:00
|
|
|
let rawConfig;
|
|
|
|
try {
|
|
|
|
rawConfig = JSON.parse(self.atob(configRule.condition.urlFilter));
|
|
|
|
} catch(ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// New format
|
|
|
|
if ( Array.isArray(rawConfig) ) {
|
|
|
|
rulesetConfig.version = rawConfig[0];
|
|
|
|
rulesetConfig.enabledRulesets = rawConfig[1];
|
|
|
|
rulesetConfig.autoReload = rawConfig[2];
|
|
|
|
return;
|
|
|
|
}
|
2022-09-13 23:44:24 +02:00
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
// Legacy format. TODO: remove when next new format is widely in use.
|
2022-09-27 13:46:24 +02:00
|
|
|
const match = /^\|\|(?:example|ubolite)\.invalid\/([^\/]+)\/(?:([^\/]+)\/)?/.exec(
|
2022-09-13 23:44:24 +02:00
|
|
|
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: '',
|
2022-10-10 18:28:24 +02:00
|
|
|
initiatorDomains: [
|
|
|
|
'ubolite.invalid',
|
|
|
|
],
|
|
|
|
resourceTypes: [
|
|
|
|
'main_frame',
|
|
|
|
],
|
2022-09-13 23:44:24 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-10-10 18:28:24 +02:00
|
|
|
const rawConfig = [
|
|
|
|
rulesetConfig.version,
|
|
|
|
rulesetConfig.enabledRulesets,
|
|
|
|
rulesetConfig.autoReload,
|
|
|
|
];
|
|
|
|
const urlFilter = self.btoa(JSON.stringify(rawConfig));
|
2022-09-13 23:44:24 +02:00
|
|
|
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-30 01:51:33 +02:00
|
|
|
async function hasGreatPowers(origin) {
|
|
|
|
if ( /^https?:\/\//.test(origin) === false ) { return false; }
|
2022-09-15 19:14:08 +02:00
|
|
|
return browser.permissions.contains({
|
2022-09-27 13:46:24 +02:00
|
|
|
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-28 01:51:38 +02:00
|
|
|
function hasOmnipotence() {
|
|
|
|
return browser.permissions.contains({
|
|
|
|
origins: [ '<all_urls>' ],
|
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-28 01:51:38 +02:00
|
|
|
function onPermissionsRemoved(permissions) {
|
2022-09-30 01:51:33 +02:00
|
|
|
if ( permissions.origins?.includes('<all_urls>') ) {
|
|
|
|
updateDynamicRules();
|
|
|
|
}
|
2022-10-10 18:28:24 +02:00
|
|
|
syncWithDemotedOrigins(permissions.origins).then(( ) => {
|
|
|
|
registerInjectables(permissions.origins);
|
|
|
|
});
|
2022-09-27 13:46:24 +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-28 16:20:57 +02:00
|
|
|
return saveRulesetConfig();
|
2022-09-15 19:14:08 +02:00
|
|
|
}).then(( ) => {
|
2022-09-28 16:20:57 +02:00
|
|
|
registerInjectables();
|
2022-09-15 19:14:08 +02:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
case 'getOptionsPageData': {
|
2022-09-16 21:56:35 +02:00
|
|
|
Promise.all([
|
2022-10-10 18:28:24 +02:00
|
|
|
getDefaultFilteringMode(),
|
2022-09-16 21:56:35 +02:00
|
|
|
getRulesetDetails(),
|
|
|
|
dnr.getEnabledRulesets(),
|
|
|
|
]).then(results => {
|
2022-10-10 18:28:24 +02:00
|
|
|
const [
|
|
|
|
defaultFilteringMode,
|
|
|
|
rulesetDetails,
|
|
|
|
enabledRulesets,
|
|
|
|
] = results;
|
2022-09-13 23:44:24 +02:00
|
|
|
callback({
|
2022-10-10 18:28:24 +02:00
|
|
|
defaultFilteringMode,
|
2022-09-13 23:44:24 +02:00
|
|
|
enabledRulesets,
|
|
|
|
rulesetDetails: Array.from(rulesetDetails.values()),
|
2022-10-10 18:28:24 +02:00
|
|
|
autoReload: rulesetConfig.autoReload === 1,
|
2022-09-30 15:18:52 +02:00
|
|
|
firstRun: rulesetConfig.firstRun,
|
2022-09-07 16:15:36 +02:00
|
|
|
});
|
2022-09-30 15:18:52 +02:00
|
|
|
rulesetConfig.firstRun = false;
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
case 'setAutoReload':
|
|
|
|
rulesetConfig.autoReload = request.state ? 1 : 0;
|
|
|
|
saveRulesetConfig().then(( ) => {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
|
2022-09-13 23:44:24 +02:00
|
|
|
case 'popupPanelData': {
|
|
|
|
Promise.all([
|
2022-10-10 18:28:24 +02:00
|
|
|
getFilteringMode(request.hostname),
|
2022-09-28 01:51:38 +02:00
|
|
|
hasOmnipotence(),
|
2022-09-15 19:14:08 +02:00
|
|
|
hasGreatPowers(request.origin),
|
2022-09-30 01:51:33 +02:00
|
|
|
getEnabledRulesetsDetails(),
|
2022-09-17 14:26:41 +02:00
|
|
|
getInjectableCount(request.origin),
|
2022-09-13 23:44:24 +02:00
|
|
|
]).then(results => {
|
|
|
|
callback({
|
2022-10-10 18:28:24 +02:00
|
|
|
level: results[0],
|
|
|
|
autoReload: rulesetConfig.autoReload === 1,
|
2022-09-28 01:51:38 +02:00
|
|
|
hasOmnipotence: results[1],
|
|
|
|
hasGreatPowers: results[2],
|
|
|
|
rulesetDetails: results[3],
|
|
|
|
injectableCount: results[4],
|
2022-09-07 16:15:36 +02:00
|
|
|
});
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 18:28:24 +02:00
|
|
|
case 'getFilteringMode': {
|
|
|
|
getFilteringMode(request.hostname).then(actualLevel => {
|
|
|
|
callback(actualLevel);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'setFilteringMode': {
|
|
|
|
getFilteringMode(request.hostname).then(actualLevel => {
|
|
|
|
if ( request.level === actualLevel ) { return actualLevel; }
|
|
|
|
return setFilteringMode(request.hostname, request.level);
|
|
|
|
}).then(actualLevel => {
|
2022-09-28 16:20:57 +02:00
|
|
|
registerInjectables();
|
2022-10-10 18:28:24 +02:00
|
|
|
callback(actualLevel);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'setDefaultFilteringMode': {
|
|
|
|
getDefaultFilteringMode(
|
|
|
|
).then(beforeLevel =>
|
|
|
|
setDefaultFilteringMode(request.level).then(afterLevel =>
|
|
|
|
({ beforeLevel, afterLevel })
|
|
|
|
)
|
|
|
|
).then(({ beforeLevel, afterLevel }) => {
|
|
|
|
if ( beforeLevel === 1 || afterLevel === 1 ) {
|
|
|
|
updateDynamicRules();
|
|
|
|
}
|
|
|
|
if ( afterLevel !== beforeLevel ) {
|
|
|
|
registerInjectables();
|
|
|
|
}
|
|
|
|
callback(afterLevel);
|
2022-09-13 23:44:24 +02:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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-30 01:51:33 +02:00
|
|
|
updateDynamicRules().then(( ) => {
|
2022-09-18 15:31:44 +02:00
|
|
|
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.
|
2022-09-28 01:51:38 +02:00
|
|
|
registerInjectables();
|
2022-09-18 15:31:44 +02:00
|
|
|
|
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);
|
|
|
|
|
2022-09-28 01:51:38 +02:00
|
|
|
browser.permissions.onRemoved.addListener(onPermissionsRemoved);
|
2022-09-30 15:18:52 +02:00
|
|
|
|
|
|
|
if ( rulesetConfig.firstRun ) {
|
|
|
|
runtime.openOptionsPage();
|
|
|
|
}
|
2022-09-07 16:15:36 +02:00
|
|
|
})();
|