-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
87 lines (77 loc) · 2.49 KB
/
helpers.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
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
// websites with feature disabled by default
// because they already have the feature
export const SITES_ALREADY_WITH_FEATURE = [
'www.youtube.com',
'github.com'
]
export const asyncSendMessage = async (message) => (
new Promise(resolve => {
chrome.runtime.sendMessage(message, resp => resolve(resp))
})
)
export const asyncSetChromeSyncStorage = async(obj) => (
new Promise(resolve => {
chrome.storage.sync.set(obj, resp => resolve(resp))
})
)
// read from storage asynchronously
export const asyncReadFromStorage = (key, local = false) => (
new Promise((resolve, reject) => {
chrome.storage.sync.get(key, function(result) {
resolve(result[key])
})
})
)
// get current tab asynchronously
export const asyncGetCurrentTab = () => new Promise(resolve => (
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, tabs => (
resolve(tabs[0])
))
))
// get current URL asynchronously
export const asyncGetCurrentDomain = () => new Promise(resolve => (
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, tabs => {
resolve(new URL(tabs[0].url).hostname)
})
))
export const shouldDoOnSite = async ({ serviceName, blacklistName, whitelistName, domain }) => {
const serviceOn = await asyncReadFromStorage(serviceName)
let blacklist = JSON.parse(await asyncReadFromStorage(blacklistName) ?? JSON.stringify([]))
let whitelist = JSON.parse(await asyncReadFromStorage(whitelistName) ?? JSON.stringify([]))
return serviceOn ?
!blacklist.includes(domain)
: whitelist.includes(domain)
}
export const shouldRunServiceOnSite = async() => {
const domain = await asyncGetCurrentDomain()
return shouldDoOnSite({
serviceName: 'allSiteOn',
blacklistName: 'serviceBlacklist',
whitelistName: 'serviceWhitelist',
domain: domain
})
}
export const shouldAutoFocusOnSite = async() => {
const domain = await asyncGetCurrentDomain()
return shouldDoOnSite({
serviceName: 'autoFocus',
blacklistName: 'autoFocusBlacklist',
whitelistName: 'autoFocusWhitelist',
domain: domain
})
}
export const shouldClearSearchOnSite = async(domain) => {
domain = domain ?? await asyncGetCurrentDomain()
return shouldDoOnSite({
serviceName: 'clearSearch',
blacklistName: 'clearSearchBlacklist',
whitelistName: 'clearSearchWhitelist',
domain: domain
})
}