2022-09-13 15:44:24 -06: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 08:15:36 -06:00
|
|
|
'use strict';
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-20 06:24:01 -06:00
|
|
|
import {
|
|
|
|
browser,
|
|
|
|
dnr,
|
|
|
|
runtime,
|
2023-06-05 07:15:59 -06:00
|
|
|
localRead, localWrite,
|
|
|
|
sessionRead, sessionWrite,
|
2023-07-29 07:33:24 -06:00
|
|
|
adminRead,
|
2022-09-20 06:24:01 -06:00
|
|
|
} from './ext.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getRulesetDetails,
|
|
|
|
defaultRulesetsFromLanguage,
|
|
|
|
enableRulesets,
|
2022-09-29 17:51:33 -06:00
|
|
|
getEnabledRulesetsDetails,
|
|
|
|
updateDynamicRules,
|
2022-09-20 06:24:01 -06:00
|
|
|
} from './ruleset-manager.js';
|
|
|
|
|
|
|
|
import {
|
2022-09-27 17:51:38 -06:00
|
|
|
registerInjectables,
|
2022-09-20 06:24:01 -06:00
|
|
|
} from './scripting-manager.js';
|
|
|
|
|
|
|
|
import {
|
2022-10-10 10:28:24 -06:00
|
|
|
getFilteringMode,
|
|
|
|
setFilteringMode,
|
|
|
|
getDefaultFilteringMode,
|
|
|
|
setDefaultFilteringMode,
|
2022-10-18 06:48:31 -06:00
|
|
|
syncWithBrowserPermissions,
|
2022-10-10 10:28:24 -06:00
|
|
|
} from './mode-manager.js';
|
2022-09-07 08:15:36 -06:00
|
|
|
|
2023-06-04 09:32:55 -06:00
|
|
|
import {
|
|
|
|
ubolLog,
|
|
|
|
} from './utils.js';
|
|
|
|
|
2022-09-07 08:15:36 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
const rulesetConfig = {
|
|
|
|
version: '',
|
2022-10-10 10:28:24 -06:00
|
|
|
enabledRulesets: [ 'default' ],
|
|
|
|
autoReload: 1,
|
2022-09-13 15:44:24 -06:00
|
|
|
};
|
|
|
|
|
2022-10-15 11:05:20 -06:00
|
|
|
const UBOL_ORIGIN = runtime.getURL('').replace(/\/$/, '');
|
|
|
|
|
2023-06-06 11:42:22 -06:00
|
|
|
let firstRun = false;
|
2023-06-13 09:40:41 -06:00
|
|
|
let wakeupRun = false;
|
2023-06-06 11:42:22 -06:00
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function getCurrentVersion() {
|
|
|
|
return runtime.getManifest().version;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadRulesetConfig() {
|
2023-06-05 07:15:59 -06:00
|
|
|
let data = await sessionRead('rulesetConfig');
|
|
|
|
if ( data ) {
|
|
|
|
rulesetConfig.version = data.version;
|
|
|
|
rulesetConfig.enabledRulesets = data.enabledRulesets;
|
|
|
|
rulesetConfig.autoReload = data.autoReload;
|
2023-06-13 09:40:41 -06:00
|
|
|
wakeupRun = true;
|
|
|
|
return;
|
2022-09-13 15:44:24 -06:00
|
|
|
}
|
2023-06-05 07:15:59 -06:00
|
|
|
data = await localRead('rulesetConfig');
|
|
|
|
if ( data ) {
|
|
|
|
rulesetConfig.version = data.version;
|
|
|
|
rulesetConfig.enabledRulesets = data.enabledRulesets;
|
|
|
|
rulesetConfig.autoReload = data.autoReload;
|
2023-06-13 09:40:41 -06:00
|
|
|
sessionWrite('rulesetConfig', rulesetConfig);
|
|
|
|
return;
|
2022-09-13 15:44:24 -06:00
|
|
|
}
|
2023-06-05 07:15:59 -06:00
|
|
|
rulesetConfig.enabledRulesets = await defaultRulesetsFromLanguage();
|
|
|
|
sessionWrite('rulesetConfig', rulesetConfig);
|
|
|
|
localWrite('rulesetConfig', rulesetConfig);
|
2023-06-13 09:40:41 -06:00
|
|
|
firstRun = true;
|
2022-09-13 15:44:24 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 07:15:59 -06:00
|
|
|
async function saveRulesetConfig() {
|
|
|
|
sessionWrite('rulesetConfig', rulesetConfig);
|
|
|
|
return localWrite('rulesetConfig', rulesetConfig);
|
2022-09-13 15:44:24 -06:00
|
|
|
}
|
2022-09-07 08:15:36 -06:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-29 17:51:33 -06:00
|
|
|
async function hasGreatPowers(origin) {
|
|
|
|
if ( /^https?:\/\//.test(origin) === false ) { return false; }
|
2022-09-15 11:14:08 -06:00
|
|
|
return browser.permissions.contains({
|
2022-09-27 05:46:24 -06:00
|
|
|
origins: [ `${origin}/*` ],
|
2022-09-13 15:44:24 -06:00
|
|
|
});
|
2022-09-15 11:14:08 -06:00
|
|
|
}
|
2022-09-13 15:44:24 -06:00
|
|
|
|
2022-09-27 17:51:38 -06:00
|
|
|
function hasOmnipotence() {
|
|
|
|
return browser.permissions.contains({
|
|
|
|
origins: [ '<all_urls>' ],
|
2022-09-07 08:15:36 -06:00
|
|
|
});
|
2022-09-15 11:14:08 -06:00
|
|
|
}
|
2022-09-07 08:15:36 -06:00
|
|
|
|
2022-10-18 06:48:31 -06:00
|
|
|
async function onPermissionsRemoved() {
|
|
|
|
const beforeMode = await getDefaultFilteringMode();
|
|
|
|
const modified = await syncWithBrowserPermissions();
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( modified === false ) { return false; }
|
2022-10-18 06:48:31 -06:00
|
|
|
const afterMode = await getDefaultFilteringMode();
|
|
|
|
if ( beforeMode > 1 && afterMode <= 1 ) {
|
2022-09-29 17:51:33 -06:00
|
|
|
updateDynamicRules();
|
|
|
|
}
|
2022-10-18 06:48:31 -06:00
|
|
|
registerInjectables();
|
2023-06-13 09:40:41 -06:00
|
|
|
return true;
|
2022-09-27 05:46:24 -06:00
|
|
|
}
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
function onMessage(request, sender, callback) {
|
2022-10-15 11:05:20 -06:00
|
|
|
|
2023-06-04 09:32:55 -06:00
|
|
|
// Does not require trusted origin.
|
|
|
|
|
|
|
|
switch ( request.what ) {
|
|
|
|
|
|
|
|
case 'insertCSS': {
|
|
|
|
const tabId = sender?.tab?.id ?? false;
|
|
|
|
const frameId = sender?.frameId ?? false;
|
|
|
|
if ( tabId === false || frameId === false ) { return; }
|
|
|
|
browser.scripting.insertCSS({
|
|
|
|
css: request.css,
|
|
|
|
origin: 'USER',
|
|
|
|
target: { tabId, frameIds: [ frameId ] },
|
2023-06-05 07:15:59 -06:00
|
|
|
}).catch(reason => {
|
|
|
|
console.log(reason);
|
2023-06-04 09:32:55 -06:00
|
|
|
});
|
2023-06-13 09:40:41 -06:00
|
|
|
callback();
|
2023-06-04 09:32:55 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-08-13 11:28:02 -06:00
|
|
|
// Does require trusted origin.
|
2023-06-04 09:32:55 -06:00
|
|
|
|
2023-04-07 08:19:43 -06:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/MessageSender
|
|
|
|
// Firefox API does not set `sender.origin`
|
|
|
|
if ( sender.origin !== undefined && sender.origin !== UBOL_ORIGIN ) { return; }
|
2022-10-15 11:05:20 -06:00
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
switch ( request.what ) {
|
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
case 'applyRulesets': {
|
|
|
|
enableRulesets(request.enabledRulesets).then(( ) => {
|
|
|
|
rulesetConfig.enabledRulesets = request.enabledRulesets;
|
2022-09-28 08:20:57 -06:00
|
|
|
return saveRulesetConfig();
|
2022-09-15 11:14:08 -06:00
|
|
|
}).then(( ) => {
|
2022-09-28 08:20:57 -06:00
|
|
|
registerInjectables();
|
2022-09-15 11:14:08 -06:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
case 'getOptionsPageData': {
|
2022-09-16 13:56:35 -06:00
|
|
|
Promise.all([
|
2022-10-10 10:28:24 -06:00
|
|
|
getDefaultFilteringMode(),
|
2022-09-16 13:56:35 -06:00
|
|
|
getRulesetDetails(),
|
|
|
|
dnr.getEnabledRulesets(),
|
|
|
|
]).then(results => {
|
2022-10-10 10:28:24 -06:00
|
|
|
const [
|
|
|
|
defaultFilteringMode,
|
|
|
|
rulesetDetails,
|
|
|
|
enabledRulesets,
|
|
|
|
] = results;
|
2022-09-13 15:44:24 -06:00
|
|
|
callback({
|
2022-10-10 10:28:24 -06:00
|
|
|
defaultFilteringMode,
|
2022-09-13 15:44:24 -06:00
|
|
|
enabledRulesets,
|
2023-07-09 07:25:38 -06:00
|
|
|
maxNumberOfEnabledRulesets: dnr.MAX_NUMBER_OF_ENABLED_STATIC_RULESETS,
|
2022-09-13 15:44:24 -06:00
|
|
|
rulesetDetails: Array.from(rulesetDetails.values()),
|
2022-10-10 10:28:24 -06:00
|
|
|
autoReload: rulesetConfig.autoReload === 1,
|
2023-06-06 11:42:22 -06:00
|
|
|
firstRun,
|
2022-09-07 08:15:36 -06:00
|
|
|
});
|
2023-06-06 11:42:22 -06:00
|
|
|
firstRun = false;
|
2022-09-13 15:44:24 -06:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06:00
|
|
|
case 'setAutoReload':
|
|
|
|
rulesetConfig.autoReload = request.state ? 1 : 0;
|
|
|
|
saveRulesetConfig().then(( ) => {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
|
2022-09-13 15:44:24 -06:00
|
|
|
case 'popupPanelData': {
|
|
|
|
Promise.all([
|
2022-10-10 10:28:24 -06:00
|
|
|
getFilteringMode(request.hostname),
|
2022-09-27 17:51:38 -06:00
|
|
|
hasOmnipotence(),
|
2022-09-15 11:14:08 -06:00
|
|
|
hasGreatPowers(request.origin),
|
2022-09-29 17:51:33 -06:00
|
|
|
getEnabledRulesetsDetails(),
|
2022-09-13 15:44:24 -06:00
|
|
|
]).then(results => {
|
|
|
|
callback({
|
2022-10-10 10:28:24 -06:00
|
|
|
level: results[0],
|
|
|
|
autoReload: rulesetConfig.autoReload === 1,
|
2022-09-27 17:51:38 -06:00
|
|
|
hasOmnipotence: results[1],
|
|
|
|
hasGreatPowers: results[2],
|
|
|
|
rulesetDetails: results[3],
|
2022-09-07 08:15:36 -06:00
|
|
|
});
|
2022-09-13 15:44:24 -06:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:28:24 -06: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 08:20:57 -06:00
|
|
|
registerInjectables();
|
2022-10-10 10:28:24 -06:00
|
|
|
callback(actualLevel);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'setDefaultFilteringMode': {
|
2023-06-13 09:40:41 -06:00
|
|
|
getDefaultFilteringMode().then(beforeLevel =>
|
2022-10-10 10:28:24 -06:00
|
|
|
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 15:44:24 -06:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
async function start() {
|
2023-06-13 09:40:41 -06:00
|
|
|
await loadRulesetConfig();
|
|
|
|
|
|
|
|
if ( wakeupRun === false ) {
|
|
|
|
await enableRulesets(rulesetConfig.enabledRulesets);
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
|
|
|
|
// We need to update the regex rules only when ruleset version changes.
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( wakeupRun === false ) {
|
|
|
|
const currentVersion = getCurrentVersion();
|
|
|
|
if ( currentVersion !== rulesetConfig.version ) {
|
|
|
|
ubolLog(`Version change: ${rulesetConfig.version} => ${currentVersion}`);
|
|
|
|
updateDynamicRules().then(( ) => {
|
|
|
|
rulesetConfig.version = currentVersion;
|
|
|
|
saveRulesetConfig();
|
|
|
|
});
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
}
|
|
|
|
|
2022-10-18 06:48:31 -06:00
|
|
|
// Permissions may have been removed while the extension was disabled
|
2023-06-13 09:40:41 -06:00
|
|
|
const permissionsChanged = await onPermissionsRemoved();
|
2022-10-18 06:48:31 -06:00
|
|
|
|
2022-09-18 07:31:44 -06: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.
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( wakeupRun === false || permissionsChanged ) {
|
|
|
|
registerInjectables();
|
2022-09-18 07:31:44 -06:00
|
|
|
|
2023-06-13 09:40:41 -06:00
|
|
|
const enabledRulesets = await dnr.getEnabledRulesets();
|
|
|
|
ubolLog(`Enabled rulesets: ${enabledRulesets}`);
|
2022-09-15 11:14:08 -06:00
|
|
|
|
2023-06-13 09:40:41 -06:00
|
|
|
dnr.getAvailableStaticRuleCount().then(count => {
|
|
|
|
ubolLog(`Available static rule count: ${count}`);
|
|
|
|
});
|
|
|
|
}
|
2022-09-15 11:14:08 -06:00
|
|
|
|
2023-04-07 08:19:43 -06:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/declarativeNetRequest
|
|
|
|
// Firefox API does not support `dnr.setExtensionActionOptions`
|
2023-06-13 09:40:41 -06:00
|
|
|
if ( wakeupRun === false && dnr.setExtensionActionOptions ) {
|
2023-04-07 08:19:43 -06:00
|
|
|
dnr.setExtensionActionOptions({ displayActionCountAsBadgeText: true });
|
|
|
|
}
|
2022-09-13 15:44:24 -06:00
|
|
|
|
2022-09-15 11:14:08 -06:00
|
|
|
runtime.onMessage.addListener(onMessage);
|
|
|
|
|
2022-10-18 06:48:31 -06:00
|
|
|
browser.permissions.onRemoved.addListener(
|
|
|
|
( ) => { onPermissionsRemoved(); }
|
|
|
|
);
|
2022-09-30 07:18:52 -06:00
|
|
|
|
2023-06-06 11:42:22 -06:00
|
|
|
if ( firstRun ) {
|
2023-07-29 07:33:24 -06:00
|
|
|
const disableFirstRunPage = await adminRead('disableFirstRunPage');
|
|
|
|
if ( disableFirstRunPage !== true ) {
|
|
|
|
runtime.openOptionsPage();
|
|
|
|
}
|
2022-09-30 07:18:52 -06:00
|
|
|
}
|
2023-06-06 11:42:22 -06:00
|
|
|
}
|
|
|
|
|
2023-06-23 18:44:44 -06:00
|
|
|
try {
|
|
|
|
start();
|
|
|
|
} catch(reason) {
|
|
|
|
console.trace(reason);
|
|
|
|
}
|