-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathbackground.js
838 lines (716 loc) · 29.1 KB
/
background.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/* @flow */
import type {Locator, Src, Url, Second, JsonArray, JsonObject} from './common';
import {Visit, Visits, Blacklisted, unwrap, Methods, ldebug, linfo, lerror, lwarn} from './common';
import {get_options_async, setOptions} from './options';
import {chromeTabsExecuteScriptAsync, chromeTabsInsertCSS, chromeTabsQueryAsync, chromeRuntimeGetPlatformInfo, chromeTabsGet} from './async_chrome';
import {showTabNotification, showBlackListedNotification, showIgnoredNotification, defensify, notify} from './notifications';
import {Blacklist} from './blacklist';
async function isAndroid() {
try {
const platform = await chromeRuntimeGetPlatformInfo();
return platform.os === 'android';
} catch (error) {
// defensive just in case since isAndroid is kinda crucial for extension functioning
console.error('error while determining platfrom; assuming not android: %o', error);
return false;
}
}
const isMobile = isAndroid; // TODO deprecate old name? note sure
// TODO ugh. en-GB etc can't be parsed by Date.parse afterwards...
// TODO allow to configure in options/or even use local tz
const systemTz = Intl.DateTimeFormat().resolvedOptions().timeZone;
const tz_fmt = new Intl.DateTimeFormat('sv-SE', {
timeZone: systemTz,
day : 'numeric',
month : 'numeric',
year : 'numeric',
hour : 'numeric',
minute: 'numeric',
second: 'numeric',
});
function normTz(dt: Date): Date {
// TODO ugh, merge for dt_formatter??
return new Date(tz_fmt.format(dt));
}
async function actions(): Promise<Array<chrome$browserAction | chrome$pageAction>> {
const res = [chrome.browserAction];
const android = await isAndroid();
if (android) {
res.push(chrome.pageAction);
}
// eh, on mobile neither pageAction nor browserAction have setIcon
// but we can use pageAction to show at least some (default) icon in some circumstances
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Differences_between_desktop_and_Android#User_interface
return res;
}
function rawToVisits(vis: JsonObject): Visits {
// TODO filter errors? not sure.
const visits = vis['visits'].map(v => {
const dts = v['dt'];
const dt: Date = normTz(new Date(dts));
const vtags: Array<Src> = [v['src']]; // TODO hmm. shouldn't be array?
const vourl: string = v['original_url'];
const vnurl: string = v['normalised_url'];
const vctx: ?string = v['context'];
const vloc: ?Locator = v['locator']
const vdur: ?Second = v['duration'];
return new Visit(vourl, vnurl, dt, vtags, vctx, vloc, vdur);
});
return new Visits(
vis['original_url'],
vis['normalised_url'],
visits
);
}
async function queryBackendCommon<R>(params, endp: string): Promise<R> {
const opts = await get_options_async();
const endpoint = `${opts.host}/${endp}`;
// TODO cors mode?
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Authorization': "Basic " + btoa(opts.token),
},
body: JSON.stringify(params)
}).then(response => {
// right, fetch API doesn't reject on HTTP error status...
const ok = response.ok;
if (!ok) {
throw response.statusText; // TODO...
}
return response.json();
});
return response;
}
async function getBackendVisits(u: Url): Promise<Visits> {
return queryBackendCommon<JsonObject>({url: u}, 'visits').then(rawToVisits);
}
// TODO include browser visits here too?
export async function searchVisits(u: Url): Promise<Visits> {
return queryBackendCommon<JsonObject>({url: u}, 'search').then(rawToVisits);
}
export async function searchAround(timestamp: number): Promise<Visits> {
return queryBackendCommon<JsonObject>({timestamp: timestamp}, 'search_around').then(rawToVisits);
}
function getDelayMs(/*url*/) {
return 10 * 1000;
}
const LOCAL_TAG = 'local';
async function getChromeVisits(url: Url): Promise<Visits> {
const android = await isAndroid();
if (android) {
// ugh. 'history' api is not supported on mobile (TODO mention that in readme)
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Differences_between_desktop_and_Android#Other_UI_related_API_and_manifest.json_key_differences
return new Visits(url, url, []);
}
// $FlowFixMe
const results = await new Promise((cb) => chrome.history.getVisits({url: url}, cb));
// without delay you will always be seeing website as visited
// TODO but could be a good idea to make it configurable; e.g. sometimes we do want to know immediately. so could do domain-based delay or something like that?
const delay = getDelayMs();
const current = normTz(new Date());
// NOTE: visitTime returns UTC epoch
const times: Array<Date> = results.map(r => normTz(new Date(r['visitTime']))).filter(dt => current - dt > delay);
// TODO not sure if need to normalise..
const visits = times.map(t => new Visit(url, url, t, [LOCAL_TAG]));
return new Visits(url, url, visits);
}
// TODO think about caching blacklist on background page?
// although need to be careful and invalidate it. ugh.
// TODO ugh. can't keep get_options_async in blacklist.js because jest complains..
async function Blacklist_get(): Promise<Blacklist> {
const opts = await get_options_async();
return new Blacklist(opts.blacklist);
}
type Result = Visits | Blacklisted | Error;
export async function getVisits(url: Url): Promise<Result> {
const blacklist = await Blacklist_get();
const bl = await blacklist.contains(url);
if (bl != null) {
return new Blacklisted(url, bl);
}
// TODO hmm. maybe have a special 'error' visit so we could just merge visits here?
// it's gona be a mess though..
const backendRes: Visits | Error = await getBackendVisits(url)
.catch((err: Error) => err);
if (backendRes instanceof Error) {
return backendRes;
}
const backendVisits = backendRes;
// NOTE sort of a problem with chrome visits that they don't respect normalisation.. not sure if there is much to do with it
const chromeVisits = await getChromeVisits(url);
const allVisits = backendVisits.visits.concat(chromeVisits.visits);
return new Visits(
backendVisits.original_url,
backendVisits.normalised_url,
allVisits
);
}
type IconStyle = {
icon: string,
title: string,
text: string,
};
// TODO this can be tested?
function getIconStyle(visits: Result): IconStyle {
if (visits instanceof Blacklisted) {
return {icon: 'images/ic_blacklisted_48.png', title: `Blacklisted: ${visits.reason}`, text: ''};
}
if (visits instanceof Error) {
return {icon: 'images/ic_error.png' , title: `ERROR: ${visits.message}`, text: ''};
}
const vcount = visits.visits.length;
if (vcount === 0) {
return {icon: 'images/ic_not_visited_48.png', title: 'Not visited', text: ''};
}
const cp = [];
const self_contexts = visits.self_contexts();
const ccount = self_contexts.length;
if (ccount > 0) {
cp.push(`${ccount} contexts`);
}
const rcontexts = visits.relative_contexts();
const rcount = rcontexts.length;
if (rcount > 0) {
// TODO rename to relative later?
cp.push(`${rcount} child contexts`);
}
const btext = rcount == 0 ? `${ccount}` : `${ccount}/${rcount}`;
const ctext = cp.join(', ');
if (ccount > 0) {
return {icon: 'images/ic_visited_48.png' , title: `${vcount} visits, ${ctext}`, text: btext};
}
if (rcount > 0) {
return {icon: 'images/ic_relatives_48.png' , title: `${vcount} visits, ${ctext}`, text: btext};
}
// TODO a bit ugly, but ok for now.. maybe cut off by time?
const boring = visits.visits.every(v => v.tags.length == 1 && v.tags[0] == LOCAL_TAG);
if (boring) {
// TODO not sure if really worth distinguishing..
return {icon: "images/ic_boring_48.png" , title: `${vcount} visits (local only)`, text: ''};
} else {
return {icon: "images/ic_blue_48.png" , title: `${vcount} visits`, text: ''};
}
}
async function updateState (tab: chrome$Tab) {
const url = unwrap(tab.url);
const tabId = unwrap(tab.id);
if (ignored(url)) {
linfo("ignoring %s", url);
return;
}
const opts = await get_options_async();
// TODO this should be executed as an atomic block?
const inject = () => chromeTabsExecuteScriptAsync(tabId, {file: 'sidebar.js'})
// TODO hmm. in theory script and CSS injections commute, but css order on the othe hand might matter?
// not sure, but using deferred promises just in case
.then(() => chromeTabsInsertCSS(tabId, {file: 'sidebar-outer.css'}))
.then(() => chromeTabsInsertCSS(tabId, {code: opts.position_css}));
// NOTE: if the page is unreachable, we can't inject stuf in it
// not sure how to detect it? tab doesn't have any interesting attributes
// firefox sets tab.title to "Server Not Found"? (TODO also see isOk logic below)
// TODO in this case, could set browser action to open a new tab (i.e. search) or something?
await defensify(inject, 'sidebar injection')();
// TODO crap, at first I forgot () at the end, and flow didn't complain which resulted in flakiness wtf??
const visits = await getVisits(url);
let {icon, title, text} = getIconStyle(visits);
// TODO move to getIconStyle??
if (visits instanceof Visits) {
title = `${title}\nCanonical: ${visits.normalised_url}`;
}
// ugh, many of these are not supported on android.. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/pageAction
// TODO not sure if can benefit from setPopup?
for (const action of (await actions())) {
// ugh, some of these only present in browserAction..
if (action.setTitle) {
// $FlowFixMe
action.setTitle({
tabId: tabId,
title: title,
});
}
if (action.setIcon) {
// $FlowFixMe
action.setIcon({
tabId: tabId,
path: icon,
});
}
if (action.setBadgeText) {
// $FlowFixMe
action.setBadgeText({
tabId: tabId,
text: text,
});
}
}
/* NOTE: few things here
* 1. browser action is only shown in the 'settings on android' (and no icon), so we're using page action
* 2. page action icon can't be changed on android, so we're only showing it when there are contexts
* otherwise, we're relying on 'mobile_sidebar_injector' to open the sidebar
*/
if (await isMobile()) {
const action = chrome.pageAction;
const interesting = [
'images/ic_visited_48.png',
'images/ic_relatives_48.png',
].includes(icon); // FIXME meh. hacky
// TODO make dependent on options?
if (interesting) {
action.show(tabId);
} else {
// not sure if this is really needed, but I feel like it persists otherwise on android
action.hide(tabId);
}
}
// TODO ok, could bind blacklist here as well.. but later
// TODO wonder if I can do exhaustive check?
if (visits instanceof Error) {
// TODO share code with the Visits branch
await chromeTabsExecuteScriptAsync(tabId, {
code: `bindError("${visits.message}")`
});
} else if (visits instanceof Visits) {
// right, we can't inject code into error pages (effectively, internal). For these, display popup instead of sidebar?
// TODO and show system wide notification instead of tab notification?
// https://stackoverflow.com/questions/32761782/can-a-chrome-extension-run-code-on-a-chrome-error-page-i-e-err-internet-disco
// https://stackoverflow.com/questions/37093152/unchecked-runtime-lasterror-while-running-tabs-executescript-cannot-access-cont
// a little hacky, but kinda works? in Firefox too apparently
const isOk = (await chromeTabsGet(tabId)).favIconUrl != 'chrome://global/skin/icons/warning.svg';
// TODO maybe store last time we showed it so it's not that annoying... although I definitely need js popup notification.
const locs = visits.self_contexts().map(l => l == null ? null : l.title);
if (locs.length !== 0) {
const msg = `${locs.length} contexts!\n${locs.join('\n')}`;
if (opts.contexts_popup_on) {
await showTabNotification(tabId, msg);
}
}
if (isOk) {
// TODO even compiling this takes 50ms if 10K visits??
// faster means of communication are going to require
// so perhaps instead, truncate and suggest to use 'search-like' interface
await chromeTabsExecuteScriptAsync(tabId, {
code: `bindSidebarData(${JSON.stringify(visits)})`
});
} else {
console.warn("TODO implement binding visits to popup?");
}
}
}
// todo ugh. this can be tested on some static page... I guess?
async function markVisited(tabId) {
const mresults = await chromeTabsExecuteScriptAsync(tabId, {
code: `
link_elements = document.getElementsByTagName("a");
Array.from(link_elements).map(el => el.href)
`
});
function is_bad(u: ?string): boolean {
if (u == null) {
return true;
}
return u === '#' || u.startsWith('javascript:');
}
// not sure why it's returning array..
const results = unwrap(mresults[0]);
const unique = Array.from(new Set(results));
const good_urls = unique.filter(u => !is_bad(u));
const blacklist = await Blacklist_get();
// TODO ugh. filter can't be async, so we have to do this separately...
const res = [];
for (const u of good_urls) {
const ur = await blacklist.contains(u);
if (ur === null) {
res.push(u);
}
}
// TODO check if zero? not sure if necessary...
// TODO maybe, I need a per-site extension?
const resp = await queryBackendCommon<JsonArray>({
urls: res,
}, 'visited');
// TODO ok, we received exactly same elements as in res. now what??
// TODO cache results internally? At least for visited. ugh.
// TODO make it custom option?
const vis = {};
for (var i = 0; i < res.length; i++) {
vis[res[i]] = resp[i];
}
// TODO make a map from it..
// TODO use CSS from settings?
// TODO document how it can be configured
await chromeTabsInsertCSS(tabId, {
code: `
.promnesia-visited:after {
content: "⚫";
color: #FF4500;
vertical-align: super;
font-size: smaller;
/* prevent selecting along with the text */
user-select: none;
position:absolute;
z-index:100;
}
`
});
await chromeTabsExecuteScriptAsync(tabId, {
code: `
vis = ${JSON.stringify(vis)}; // madness!
{
for (var i = 0; i < link_elements.length; i++) {
const a_tag = link_elements[i];
let url = a_tag.href;
if (url == null) {
continue;
}
if (vis[url] === true) {
// console.log("adding class to ", a_tag);
a_tag.classList.add('promnesia-visited');
}
}
}
`
});
}
// ok, looks like this one was excessive..
// chrome.tabs.onActivated.addListener(updateState);
function isSpecialProtocol(url: string): boolean {
// TODO eh, maybe makes more sense to only allow http[s]/ftp/file?
const pro = new URL(url).protocol;
if ([
'chrome:',
'chrome-devtools:',
'chrome-extension:',
'moz-extension:',
'about:', // e.g. about:addons or about:devtool
].includes(pro)) {
return true;
}
return false;
}
function ignored(url: string): boolean {
if ([
'https://www.google.com/_/chrome/newtab?ie=UTF-8', // ugh, not sure how to dix that properly
'about:blank', // not sure why about:blank is loading like 5 times.. but this seems to fix it
].includes(url)) {
return true;
}
// TODO might be bad url
if (isSpecialProtocol(url)) {
return true;
}
return false;
}
/*
// TODO ehh... not even sure that this is correct thing to do...
// $FlowFixMe
chrome.webNavigation.onDOMContentLoaded.addListener(detail => {
get_options(opts => {
if (!opts.dots) {
return;
}
const url = unwrap(detail.url);
if (detail.frameId != 0) {
ldebug('ignoring child iframe for %s', url);
return;
}
if (ignored(url)) {
ldebug("ignoring %s", url);
return;
}
// https://kk.org/thetechnium/
ldebug('finished loading DOM %s', detail);
markVisited(detail.tabId, opts);
// updateState();
});
});
*/
// chrome.tabs.onReplaced.addListener(updateState);
chrome.tabs.onCreated.addListener((tab) => {
ldebug("onCreated %s", tab);
});
// $FlowFixMe
chrome.tabs.onUpdated.addListener(defensify(async (tabId, info, tab) => {
// too spammy in logs
delete tab.favIconUrl;
delete info.favIconUrl;
//
ldebug("onUpdated %s %s", tab, info);
const url = tab.url;
if (!url) { /* on Vivaldi I've seen url being "" */
ldebug('onUpdated: ignoring as URL is not set');
return;
}
// TODO make logging optional? not sure if there are any downsides
if (ignored(url)) {
linfo('onUpdated: ignored explicitly %s', url);
return;
}
// right, tab updated triggered quite a lot, e.g. when the title is blinking
// ok, so far there are basically two cases
// 1. you open new tab. in that case 'url' won't be passed but onDomContentLoaded will be triggered
// 2. you navigate within the same tab, e.g. on youtube. then url will be passed, but onDomContentLoaded doesn't trigger. TODO not sure if it's always the case. maybe it's only YT
// TODO shit, so we might need to hide previous dots? ugh...
// TODO vvvv these might need to be cleaned up; not sure how relevant...
// page refresh: loading -> complete (no url at any point)
// clicking on link: loading (url) -> complete
// opening new link: loading -> loading (url) -> complete
// ugh. looks like 'complete' is the most realiable???
// but, I checked with 'complete' and sometimes it would reload many things with loading -> complete..... shit.
// also if you, say, go to web.telegram.org it's gonna show multiple notifications due to redirect... but perhaps this can just be suppressed..
if (info['status'] != 'complete') {
return;
}
linfo('requesting! %s', url);
try {
await updateState(tab);
} catch (error) {
const message = error.message;
if (message == null) {
throw error;
}
if (message.includes('Invalid tab ID')) {
console.warn('Error %o ignored; most likely due to closed tab', error);
return;
}
if (message.includes('An unexpected error occurred')) {
console.warn('Error %o ignored; presumably bug in Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1397667', error);
// also that https://bugzilla.mozilla.org/show_bug.cgi?id=1290016
return;
}
throw error;
}
}, 'onUpdated'));
export async function getActiveTab(): Promise<chrome$Tab> {
const tabs = await chromeTabsQueryAsync({
'currentWindow': true,
'active': true,
});
// TODO can it be empty at all??
if (tabs.length > 1) {
console.error("Multiple active tabs: %o", tabs); // TODO handle properly?
}
const tab = tabs[0];
return tab;
}
async function showActiveTabNotification(text: string, color: string): Promise<void> {
const atab = await getActiveTab();
await showTabNotification(unwrap(atab.id), text, color);
}
async function handleMarkVisited() {
// TODO actually use mark visited setting?
// const opts = await get_options_async();
const atab = await getActiveTab();
const url = unwrap(atab.url);
const tid = unwrap(atab.id);
if (ignored(url)) {
await showIgnoredNotification(tid, url);
} else {
const blacklist = await Blacklist_get();
const bl = await blacklist.contains(url);
if (bl != null) {
await showBlackListedNotification(tid, new Blacklisted(url, bl));
} else {
await markVisited(tid);
}
}
}
async function handleOpenSearch() {
// TODO get current tab url and pass as get parameter?
chrome.tabs.create({ url: "search.html" });
}
const onMessageCallback = async (msg) => { // TODO not sure if should defensify here?
const method = msg.method;
if (method == Methods.GET_SIDEBAR_VISITS) {
const atab = await getActiveTab();
const url = unwrap(atab.url);
if (!ignored(url)) { // TODO shouldn't have been requested in the first place?
const visits = await getVisits(unwrap(atab.url));
if (visits instanceof Visits) {
return visits;
} else {
// hmm. generally shouldn't happen, since sidebar is not bound on blacklisted urls
lerror("Shouldn't have happened! %s", visits);
}
}
// TODO err. not sure what's happening here...
// if i'm using await in return type, it expects me to return visits instead of true/false??
// is it automatically detecting whether it's a promise or not??
// perhaps async automatically uncurries last argument?
// could be Firefox only?
// sendResponse(visits);
// return true; // this is important!! otherwise message will not be sent?
} else if (method == Methods.SEARCH_VISITS_AROUND) {
// TODO reuse handleOpenSearch?
const utc_timestamp_s: number = msg.utc_timestamp_s;
const params = new URLSearchParams();
params.append('utc_timestamp', utc_timestamp_s.toString());
const search_url = chrome.extension.getURL('search.html') + '?' + params.toString();
chrome.tabs.create({'url': search_url});
} else if (method == Methods.MARK_VISITED) {
await handleMarkVisited();
} else if (method == Methods.OPEN_SEARCH) {
await handleOpenSearch();
}
return false;
};
/*
On android, clicking on icon in address bar doesn't seem to work.. however clicking in menu triggers this action?
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Differences_between_desktop_and_Android#User_interface
popup is available for pageAction?? can use it for blacklisting/search?
*/
async function registerActions() {
// NOTE: on mobile, this sets action for both icon (if it's displayed) and in the menu
for (const action of (await actions())) {
// $FlowFixMe
action.onClicked.addListener(defensify(injectSidebar, 'action.onClicked'));
}
}
export async function injectSidebar(tab: chrome$Tab) {
const url = unwrap(tab.url);
const tid = unwrap(tab.id);
if (ignored(url)) {
// TODO tab notification?
notify(`${url} can't be handled`);
return;
}
const blacklist = await Blacklist_get();
const bl = await blacklist.contains(url);
if (bl != null) {
await showBlackListedNotification(tid, new Blacklisted(url, bl));
// TODO show popup; suggest to whitelist?
} else {
// TODO ugh. messy
await chromeTabsExecuteScriptAsync(tid, {file: 'sidebar.js'});
await chromeTabsExecuteScriptAsync(tid, {code: 'toggleSidebar();'});
}
}
// TODO reuse these in webpack config...
const COMMAND_SEARCH = 'search';
const COMMAND_MARK_VISITED = 'mark_visited';
const onCommandCallback = defensify(async cmd => {
// ok apparently background page shouldn't communicate with itself via messages. wonder how could it work for me before..
// https://stackoverflow.com/a/35858654/706389
if (cmd === COMMAND_MARK_VISITED) {
await handleMarkVisited();
} else if (cmd === COMMAND_SEARCH) {
await handleOpenSearch();
} else {
// TODO throw?
lerror("unexpected command %s", cmd);
}
}, 'onCommand');
async function blacklist(e): Promise<void> {
const url = unwrap(e.pageUrl);
const atab = await getActiveTab();
const tabId = unwrap(atab.id);
// TODO I'm really not sure it's the right way to do this..
let prompt = `Blacklist. Supported formats:
- domain.name, e.g.: web.telegram.org
Will exclude whole Telegram website.
- http://exact/match, e.g.: http://github.com
Will only exclude Github main page. Subpages will still work.
- /regul.r.*expression/, e.g.: /github.*/username/
Quick way to exclude your own Github repostitories.
`;
const to_blacklist = await chromeTabsExecuteScriptAsync(tabId, {
code: `prompt(\`${prompt}\`, "${url}");`
});
if (to_blacklist == null) {
console.info('user chose not to blacklist %s', url);
return;
}
// TODO not sure if it should be normalised? just rely on regexes, it should be fine 99% of time?
console.debug('blacklisting %s', to_blacklist);
const opts = await get_options_async();
opts.blacklist += (opts.blacklist.endsWith('\n') ? '' : '\n') + to_blacklist;
/*
TODO ''.split('\n') gives an emptly line, which would block local files
will fix later if necessary, it's not a big issue I guess
*/
const ll = opts.blacklist.split(/\n/).length;
// TODO could open sidebar here and display blacklist??
await showActiveTabNotification(`Added ${to_blacklist} to blacklist (${ll} items now)`, 'blue');
await setOptions(opts);
}
const MENU_BLACKLIST = 'menu_blacklist';
const MENU_MARK_VISITS = 'menu_mark_visits';
const MENU_SEARCH = 'menu_search'
// looks like onClicked is more portable...
const onMenuClickedCallback = defensify(async (info) => {
const mid = info.menuItemId;
if ( mid === MENU_BLACKLIST) {
await blacklist(info);
} else if (mid === MENU_MARK_VISITS) {
await handleMarkVisited();
} else if (mid === MENU_SEARCH) {
await handleOpenSearch();
}
}, 'onMenuClicked');
/*
Right, that's a hack for some nasty bug/behaviour that happens both in firefox and chrome.
Basically, if you have tabs open for html pages within the extensions (e.g. moz-extensions://<id>/search.html ), each of them ends up with a copy of background page.
That results it multiple responses for commands, messages etc.
Only relevantinformation I could found about that is https://stackoverflow.com/questions/30856001/why-does-chrome-tabs-create-create-2-tabs , but that didn't really help
This behaviour is tested by test_duplicate_background_pages to prevent regressions
*/
// TODO maybe this is what needs to be persisted?
var backgroundInitialised = false; // should be synchronous hopefully?
function initBackground() {
/* better set early to minimize the potential for races? */
backgroundInitialised = true;
// $FlowFixMe
chrome.runtime.onMessage.addListener(onMessageCallback);
registerActions();
// TODO make it defensive in case of error tabs? if it fails then can be conservative and ignore menu etc anyway
isAndroid().then(android => {
if (android) {
return;
}
// $FlowFixMe // err, complains at Promise but nevertheless works
chrome.commands.onCommand.addListener(onCommandCallback);
// TODO?? Unchecked runtime.lastError: Cannot create item with duplicate id blacklist-domain on Chrome
chrome.contextMenus.create({
'id' : MENU_BLACKLIST,
'contexts' : ['page', 'browser_action'],
'title' : "Blacklist (domain/specific page/subpages)",
});
chrome.contextMenus.create({
'id' : MENU_MARK_VISITS,
'contexts' : ['page', 'browser_action'],
'title' : "Mark visited urls",
});
chrome.contextMenus.create({
'id' : MENU_SEARCH,
'contexts' : ['page', 'browser_action'],
'title' : "Search in browsing history",
})
// $FlowFixMe // err, complains at Promise but nevertheless works
chrome.contextMenus.onClicked.addListener(onMenuClickedCallback);
})
}
/*
The idea is that each page pokes background.
If background happens to be extensions' background, it's ignored; otherwise we're trying to register callbacks.
*/
chrome.runtime.onMessage.addListener((info: any, sender: chrome$MessageSender) => {
if (info.method != "INJECT_BACKGROUND_CALLBACKS") {
console.debug("ignoring %o %o; %s", info, sender, backgroundInitialised);
return;
}
console.log("onmessage %o %o", info, sender);
const aurl = sender.tab == null ? null : sender.tab.url;
console.info("Registering background page callbacks %s", aurl);
if (backgroundInitialised) {
console.debug("background already initialised");
return;
}
// TODO elaborate
if (aurl && isSpecialProtocol(aurl)) {
lwarn("Suppressing special background page %s", aurl);
return;
}
/* TODO not sure if ok or not to await? it shouldn't be blocking right? */
initBackground();
});