-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcontent-script-handler.ts
More file actions
76 lines (70 loc) · 3.19 KB
/
content-script-handler.ts
File metadata and controls
76 lines (70 loc) · 3.19 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
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { executeScript } from "@api/chrome/script"
import { createTab } from "@api/chrome/tab"
import { ANALYSIS_ROUTE, LIMIT_ROUTE } from "@app/router/constants"
import optionHolder from "@service/components/option-holder"
import whitelistHolder from "@service/components/whitelist-holder"
import limitService from "@service/limit-service"
import siteService from "@service/site-service"
import { getAppPageUrl } from "@util/constant/url"
import { extractFileHost, extractHostname } from "@util/pattern"
import badgeManager from "./badge-manager"
import { collectIconAndAlias } from "./icon-and-alias-collector"
import MessageDispatcher from "./message-dispatcher"
const handleOpenAnalysisPage = (sender: ChromeMessageSender) => {
const { tab, url } = sender || {}
if (!url) return
const host = extractFileHost(url) || extractHostname(url)?.host
const newTabUrl = getAppPageUrl(ANALYSIS_ROUTE, { host })
const tabIndex = tab?.index
const newTabIndex = tabIndex ? tabIndex + 1 : undefined
createTab({ url: newTabUrl, index: newTabIndex })
}
const handleOpenLimitPage = (sender: ChromeMessageSender) => {
const { tab, url } = sender || {}
if (!url) return
const newTabUrl = getAppPageUrl(LIMIT_ROUTE, { url })
const tabIndex = tab?.index
const newTabIndex = tabIndex ? tabIndex + 1 : undefined
createTab({ url: newTabUrl, index: newTabIndex })
}
const handleInjected = async (sender: ChromeMessageSender) => {
const tabId = sender?.tab?.id
if (!tabId) return
collectIconAndAlias(tabId)
badgeManager.updateFocus()
executeScript(tabId, ['content_scripts.js'])
}
/**
* Handle request from content script
*
* @param dispatcher message dispatcher
*/
export default function init(dispatcher: MessageDispatcher) {
dispatcher
// Judge is in whitelist
.register<{ host?: string, url?: string }, boolean>('cs.isInWhitelist', ({ host, url } = {}) => !!host && !!url && whitelistHolder.contains(host, url))
// Need to print the information of today
.register<void, boolean>('cs.printTodayInfo', async () => {
const option = await optionHolder.get()
return !!option.printInConsole
})
.register<string, timer.limit.Item[]>('cs.getLimitedRules', url => limitService.getLimited(url))
.register<string, timer.limit.Item[]>('cs.getRelatedRules', url => limitService.getRelated(url))
.register<void, void>('cs.openAnalysis', (_, sender) => handleOpenAnalysisPage(sender))
.register<void, void>('cs.openLimit', (_, sender) => handleOpenLimitPage(sender))
.register<void, void>('cs.onInjected', async (_, sender) => handleInjected(sender))
// Get sites which need to count run time
.register<string, timer.site.SiteKey | null>('cs.getRunSites', async url => {
const { host } = extractHostname(url) || {}
if (!host) return null
const site: timer.site.SiteKey = { host, type: 'normal' }
const exist = await siteService.get(site)
return exist?.run ? site : null
})
}