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
74 lines (67 loc) · 2.39 KB
/
content-script-handler.ts
File metadata and controls
74 lines (67 loc) · 2.39 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
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { IS_ANDROID, IS_CHROME, IS_SAFARI } from "@util/constant/environment"
import { extractHostname, isBrowserUrl, isHomepage } from "@util/pattern"
import { extractSiteName } from "@util/site"
import badgeManager from "./badge-manager"
import MessageDispatcher from "./message-dispatcher"
import { saveAlias, saveIconUrl } from "./service/site-service"
import { incVisitCount } from './track-server/normal'
function isUrl(title: string) {
return title.startsWith('https://') || title.startsWith('http://') || title.startsWith('ftp://')
}
async function collectAlias(key: timer.site.SiteKey, tabTitle: string) {
if (!tabTitle) return
if (isUrl(tabTitle)) return
const siteName = extractSiteName(tabTitle, key.host)
siteName && await saveAlias(key, siteName, true)
}
/**
* Process the tab
*/
async function processTabInfo(tab: ChromeTab): Promise<void> {
let { favIconUrl, url, title } = tab
if (!url || !title) return
if (isBrowserUrl(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) && (favIconUrl = undefined)
const siteKey: timer.site.SiteKey = { host, type: 'normal' }
favIconUrl && await saveIconUrl(siteKey, favIconUrl)
!IS_ANDROID
&& !isBrowserUrl(url)
&& isHomepage(url)
&& await collectAlias(siteKey, title)
}
/**
* Collect the favicon of host
*/
const collectIconAndAlias = async (tab: ChromeTab) => {
if (IS_SAFARI || IS_ANDROID) return
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)
}