-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathbackground.tsx
95 lines (85 loc) · 2.44 KB
/
background.tsx
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
95
import 'chrome-extension-async'
import {
findMatchedHosts,
getHostKey,
getActiveTab,
getHosts,
setLastFocusedWindowId
} from 'libs'
const getURL = ({ url }) => new window.URL(url)
const reloadTab = (tab) => chrome.tabs.reload(tab.id)
const methodMap = {
getData: async (message, { tab, url }, sendResponse) => {
const { host, protocol } = url
const hosts = await getHosts()
const matchedHosts = findMatchedHosts(hosts, url, message)
if (matchedHosts.length === 0) {
sendResponse({ host, protocol, tab })
} else {
const matchedHost = matchedHosts[0]
const hostKey = getHostKey(matchedHost)
const data = await chrome.storage.sync.get(hostKey)
const customjs = data[hostKey]
sendResponse({ customjs, host, protocol, tab, matchedHost })
}
},
setData: async (message, _, sendResponse) => {
const { matchedHost, customjs } = message
const hostKey = getHostKey(matchedHost)
try {
await chrome.storage.sync.set({ [hostKey]: customjs })
sendResponse()
} catch (err) {
sendResponse(err)
}
},
removeData: (message, { url }) => {
const { isRegex, pattern } = message
if (isRegex) {
chrome.storage.sync.remove(pattern)
} else {
chrome.storage.sync.remove(url.origin)
}
},
goTo: (message, { tab }) => chrome.tabs.update(tab.id, { url: message.link })
}
const onMessage = async (message, sender, sendResponse) => {
const { domain } = message
try {
const tab = await getActiveTab()
const url = domain ? getURL({ url: domain }) : getURL(tab)
const { method, reload } = message
const func = methodMap[method]
if (func && typeof func === 'function') {
func(message, { tab, url }, sendResponse)
} else {
console.error(`Unknown method: ${method}`)
sendResponse({ source: '', config: {} })
}
if (reload) {
reloadTab(tab)
}
} catch (e) {
sendResponse({ error: e.message })
}
}
const onFocusChanged = (windowId) => {
if (windowId < 0) {
return
}
setLastFocusedWindowId(windowId)
}
chrome.runtime.onMessage.addListener((...args) => {
onMessage(...args)
return true
})
chrome.windows.onFocusChanged.addListener(onFocusChanged)
chrome.runtime.onInstalled.addListener((details) => {
chrome.tabs.query({ currentWindow: true, active: true }, (tabs) => {
if (tabs.length <= 0) {
return
}
const { windowId } = tabs[0]
setLastFocusedWindowId(windowId)
})
})