-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
137 lines (124 loc) · 3.63 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
const ignoreProtocols = [
"https://",
"http://",
"ftp://",
"file://",
"data:",
"mailto:",
"tel:",
"javascript:",
"blob:",
"ws://",
"wss://",
"about:",
"view-source:",
"chrome:"
];
class AppLink {
constructor(url, origin) {
this.url = url;
this.origin = origin;
this.visitedAt = new Date().toJSON();
}
}
async function storeAppLink(appLink) {
browser.storage.local.set({appLink: appLink}).then(
() => console.log("Url stored successfully"),
(error) => console.log(`Error: ${error}`)
);
try {
data = await browser.storage.local.get("history");
if (data.history) {
historical = JSON.parse(data.history);
historical.unshift(appLink);
} else {
historical = [appLink];
}
} catch (e) {
console.log(`Error: ${e}`);
historical = [appLink];
}
browser.browserAction.setBadgeText({ text: `${historical.length}` });
browser.storage.local.set({history: JSON.stringify(historical)}).then(
() => console.log("Url stored successfully in history"),
(error) => console.log(`Error: ${error}`)
);
}
function shouldIgnore(url) {
for (let i = 0; i < ignoreProtocols.length; i++) {
if (url.startsWith(ignoreProtocols[i])) {
return true;
}
}
return false;
}
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url && shouldIgnore(changeInfo.url)) {
browser.storage.local.set({previous: new URL(changeInfo.url).origin});
}
});
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
if (!details.url.includes("://")) {
return;
}
if (shouldIgnore(details.url)) {
return;
}
await registerLink(details.url)
});
async function registerLink(url) {
try {
prev = (await browser.storage.local.get("previous")).previous;
} catch (e) {
console.log(`Error: ${e}`);
prev = "manual";
}
appLink = new AppLink(url, prev);
storeAppLink(appLink);
browser.browserAction.setIcon({
path: "icons/default-icon-active.png"
});
browser.browserAction.setBadgeBackgroundColor({ color: '#77216F' });
}
function handleRequest(request, sender, sendResponse) {
console.log(request, sender);
switch (request.command) {
case "showLink":
browser.storage.local.get("appLink").then(
(data) => {
if (data) {
sendResponse({data: data.appLink});
} else {
sendResponse({data: null});
}
},
(error) => {
sendResponse({data: null});
console.log(`Error: ${error}`)
}
);
return true;
case "clearData":
try {
browser.storage.local.set({history: "[]"});
} catch {}
try {
browser.storage.local.set({appLink: undefined});
} catch {}
try {
browser.storage.local.set({previous: undefined});
} catch {}
browser.browserAction.setIcon({
path: "icons/default-icon.png"
});
browser.browserAction.setBadgeText({ text: '' });
return true;
case "handleQrLink":
registerLink(request.data);
sendResponse({data: null})
return true;
default:
return false;
}
}
browser.runtime.onMessage.addListener(handleRequest);