forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit-processor.ts
More file actions
68 lines (58 loc) · 2.1 KB
/
Copy pathlimit-processor.ts
File metadata and controls
68 lines (58 loc) · 2.1 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
/**
* Copyright (c) 2022-present Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { listTabs, sendMsg2Tab } from "@api/chrome/tab"
import { getSite } from '@service/site-service'
import { matches } from "@util/limit"
import { extractHostname } from '@util/pattern'
import { getStartOfDay, MILL_PER_DAY } from "@util/time"
import alarmManager from "./alarm-manager"
import MessageDispatcher from "./message-dispatcher"
import {
createLimitRule, delayLimit, noticeLimitChanged, removeLimitRules, selectLimit, updateLimitRules,
} from "./service/limit-service"
function initDailyBroadcast() {
// Broadcast rules at the start of each day
alarmManager.setWhen(
'limit-daily-broadcast',
() => getStartOfDay(new Date()) + MILL_PER_DAY,
noticeLimitChanged,
)
}
const processAskHitVisit = async (item: tt4b.limit.Item) => {
let tabs = await listTabs()
const { cond } = item
for (const { id, url } of tabs) {
try {
if (!url || !matches(cond, url) || !id) continue
const visitHit = await sendMsg2Tab(id, "askVisitHit", item.id)
if (visitHit) return true
} catch {
// Ignored
}
}
return false
}
async function querySummary(): Promise<tt4b.limit.Summary | undefined> {
const tabs = await listTabs({ currentWindow: true, active: true })
const url = tabs[0]?.url
if (!url) return undefined
const { host } = extractHostname(url)
const site = await getSite({ host, type: 'normal' })
const items = await selectLimit({ url, effective: true })
return { url, site, items }
}
export default function init(dispatcher: MessageDispatcher) {
initDailyBroadcast()
dispatcher
.register('limit.list', selectLimit)
.register('limit.delete', removeLimitRules)
.register('limit.update', updateLimitRules)
.register('limit.add', createLimitRule)
.register('limit.hitVisit', processAskHitVisit)
.register('limit.delay', delayLimit)
.register('limit.summary', querySummary)
}