-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
68 lines (60 loc) · 1.88 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
const sigarraRegex = /.*:\/\/sigarra\.up\.pt\/feup\/.*/;
// Add default values for each option here
const popupOptions = {
navbar: "on",
shortcuts: "on",
autoLogin: "off",
font: "on",
};
const reloadFEUPSigarraPages = () => {
chrome.tabs.query({ url: "*://sigarra.up.pt/feup/*" }, (tabs) => {
tabs.forEach((tab) => {
chrome.tabs.reload(tab.id);
});
});
};
chrome.runtime.onInstalled.addListener((object) => {
if (object.reason === "install") {
reloadFEUPSigarraPages();
if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) {
chrome.tabs.create({
url: chrome.runtime.getURL("html/autorize.html"),
});
} else {
chrome.tabs.create({
url: chrome.runtime.getURL("html/installed.html"),
});
}
chrome.storage.local.set(popupOptions);
}
if (object.reason === "update") {
reloadFEUPSigarraPages();
for (const opt in popupOptions) {
if (chrome.storage.local.get(opt) == null)
chrome.storage.local.set({ [opt]: popupOptions[opt] });
}
}
});
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (!sender.tab.active) {
console.log("tab not active skipping message...");
return;
}
if (message.type == "login") {
const cookie = await chrome.cookies.get({
name: "SI_SESSION",
url: sender.tab.url,
});
console.log(cookie);
if (cookie == null || cookie.value === "0") {
sendResponse(false);
return;
}
message.auto_login.verifed = true;
await chrome.storage.local.set({ auto_login: message.auto_login });
sendResponse(true);
}
});
chrome.permissions.onRemoved.addListener((permissions) => {
//TODO:
});