-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathindex.ts
More file actions
79 lines (67 loc) · 2.46 KB
/
index.ts
File metadata and controls
79 lines (67 loc) · 2.46 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
79
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { sendMsg2Runtime } from "@api/chrome/runtime"
import { initLocale } from "@i18n"
import processLimit from "./limit"
import printInfo from "./printer"
import NormalTracker from "./tracker/normal"
import RunTimeTracker from "./tracker/run-time"
const host = document?.location?.host
const url = document?.location?.href
const FLAG_ID = '__TIMER_INJECTION_FLAG__' + chrome.runtime.id
function getOrSetFlag(): boolean {
const pre = document?.getElementById(FLAG_ID)
if (!pre) {
const flag = document?.createElement('a')
flag.style && (flag.style.visibility = 'hidden')
flag && (flag.id = FLAG_ID)
if (document.readyState === "complete") {
document?.body?.appendChild(flag)
} else {
const oldListener = document.onreadystatechange
document.onreadystatechange = function (ev) {
oldListener?.call(this, ev)
document.readyState === "complete" && document?.body?.appendChild(flag)
}
}
}
return !!pre
}
/**
* Wrap for hooks, after the extension reloaded or upgraded, the context of current content script will be invalid
* And sending messages to the runtime will be failed
*/
async function trySendMsg2Runtime<Req, Res>(code: timer.mq.ReqCode, data?: Req): Promise<Res | undefined> {
try {
return await sendMsg2Runtime(code, data)
} catch {
// ignored
}
}
async function main() {
// Execute in every injections
const normalTracker = new NormalTracker({
onReport: data => trySendMsg2Runtime('cs.trackTime', data),
onResume: reason => reason === 'idle' && trySendMsg2Runtime('cs.idleChange', false),
onPause: reason => reason === 'idle' && trySendMsg2Runtime('cs.idleChange', true),
})
normalTracker.init()
const runTimeTracker = new RunTimeTracker(url)
runTimeTracker.init()
// Execute only one time for each dom
if (getOrSetFlag()) return
if (!host) return
const isWhitelist = await sendMsg2Runtime('cs.isInWhitelist', { host, url })
if (isWhitelist) return
await initLocale()
const needPrintInfo = await sendMsg2Runtime('cs.printTodayInfo')
!!needPrintInfo && printInfo(host)
await processLimit(url)
// Increase visit count at the end
await sendMsg2Runtime('cs.incVisitCount', { host, url })
}
main()