This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
worker.js
60 lines (55 loc) · 1.7 KB
/
worker.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
/*
* notion-enhancer
* (c) 2021 dragonwocky <[email protected]> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
'use strict';
function focusMenu() {
chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT }, (tabs) => {
const url = chrome.runtime.getURL('repo/menu/menu.html'),
menu = tabs.find((tab) => tab.url.startsWith(url));
if (menu) {
chrome.tabs.highlight({ 'tabs': menu.index });
} else chrome.tabs.create({ url });
});
}
chrome.browserAction.onClicked.addListener(focusMenu);
function focusNotion() {
chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT }, (tabs) => {
const notion = tabs.find((tab) => {
const url = new URL(tab.url),
matches = url.host.endsWith('.notion.so') || url.host.endsWith('.notion.site');
return matches;
});
if (notion) {
chrome.tabs.highlight({ 'tabs': notion.index });
} else chrome.tabs.create({ url: 'https://notion.so/' });
});
}
function reload() {
chrome.tabs.query({ windowId: chrome.windows.WINDOW_ID_CURRENT }, (tabs) => {
const menu = chrome.runtime.getURL('repo/menu/menu.html');
tabs.forEach((tab) => {
const url = new URL(tab.url),
matches =
url.host.endsWith('.notion.so') ||
url.host.endsWith('.notion.site') ||
tab.url.startsWith(menu);
if (matches) chrome.tabs.reload(tab.id);
});
});
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case 'focusMenu':
focusMenu();
break;
case 'focusNotion':
focusNotion();
break;
case 'reload':
reload();
break;
}
return true;
});