-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathcontent-script-handler.ts
More file actions
69 lines (64 loc) · 2.34 KB
/
Copy pathcontent-script-handler.ts
File metadata and controls
69 lines (64 loc) · 2.34 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
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { getTab } from '@api/chrome/tab'
import { getSite, saveSite } from '@service/site-service'
import { IS_ANDROID, IS_CHROME, IS_FIREFOX, IS_SAFARI, isNotTrackable } from "@util/constant/environment"
import { extractHostname, isHomepage } from "@util/pattern"
import { extractSiteName } from "@util/site"
import badgeManager from "./badge-manager"
import MessageDispatcher from "./message-dispatcher"
import { incVisitCount } from './track-server/normal'
/**
* Process the tab
*/
async function processTabInfo(tab: ChromeTab): Promise<void> {
// Not support to modify site info on Android, so skip it
if (IS_ANDROID) return
let { favIconUrl: iconUrl, url, title } = tab
if (!url || !title) return
if (isNotTrackable(url)) return
const hostInfo = extractHostname(url)
const host = hostInfo.host
if (!host) return
// localhost hosts with Chrome use cache, so keep the favIcon url undefined
IS_CHROME && /^localhost(:.+)?/.test(host) && (iconUrl = undefined)
// Only collect site name for homepage
const alias = isHomepage(url) ? extractSiteName(title) : undefined
if (!iconUrl && !alias) return
const exist = await getSite({ host, type: 'normal' })
exist.iconUrl ??= iconUrl
exist.alias ??= alias
await saveSite(exist)
}
/**
* Collect the favicon of host
*/
const collectIconAndAlias = async (tab: ChromeTab) => {
if (IS_SAFARI || IS_ANDROID) return
// Tab from sender does not contain favIconUrl for FF
if (IS_FIREFOX) tab = (tab.id ? await getTab(tab.id) : undefined) ?? tab
processTabInfo(tab)
}
const handleInjected = async (sender: ChromeMessageSender) => {
const { tab, url } = sender
if (!tab) return
await incVisitCount(tab)
await collectIconAndAlias(tab)
const tabId = tab.id
await badgeManager.updateFocus(tabId && url ? { tabId, url } : undefined)
}
/**
* Handle request from content script
*
* @param dispatcher message dispatcher
*/
export default function init(dispatcher: MessageDispatcher) {
dispatcher
.register('cs.injected', (_, sender) => handleInjected(sender))
// Get sites which need to count run time
.register('cs.getAudible', async (_, sender) => !!sender.tab?.audible)
}