forked from gorhill/uBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
217 lines (187 loc) · 6.71 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2017-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
*/
'use strict';
/******************************************************************************/
import µb from './background.js';
import { hostnameFromURI } from './uri-utils.js';
/******************************************************************************/
µb.canUseShortcuts = vAPI.commands instanceof Object;
// https://github.com/uBlockOrigin/uBlock-issues/issues/386
// Firefox 74 and above has complete shotcut assignment user interface.
µb.canUpdateShortcuts = false;
if (
µb.canUseShortcuts &&
vAPI.webextFlavor.soup.has('firefox') &&
typeof vAPI.commands.update === 'function'
) {
self.addEventListener(
'webextFlavor',
( ) => {
µ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 });
}
});
},
{ once: true }
);
}
/******************************************************************************/
(( ) => {
// *****************************************************************************
// start of local namespace
if ( µb.canUseShortcuts === false ) { return; }
const relaxBlockingMode = (( ) => {
const reloadTimers = new Map();
return function(tab) {
if ( tab instanceof Object === false || tab.id <= 0 ) { return; }
const normalURL = µb.normalizeTabURL(tab.id, tab.url);
if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; }
const hn = 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;
}
}
// TODO: Reset to original blocking profile?
if ( newProfileBits === undefined ) { return; }
const noReload = (newProfileBits & 0b00000001) === 0;
if (
(curProfileBits & 0b00000010) !== 0 &&
(newProfileBits & 0b00000010) === 0
) {
µb.toggleHostnameSwitch({
name: 'no-scripting',
hostname: hn,
state: false,
});
}
if ( µb.userSettings.advancedUserEnabled ) {
if (
(curProfileBits & 0b00000100) !== 0 &&
(newProfileBits & 0b00000100) === 0
) {
µb.toggleFirewallRule({
tabId: noReload ? tab.id : undefined,
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,
});
}
}
// Reload the target tab?
if ( noReload ) { 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);
};
})();
vAPI.commands.onCommand.addListener(async command => {
// 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; }
switch ( command ) {
case 'launch-element-picker':
case 'launch-element-zapper': {
µb.epickerArgs.mouse = false;
µb.elementPickerExec(
tab.id,
0,
undefined,
command === 'launch-element-zapper'
);
break;
}
case 'launch-logger': {
const hash = tab.url.startsWith(vAPI.getURL(''))
? ''
: `#_+${tab.id}`;
µb.openNewTab({
url: `logger-ui.html${hash}`,
select: true,
index: -1,
});
break;
}
case 'relax-blocking-mode':
relaxBlockingMode(tab);
break;
case 'toggle-cosmetic-filtering':
µb.toggleHostnameSwitch({
name: 'no-cosmetic-filtering',
hostname: hostnameFromURI(µb.normalizeTabURL(tab.id, tab.url)),
});
break;
default:
break;
}
});
// end of local namespace
// *****************************************************************************
})();
/******************************************************************************/