|
5 | 5 | * https://opensource.org/licenses/MIT |
6 | 6 | */ |
7 | 7 |
|
8 | | -import optionService from "@service/option-service" |
9 | | -import timerService from "@service/timer-service" |
10 | | -import whitelistService from "@service/whitelist-service" |
11 | 8 | import { initLocale } from "@util/i18n" |
12 | 9 | import processLimit from "./limit" |
13 | 10 | import printInfo from "./printer" |
14 | 11 |
|
15 | 12 | const host = document.location.host |
16 | 13 | const url = document.location.href |
17 | 14 |
|
| 15 | +function isInWhitelist(host: string): Promise<boolean> { |
| 16 | + const request: timer.mq.Request<string> = { |
| 17 | + code: 'cs.isInWhitelist', |
| 18 | + data: host |
| 19 | + } |
| 20 | + return new Promise(resolve => chrome.runtime.sendMessage(request, {}, |
| 21 | + (res: timer.mq.Response<boolean>) => resolve(res.code === 'success' && !!res.data) |
| 22 | + )) |
| 23 | +} |
| 24 | + |
| 25 | +function addOneTime(host: string): void { |
| 26 | + const request: timer.mq.Request<string> = { |
| 27 | + code: 'cs.incVisitCount', |
| 28 | + data: host |
| 29 | + } |
| 30 | + chrome.runtime.sendMessage(request, () => { }) |
| 31 | +} |
| 32 | + |
| 33 | +function printTodayInfo(): Promise<boolean> { |
| 34 | + const request: timer.mq.Request<void> = { |
| 35 | + code: 'cs.printTodayInfo', |
| 36 | + data: undefined |
| 37 | + } |
| 38 | + return new Promise(resolve => chrome.runtime.sendMessage(request, |
| 39 | + (res: timer.mq.Response<boolean>) => resolve(res.code === 'success' && !!res.data) |
| 40 | + )) |
| 41 | +} |
| 42 | + |
18 | 43 | async function main() { |
19 | 44 | if (!host) return |
20 | 45 |
|
21 | | - const isWhitelist = await whitelistService.include(host) |
| 46 | + const isWhitelist = await isInWhitelist(host) |
22 | 47 | if (isWhitelist) return |
23 | 48 |
|
24 | | - timerService.addOneTime(host) |
| 49 | + addOneTime(host) |
25 | 50 |
|
26 | 51 | await initLocale() |
27 | | - const option = await optionService.getAllOption() |
28 | | - if (!!option.printInConsole) { |
29 | | - printInfo(host) |
30 | | - } |
| 52 | + const needPrintInfo = await printTodayInfo() |
| 53 | + !!needPrintInfo && printInfo(host) |
31 | 54 | processLimit(url) |
32 | 55 | } |
33 | 56 |
|
|
0 commit comments