/
background.js
71 lines (62 loc) · 1.72 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
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status != "complete" || !tab.active) {
return;
}
async function execute() {
function isAsanaPage() {
return isMatch("asana", "asana.com", ["Opening link in Asana"]);
}
function isNotionPage() {
return isMatch("notion", "notion.so", ["Redirecting to your Notion app"]);
}
function isZoomPage() {
return isMatch("zoom", "zoom.us", [
"Your meeting has been launched",
"on the dialog shown by your browser",
]);
}
async function isMatch(name, origin, searches) {
const regex = new RegExp(`https://[a-z]*\\.${origin}`);
const isSiteActive = (await chrome.storage.local.get("sites")).sites[
name
];
return (
isSiteActive &&
window.location.origin.match(regex) &&
searches.find((search) =>
window.find(
search,
/* aCaseSensitive */ false,
/* aBackwards */ false,
/* aWrapAround */ true
)
)
);
}
const checks = [isAsanaPage, isNotionPage, isZoomPage];
for (let i = 0; i < checks.length; i++) {
if (await checks[i]()) {
return true;
}
}
return false;
}
chrome.scripting
.executeScript({
target: { tabId, allFrames: true },
func: execute,
})
.then((response) => {
const { result } = response[0];
if (result) {
setTimeout(() => {
chrome.tabs.remove(tabId);
chrome.storage.local
.get("tally")
.then(({ tally }) =>
chrome.storage.local.set({ tally: (tally || 0) + 1 })
);
}, 1000);
}
});
});