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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-07-20 19:52:14 +02:00
|
|
|
µBlock.canUseShortcuts = vAPI.commands instanceof Object;
|
2020-04-05 15:47:22 +02:00
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/386
|
|
|
|
// Firefox 74 and above has complete shotcut assignment user interface.
|
|
|
|
µBlock.canUpdateShortcuts =
|
|
|
|
µBlock.canUseShortcuts &&
|
|
|
|
vAPI.webextFlavor.soup.has('firefox') &&
|
|
|
|
typeof vAPI.commands.update === 'function';
|
|
|
|
|
|
|
|
if ( µBlock.canUpdateShortcuts ) {
|
|
|
|
self.addEventListener(
|
|
|
|
'webextFlavor',
|
2020-04-10 23:25:36 +02:00
|
|
|
( ) => {
|
|
|
|
const µb = µBlock;
|
|
|
|
µ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
|
|
|
|
2019-06-26 13:47:14 +02:00
|
|
|
if ( µBlock.canUseShortcuts === false ) { return; }
|
|
|
|
|
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
|
|
|
|
2019-09-08 18:52:28 +02:00
|
|
|
const µb = µBlock;
|
|
|
|
const normalURL = µb.normalizePageURL(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; }
|
|
|
|
|
|
|
|
const hn = µb.URI.hostnameFromURI(normalURL);
|
|
|
|
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; }
|
|
|
|
|
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({
|
|
|
|
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?
|
|
|
|
if ( (newProfileBits & 0b00000001) === 0 ) { return; }
|
|
|
|
|
|
|
|
// 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 => {
|
2019-06-26 13:47:14 +02:00
|
|
|
const µb = µBlock;
|
|
|
|
|
|
|
|
switch ( command ) {
|
|
|
|
case 'launch-element-picker':
|
2019-09-16 15:45:17 +02:00
|
|
|
case 'launch-element-zapper': {
|
|
|
|
const tab = await vAPI.tabs.getCurrent();
|
|
|
|
if ( tab instanceof Object === false ) { return; }
|
2019-09-18 18:17:45 +02:00
|
|
|
µb.epickerArgs.mouse = false;
|
2019-09-16 15:45:17 +02:00
|
|
|
µb.elementPickerExec(
|
|
|
|
tab.id,
|
|
|
|
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 tab = await vAPI.tabs.getCurrent();
|
|
|
|
if ( tab instanceof Object === false ) { return; }
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
case 'open-dashboard': {
|
|
|
|
µb.openNewTab({
|
|
|
|
url: 'dashboard.html',
|
|
|
|
select: true,
|
|
|
|
index: -1,
|
2019-06-26 13:47:14 +02:00
|
|
|
});
|
|
|
|
break;
|
2019-09-16 15:45:17 +02:00
|
|
|
}
|
2019-06-27 14:16:18 +02:00
|
|
|
case 'relax-blocking-mode':
|
2019-09-16 15:45:17 +02:00
|
|
|
relaxBlockingMode(await vAPI.tabs.getCurrent());
|
2019-06-26 13:47:14 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// end of local namespace
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2017-05-27 17:51:24 +02:00
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|