-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
55 lines (46 loc) · 1.37 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
import {currentWindowTabQuery, METHOD_TAG,} from './js/module/messageModule.js';
let tabInfo = {
tabId: null,
windowId: null
}
//监听快捷键,并发消息给Content.js,使其执行相关命令
chrome.commands.onCommand.addListener((command) => {
execCommand(command)
})
function execCommand(command) {
if (tabInfo?.tabId) {
chrome.tabs.sendMessage(tabInfo.tabId, {
method: command
})
} else {
//没有获取到tabId,通知当前tab,并存储
chrome.tabs.query(currentWindowTabQuery, ([tab]) => {
if (tab) {
//通知
chrome.tabs.sendMessage(tab.id, {
method: command
})
//存储
tabInfo.tabId = tab.id;
tabInfo.windowId = tab.windowId;
}
})
}
}
//监听Content.js的消息,获取和存储共享的页面index
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.method) {
case METHOD_TAG.GET_INDEX:
sendResponse(tabInfo)
break;
case METHOD_TAG.SET_INDEX:
tabInfo = {
tabId: null,
windowId: null
}
tabInfo.tabId = message.tabId;
tabInfo.windowId = message.windowId;
break;
}
return true;
})