forked from 404-novel-project/novel-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.ts
72 lines (63 loc) · 1.7 KB
/
stat.ts
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
import { _GM_getValue, _GM_setValue } from "./lib/GM";
import { log } from "./log";
const statKeyName = "novel-downloader-22932304826849026";
const domain = document.location.hostname;
interface StatData {
success: {
[domain: string]: number;
};
failed: {
[domain: string]: number;
};
}
async function getStatData() {
const _data = (await _GM_getValue(statKeyName)) as string;
let statData: StatData;
if (_data) {
statData = JSON.parse(_data);
} else {
statData = { success: {}, failed: {} };
}
return statData;
}
const saveData = async (statData: StatData) => {
const dataJSON = JSON.stringify(statData);
await _GM_setValue(statKeyName, dataJSON);
return statData;
};
const dataPlus = async (key: keyof StatData) => {
const statData = await getStatData();
const tmpData = statData[key];
if (tmpData[domain]) {
tmpData[domain] = tmpData[domain] + 1;
} else {
tmpData[domain] = 1;
}
return saveData(statData);
};
export const successPlus = () => {
return dataPlus("success");
};
export const failedPlus = () => {
return dataPlus("failed");
};
export const printStat = async () => {
const statData = await getStatData();
log.info("[stat]小说下载器脚本运行情况统计:");
log.info(statData);
for (const k in statData) {
if (Object.prototype.hasOwnProperty.call(statData, k)) {
log.info(`[stat]${k}:`);
const subData = statData[k as keyof StatData];
for (const j in subData) {
if (Object.prototype.hasOwnProperty.call(subData, j)) {
log.info(` ${j}: ${subData[j]}`);
}
}
}
}
};
export const resetStat = () => {
const statData = { success: {}, failed: {} };
return saveData(statData);
};