forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script-handler.ts
More file actions
78 lines (74 loc) · 3.26 KB
/
content-script-handler.ts
File metadata and controls
78 lines (74 loc) · 3.26 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
77
78
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { getUrl } from "@api/chrome/runtime"
import { createTab } from "@api/chrome/tab"
import { ANALYSIS_ROUTE, LIMIT_ROUTE } from "@app/router/constants"
import whitelistHolder from "@service/components/whitelist-holder"
import itemService from "@service/item-service"
import limitService from "@service/limit-service"
import optionService from "@service/option-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 appUrl = getAppPageUrl(true, ANALYSIS_ROUTE) + "?host=" + host
const newTabUrl = getUrl(appUrl)
const tabIndex = tab?.index
const newTabIndex = tabIndex ? tabIndex + 1 : null
createTab({ url: newTabUrl, index: newTabIndex })
}
const handleOpenLimitPage = (sender: ChromeMessageSender) => {
const { tab, url } = sender || {}
if (!url) return
const limitUrl = getAppPageUrl(true, LIMIT_ROUTE) + "?url=" + url
const newTabUrl = getUrl(limitUrl)
const tabIndex = tab?.index
const newTabIndex = tabIndex ? tabIndex + 1 : null
createTab({ url: newTabUrl, index: newTabIndex })
}
/**
* Handle request from content script
*
* @param dispatcher message dispatcher
*/
export default function init(dispatcher: MessageDispatcher) {
dispatcher
// Increase the visit time
.register<string | { host: string, url: string }, void>('cs.incVisitCount', async (param) => {
let host: string, url: string = undefined
if (typeof param === 'string') {
host = param
} else {
host = param?.host
url = param?.url
}
itemService.addOneTime(host, url)
})
// Judge is in whitelist
.register<{ host?: string, url?: string }, boolean>('cs.isInWhitelist', ({ host, url } = {}) => whitelistHolder.contains(host, url))
// Need to print the information of today
.register<void, boolean>('cs.printTodayInfo', async () => {
const option = await optionService.getAllOption()
return !!option.printInConsole
})
// Get today info
.register<string, timer.stat.Result>('cs.getTodayInfo', host => itemService.getResult(host, new Date()))
// cs.getLimitedRules
.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', (_, sender) => {
collectIconAndAlias(sender)
badgeManager.updateFocus()
})
}