2017-05-27 17:51:24 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-07-20 19:52:14 +02:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-05-27 17:51:24 +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';
|
|
|
|
|
2017-05-27 17:51:24 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import µb from './background.js';
|
2021-10-07 20:41:29 +02:00
|
|
|
import { hostnameFromURI } from './uri-utils.js';
|
2017-05-27 17:51:24 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.canUseShortcuts = vAPI.commands instanceof Object;
|
2020-04-05 15:47:22 +02:00
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/386
|
2022-03-12 19:50:46 +01:00
|
|
|
// Firefox 74 and above has complete shortcut assignment user interface.
|
2021-10-13 14:23:20 +02:00
|
|
|
µb.canUpdateShortcuts = false;
|
|
|
|
|
|
|
|
if (
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.canUseShortcuts &&
|
2020-04-05 15:47:22 +02:00
|
|
|
vAPI.webextFlavor.soup.has('firefox') &&
|
2021-10-13 14:23:20 +02:00
|
|
|
typeof vAPI.commands.update === 'function'
|
|
|
|
) {
|
2020-04-05 15:47:22 +02:00
|
|
|
self.addEventListener(
|
|
|
|
'webextFlavor',
|
2020-04-10 23:25:36 +02:00
|
|
|
( ) => {
|
|
|
|
µb.canUpdateShortcuts = vAPI.webextFlavor.major < 74;
|
|
|
|
if ( µb.canUpdateShortcuts === false ) { return; }
|
|
|
|
vAPI.storage.get('commandShortcuts').then(bin => {
|
|
|
|
if ( bin instanceof Object === false ) { return; }
|
|
|
|
const shortcuts = bin.commandShortcuts;
|
|
|
|
if ( Array.isArray(shortcuts) === false ) { return; }
|
|
|
|
µb.commandShortcuts = new Map(shortcuts);
|
|
|
|
for ( const [ name, shortcut ] of shortcuts ) {
|
|
|
|
vAPI.commands.update({ name, shortcut });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2020-04-05 15:47:22 +02:00
|
|
|
{ once: true }
|
|
|
|
);
|
|
|
|
}
|
2018-07-20 19:52:14 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-06-26 13:47:14 +02:00
|
|
|
(( ) => {
|
2017-05-27 17:51:24 +02:00
|
|
|
|
2019-06-26 13:47:14 +02:00
|
|
|
// *****************************************************************************
|
|
|
|
// start of local namespace
|
2017-05-27 17:51:24 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.canUseShortcuts === false ) { return; }
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2019-09-08 18:52:28 +02:00
|
|
|
const relaxBlockingMode = (( ) => {
|
|
|
|
const reloadTimers = new Map();
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2019-09-08 18:52:28 +02:00
|
|
|
return function(tab) {
|
|
|
|
if ( tab instanceof Object === false || tab.id <= 0 ) { return; }
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
const normalURL = µb.normalizeTabURL(tab.id, tab.url);
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2019-09-08 18:52:28 +02:00
|
|
|
if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; }
|
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
const hn = hostnameFromURI(normalURL);
|
2019-09-08 18:52:28 +02:00
|
|
|
const curProfileBits = µb.blockingModeFromHostname(hn);
|
|
|
|
let newProfileBits;
|
|
|
|
for ( const profile of µb.liveBlockingProfiles ) {
|
|
|
|
if ( (curProfileBits & profile.bits & ~1) !== curProfileBits ) {
|
|
|
|
newProfileBits = profile.bits;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-26 13:47:14 +02:00
|
|
|
}
|
2019-09-08 18:52:28 +02:00
|
|
|
|
|
|
|
// TODO: Reset to original blocking profile?
|
|
|
|
if ( newProfileBits === undefined ) { return; }
|
|
|
|
|
2020-12-12 20:33:49 +01:00
|
|
|
const noReload = (newProfileBits & 0b00000001) === 0;
|
|
|
|
|
2019-06-26 13:47:14 +02:00
|
|
|
if (
|
2019-09-08 18:52:28 +02:00
|
|
|
(curProfileBits & 0b00000010) !== 0 &&
|
|
|
|
(newProfileBits & 0b00000010) === 0
|
2019-06-26 13:47:14 +02:00
|
|
|
) {
|
2019-09-08 18:52:28 +02:00
|
|
|
µb.toggleHostnameSwitch({
|
|
|
|
name: 'no-scripting',
|
|
|
|
hostname: hn,
|
|
|
|
state: false,
|
2019-06-26 13:47:14 +02:00
|
|
|
});
|
|
|
|
}
|
2019-09-08 18:52:28 +02:00
|
|
|
if ( µb.userSettings.advancedUserEnabled ) {
|
|
|
|
if (
|
|
|
|
(curProfileBits & 0b00000100) !== 0 &&
|
|
|
|
(newProfileBits & 0b00000100) === 0
|
|
|
|
) {
|
|
|
|
µb.toggleFirewallRule({
|
2020-12-12 20:33:49 +01:00
|
|
|
tabId: noReload ? tab.id : undefined,
|
2019-09-08 18:52:28 +02:00
|
|
|
srcHostname: hn,
|
|
|
|
desHostname: '*',
|
|
|
|
requestType: '3p',
|
|
|
|
action: 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(curProfileBits & 0b00001000) !== 0 &&
|
|
|
|
(newProfileBits & 0b00001000) === 0
|
|
|
|
) {
|
|
|
|
µb.toggleFirewallRule({
|
|
|
|
srcHostname: hn,
|
|
|
|
desHostname: '*',
|
|
|
|
requestType: '3p-script',
|
|
|
|
action: 3,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(curProfileBits & 0b00010000) !== 0 &&
|
|
|
|
(newProfileBits & 0b00010000) === 0
|
|
|
|
) {
|
|
|
|
µb.toggleFirewallRule({
|
|
|
|
srcHostname: hn,
|
|
|
|
desHostname: '*',
|
|
|
|
requestType: '3p-frame',
|
|
|
|
action: 3,
|
|
|
|
});
|
|
|
|
}
|
2017-05-27 17:51:24 +02:00
|
|
|
}
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2019-09-08 18:52:28 +02:00
|
|
|
// Reload the target tab?
|
2020-12-12 20:33:49 +01:00
|
|
|
if ( noReload ) { return; }
|
2019-09-08 18:52:28 +02:00
|
|
|
|
|
|
|
// Reload: use a timer to coalesce bursts of reload commands.
|
|
|
|
let timer = reloadTimers.get(tab.id);
|
|
|
|
if ( timer !== undefined ) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
timer = vAPI.setTimeout(
|
|
|
|
tabId => {
|
|
|
|
reloadTimers.delete(tabId);
|
|
|
|
vAPI.tabs.reload(tabId);
|
|
|
|
},
|
|
|
|
547,
|
|
|
|
tab.id
|
|
|
|
);
|
|
|
|
reloadTimers.set(tab.id, timer);
|
|
|
|
};
|
|
|
|
})();
|
2019-06-26 13:47:14 +02:00
|
|
|
|
2019-09-16 15:45:17 +02:00
|
|
|
vAPI.commands.onCommand.addListener(async command => {
|
2022-02-22 14:44:09 +01:00
|
|
|
// Generic commands
|
|
|
|
if ( command === 'open-dashboard' ) {
|
|
|
|
µb.openNewTab({
|
|
|
|
url: 'dashboard.html',
|
|
|
|
select: true,
|
|
|
|
index: -1,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Tab-specific commands
|
|
|
|
const tab = await vAPI.tabs.getCurrent();
|
|
|
|
if ( tab instanceof Object === false ) { return; }
|
2019-06-26 13:47:14 +02:00
|
|
|
switch ( command ) {
|
|
|
|
case 'launch-element-picker':
|
2019-09-16 15:45:17 +02:00
|
|
|
case 'launch-element-zapper': {
|
2019-09-18 18:17:45 +02:00
|
|
|
µb.epickerArgs.mouse = false;
|
2019-09-16 15:45:17 +02:00
|
|
|
µb.elementPickerExec(
|
|
|
|
tab.id,
|
2020-12-05 21:26:29 +01:00
|
|
|
0,
|
2019-09-16 15:45:17 +02:00
|
|
|
undefined,
|
|
|
|
command === 'launch-element-zapper'
|
|
|
|
);
|
2019-06-26 13:47:14 +02:00
|
|
|
break;
|
2019-09-16 15:45:17 +02:00
|
|
|
}
|
|
|
|
case 'launch-logger': {
|
|
|
|
const hash = tab.url.startsWith(vAPI.getURL(''))
|
|
|
|
? ''
|
|
|
|
: `#_+${tab.id}`;
|
|
|
|
µb.openNewTab({
|
|
|
|
url: `logger-ui.html${hash}`,
|
|
|
|
select: true,
|
2020-04-30 16:31:23 +02:00
|
|
|
index: -1,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2019-06-27 14:16:18 +02:00
|
|
|
case 'relax-blocking-mode':
|
2022-02-22 14:44:09 +01:00
|
|
|
relaxBlockingMode(tab);
|
|
|
|
break;
|
|
|
|
case 'toggle-cosmetic-filtering':
|
|
|
|
µb.toggleHostnameSwitch({
|
|
|
|
name: 'no-cosmetic-filtering',
|
|
|
|
hostname: hostnameFromURI(µb.normalizeTabURL(tab.id, tab.url)),
|
|
|
|
});
|
2019-06-26 13:47:14 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// end of local namespace
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2017-05-27 17:51:24 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|