-
Notifications
You must be signed in to change notification settings - Fork 3
/
g_off_handler.js
325 lines (295 loc) · 9.58 KB
/
g_off_handler.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var global_enabled = false;
var list_w_disable = []; // window off list
var list_h_disable = []; // tab and sub tabs off list
var list_t_disable = []; // tab off list
var listeners = [];
browser.runtime.onMessage.addListener(async (message) => {
if (message.action === 're-globalEnable-if-is-enabled')
{
//console.log("received message re-globalEnable-if-is-enabled");
if ( isGlobalEnabled() )
{
unsetGlobalEnable();
setGlobalEnable();
}
}
});
var NOTITLE;
if (isFirefox)
NOTITLE=null
else if (isChrome)
NOTITLE='';
/* blue icon: normal
* gray icon or red badge "off": globally off
* red badge "woff" : off on this window
* red badge "TN" : off on this tab and sub tabs
* red badge "T" : off on this tab
*/
function updateGlobalIcon(){
if (!global_enabled) //globally disabled
{
browser.browserAction.setBadgeText({ text: "Off" });
browser.browserAction.setBadgeBackgroundColor({ color: "#dd0000" }); // red
browser.browserAction.setTitle({title: default_title + " (Globally disabled)"});
browser.browserAction.setIcon( { path: "icon_gray.png" } );
}
else{ // globally enabled
browser.browserAction.setBadgeText({ text: null });
browser.browserAction.setBadgeBackgroundColor({ color: "#00BF00" });
browser.browserAction.setTitle({title: NOTITLE});
browser.browserAction.setIcon( { path: "icon.png" } );
}
}
function isGlobalEnabled(){
return global_enabled;
}
async function setGlobalEnable(){
if ( global_enabled == true )
return;
// --------------------------------
// any request type, including main_frame
if ( ( await browser.storage.local.get() )['easycpu'] !== true )
{
var onBeforeSendHeaders_listener_options;
if (isFirefox)
onBeforeSendHeaders_listener_options = ["blocking", "requestHeaders"];
if (isChrome)
onBeforeSendHeaders_listener_options = ["blocking", "requestHeaders", "extraHeaders"];
listeners.push([browser.webRequest.onBeforeSendHeaders, onBeforeSendHeaders]);
browser.webRequest.onBeforeSendHeaders.addListener(
onBeforeSendHeaders,
{urls: ["<all_urls>", "*://*/*", "ws://*/*", "wss://*/*", ]},
onBeforeSendHeaders_listener_options
);
}
//---------------------------
// main_frame only
if ( ( await browser.storage.local.get() )['workaround'] === true )
{
listeners.push([browser.webRequest.onBeforeRequest, onBeforeRequest_main]);
browser.webRequest.onBeforeRequest.addListener(
onBeforeRequest_main,
{urls: ["<all_urls>", "*://*/*", "ws://*/*", "wss://*/*", ], types: ["main_frame"]},
["blocking"]
);
}
//------------------------------
global_enabled = true;
updateGlobalIcon();
}
function unsetGlobalEnable(){
while( listeners[0] !== undefined )
{
L = listeners[0];
L[0].removeListener(L[1]);
listeners.shift();
}
global_enabled = false;
updateGlobalIcon();
}
function toggle_global_enabled()
{
if( isGlobalEnabled() ) {
unsetGlobalEnable();
}else{
setGlobalEnable();
}
}
//------------------------------------------
function isWindowDisabled(wid){
if (list_w_disable.includes(wid))
return true;
return false;
}
function setWindowDisabled(wid){
if (isWindowDisabled(wid))
return;
list_w_disable.push(wid);
update_windowBadge(wid);
}
function unsetWindowDisabled(wid){
if ( ! isWindowDisabled(wid))
return;
list_w_disable.splice( list_w_disable.indexOf(wid) ,1);
update_windowBadge(wid);
}
function toggle_window_disabled(wid)
{
if ( isWindowDisabled(wid) ) {
unsetWindowDisabled(wid);
return true;
}else{
setWindowDisabled(wid);
return false;
}
}
function update_windowBadge(wid){
if ( isWindowDisabled(wid) ){
browser.browserAction.setTitle({title: default_title + " (Disabled in this window)", windowId: wid });
browser.browserAction.setBadgeText({ text: "woff" , windowId: wid});
browser.browserAction.setBadgeBackgroundColor({ color: "#ff6666" , windowId: wid}); // red
}else{
browser.browserAction.setTitle({title: NOTITLE, windowId: wid });
browser.browserAction.setBadgeText({ text: null , windowId: wid});
//browser.browserAction.setBadgeBackgroundColor({ color: "" , windowId: wid});
}
}
//---------------------------------------------------
function isTabIn_list_t(tabid){
if ( list_t_disable.includes(tabid) )
return true;
return false;
}
function isTabIn_list_h(tabid){
if ( list_h_disable.includes(tabid) ) {
return true;
}
return false;
}
function unsetTab_t(tabid){
if ( isTabIn_list_t(tabid) )
list_t_disable.splice( list_t_disable.indexOf(tabid), 1 );
update_tabBadge(tabid);
}
function unsetTab_h(tabid){
if ( isTabIn_list_h(tabid) )
list_h_disable.splice( list_h_disable.indexOf(tabid), 1 );
update_tabBadge(tabid);
}
function setTab_t(tabid) {
unsetTab_h(tabid);
if ( isTabIn_list_t(tabid) )
return;
list_t_disable.push(tabid);
update_tabBadge(tabid);
}
function setTab_h(tabid) {
unsetTab_t(tabid);
if (isTabIn_list_h(tabid) )
return;
list_h_disable.push(tabid);
update_tabBadge(tabid);
}
function normalizeTab(tabid){
unsetTab_t(tabid);
unsetTab_h(tabid);
}
function toggleTab_t(tabid){
if (isTabIn_list_t(tabid) )
unsetTab_t(tabid);
else
setTab_t(tabid);
}
function toggleTab_h(tabid){
if (isTabIn_list_h(tabid) ) {
unsetTab_h(tabid);
}else{
setTab_h(tabid);
}
}
async function update_tabBadge(tabid){
try{
if (isTabIn_list_h(tabid)){
await browser.browserAction.setTitle({title: default_title + " (Disabled on this tab and its sub new tabs)", tabId: tabid });
await browser.browserAction.setBadgeText({ text: "TN" , tabId: tabid});
//await browser.browserAction.setBadgeBackgroundColor({ color: "#fbff00" , tabId: tabid}); // yellow
await browser.browserAction.setBadgeBackgroundColor({ color: "#ff6666" , tabId: tabid}); // red
}else if( isTabIn_list_t(tabid) ){
await browser.browserAction.setTitle({title: default_title + " (Disabled on this tab)", tabId: tabid });
await browser.browserAction.setBadgeText({ text: "T" , tabId: tabid});
//await browser.browserAction.setBadgeBackgroundColor({ color: "#ffea00" , tabId: tabid}); // orange
await browser.browserAction.setBadgeBackgroundColor({ color: "#ff6666" , tabId: tabid}); // red
}else{
await browser.browserAction.setTitle({title: NOTITLE, tabId: tabid });
//await browser.browserAction.setBadgeTextColor({color: "", tabId: tabid });
await browser.browserAction.setBadgeText({ text: null , tabId: tabid});
//await browser.browserAction.setBadgeBackgroundColor({ color: "" , tabId: tabid});
}
} catch(err){
if ( ! err.message.startsWith("Invalid tab ID:") )
console.error(err);
}
}
browser.tabs.onUpdated.addListener( (tabid) => {
update_tabBadge(tabid);
});
browser.tabs.onRemoved.addListener( (tabid, removeInfo) => {
const wid = removeInfo.windowId;
normalizeTab(tabid);
});
browser.windows.onRemoved.addListener((wid) => {
unsetWindowDisabled(wid);
});
browser.browserAction.onClicked.addListener((tab) => {
const tabid = tab.id;
const wid = tab.windowId;
if ( ! isTabIn_list_h(tabid) && ! isTabIn_list_t(tabid) )
{
setTab_t(tabid);
}else if ( isTabIn_list_t(tabid) )
{
setTab_h(tabid);
}else if ( isTabIn_list_h(tabid) )
{
normalizeTab(tabid);
}
});
browser.tabs.onCreated.addListener( (tab) => {
if ( isTabIn_list_h(tab.openerTabId) ) {
setTab_h(tab.id);
}
});
#ifndef CHROME
async function is_off(details, tabid, tab, wid, changeInfo){
#else
function is_off(details, tabid, tab, wid, changeInfo){
#endif
if ( ! global_enabled )
return true;
if (typeof(details) == "object" )
{
tabid = details.tabId;
}
if (typeof(tab) == "object")
{
tabid = tab.id;
wid = tab.windowId;
}
if ( tabid < 0 )
return true;
if (isTabIn_list_h(tabid) || isTabIn_list_t(tabid) )
return true;
#ifndef CHROME
if ( wid === undefined )
try{
wid = (await browser.tabs.get(tabid)).windowId;
} catch(err){
if ( ! err.message.startsWith("Invalid tab ID:") )
console.error(err);
return true;
}
#endif
if( isWindowDisabled( wid ) )
return true;
}
browser.commands.onCommand.addListener(async function (command) {
switch (command) {
case "toggle_global":
toggle_global_enabled();
break;
case "toggle_t":
case "toggle_h":
case "toggle_window":
const cur_tabInfo = (await browser.tabs.query({currentWindow: true, active: true}) ) [0];
const tabid = cur_tabInfo.id;
const wid = cur_tabInfo.windowId;
if (command == "toggle_t") {
toggleTab_t(tabid);
}else if (command == "toggle_h") {
toggleTab_h(tabid);
}else if (command == "toggle_window") {
toggle_window_disabled(wid);
}
break;
}
});