-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.ts
94 lines (90 loc) · 3.01 KB
/
background.ts
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
export { }
// Note:点击图标时,打开 sidepanel
/* chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error(error)); */
chrome.tabs.onActivated.addListener(async (activeInfo) => {
// console.log('onActivated: ', activeInfo);
const tabId = activeInfo.tabId;
const tab = await chrome.tabs.get(tabId);
if (!tab.url) {
await chrome.sidePanel.setOptions({
tabId,
enabled: false
});
return;
};
const url = new URL(tab.url);
if (url.origin === 'https://www.notion.so' || url.origin.endsWith('.notion.site')) {
await chrome.sidePanel.setOptions({
tabId,
path: 'sidepanel.html',
enabled: true
});
} else {
// Disables the side panel on all other sites
await chrome.sidePanel.setOptions({
tabId,
enabled: false
});
}
});
chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
// console.log('onUpdated: ', tabId, info, tab);
if (!tab.url) {
await chrome.sidePanel.setOptions({
tabId,
enabled: false
});
return;
};
const url = new URL(tab.url);
if (url.origin === 'https://www.notion.so' || url.origin.endsWith('.notion.site')) {
await chrome.sidePanel.setOptions({
tabId,
path: 'sidepanel.html',
enabled: true
});
} else {
// Disables the side panel on all other sites
await chrome.sidePanel.setOptions({
tabId,
enabled: false
});
}
});
// Note: 点击图标时,打开 sidepanel,如果不在 notion 页面上,通知
chrome.action.onClicked.addListener(async (tab) => {
// console.log('onClicked: ', tab);
if (!tab.url) {
return;
};
const url = new URL(tab.url);
const tabId = tab.id;
if (url.origin === 'https://www.notion.so' || url.origin.endsWith('.notion.site')) {
await chrome.sidePanel.open({
tabId,
});
} else {
// Note: 显示一个弹窗说 Notion Flow 插件仅在 Notion 页面上可用以保持适当的边界感
/* chrome.scripting.executeScript({
target: {tabId: tab.id},
files: [chrome.runtime.getURL('notify.js')]
}); */
chrome.notifications.create({
type: 'basic',
iconUrl: chrome.runtime.getURL('assets/icon.png'),
title: 'Notion Flow',
message: 'Notion Flow only work on Notion Page',
});
}
});
// Note: 检测首次安装和更新
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install" || details.reason == "update"){
chrome.tabs.create({url:chrome.runtime.getURL("options.html")}, function (tab) {
var thisVersion = chrome.runtime.getManifest().version;
console.log(`Open Type: ${details.reason}; Version: ${details.previousVersion} to ${thisVersion} !`);
});
}
});