forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (55 loc) · 2.01 KB
/
index.ts
File metadata and controls
66 lines (55 loc) · 2.01 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
/**
* 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 TrackerClient from "@src/background/timer/client"
import processLimit from "./limit"
import printInfo from "./printer"
import { injectPolyfill } from "./polyfill/inject"
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
}
async function main() {
// Execute in every injections
const tracker = new TrackerClient({
onReport: data => sendMsg2Runtime('cs.trackTime', data),
onResume: reason => reason === 'idle' && sendMsg2Runtime('cs.idleChange', false),
onPause: reason => reason === 'idle' && sendMsg2Runtime('cs.idleChange', true),
})
tracker.init()
sendMsg2Runtime('cs.onInjected')
// Execute only one time for each dom
if (getOrSetFlag()) return
if (!host) return
const isWhitelist = await sendMsg2Runtime('cs.isInWhitelist', { host, url })
if (isWhitelist) return
sendMsg2Runtime('cs.incVisitCount', { host, url })
await initLocale()
const needPrintInfo = await sendMsg2Runtime('cs.printTodayInfo')
!!needPrintInfo && printInfo(host)
injectPolyfill()
processLimit(url)
}
main()