background.js
1.41 KB
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
'use strict';
chrome.runtime.onInstalled.addListener(function()
{
chrome.declarativeContent.onPageChanged.removeRules(undefined, function()
{
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
pageUrl: {hostSuffix: '.notion.so'},
})],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
chrome.tabs.onActivated.addListener(function(activeInfo)
{
let tabId = activeInfo.tabId;
chrome.tabs.get(tabId, tab => {
if (tab.url && new URL(tab.url).host.indexOf('.notion.so') > 0)
{
chrome.storage.local.set({ notionTabId: tabId });
}
});
});
chrome.runtime.onConnect.addListener(function(port)
{
//Popup messages
port.onMessage.addListener(function(msg)
{
chrome.storage.local.get(['notionTabId'], ({ notionTabId }) =>
{
if (notionTabId != null)
{
try
{
chrome.tabs.sendMessage(notionTabId, msg, function(resp)
{
console.log(resp);
port.postMessage(resp);
});
}
catch(e)
{
if (e.message.indexOf('disconnected') >= 0)
{
port.postMessage({msg: 'NotionDisconnected', error: true});
}
else
{
port.postMessage({msg: 'Exception', errMsg: e.message, error: true});
}
}
}
else
{
port.postMessage({msg: 'NotionNotFound', error: true, origMsg: msg});
}
});
});
});