-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.js
72 lines (58 loc) · 1.66 KB
/
event.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
'use strict';
import * as utils from "./js/utils.js";
import * as store from "./js/store.js";
let browser = self.browser || self.chrome;
const handler = async (id) => {
let tab;
try {
tab = await browser.tabs.get(id);
} catch (e) {
console.log(e);
return;
}
let feeds = [];
if (tab.url && tab.url.startsWith("http")) {
try {
let x = await browser.scripting.executeScript({
target: {tabId: id},
files: ['./js/page.js'],
});
feeds = x[0].result || [];
feeds = [...new Map(feeds.map(f => {
if (!f.url.startsWith('http')) {
if (f.url.startsWith('/')) {
f.url = (new URL(tab.url)).origin + f.url;
} else {
f.url = tab.url + f.url;
}
}
return [f.url, f];
})).values()];
} catch (e) {
console.error(e);
}
}
let popup = "";
if (feeds.length > 0) {
await browser.action.setIcon({
path: { "128": "icons/icon.png" },
tabId: id
});
popup = `./popup.html?links=${encodeURIComponent(JSON.stringify(feeds))}`;
store.saveIcon(utils.getSiteTitle(tab.url), tab.favIconUrl);
}
await browser.action.setPopup({
popup: popup,
tabId: id,
});
};
browser.runtime.onInstalled.addListener(async details => {
store.fixBookmarks();
browser.alarms.create("sync-feed", {periodInMinutes:1});
});
browser.tabs.onActivated.addListener(info => handler(info.tabId));
browser.tabs.onUpdated.addListener(id => handler(id));
browser.action.onClicked.addListener((tab) => {
browser.tabs.create({url:browser.runtime.getURL("./list.html")});
});
browser.alarms.onAlarm.addListener(async e => utils.syncAll());