-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
180 lines (152 loc) · 5.88 KB
/
main.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const kLogCount = 'logCleaner::collectedGarbageLogCount';
const kLogSize = 'logCleaner::collectedGarbageLogSize';
const kEnableIntercept = 'logCleaner::enableIntercept::fixed';
let collectedGarbageLogCount = parseInt(localStorage[kLogCount] || '0');
let collectedGarbageLogSize = parseInt(localStorage[kLogSize] || '0');
if (localStorage[kEnableIntercept] == undefined) localStorage[kEnableIntercept] = 'true';
let enableIntercept = localStorage[kEnableIntercept] === 'true';
const performHook = () => {
const oldChannelCall = channel.call;
channel.call = (name, ...args) => {
if (!enableIntercept) return oldChannelCall(name, ...args);
try {
if (name.includes('log')
|| (name === 'storage.savetofile' && args[0] && args[0].includes && args[0].includes('crash_report'))) {
for (const log of args)
if (typeof log === "string") collectedGarbageLogSize += log.length;
collectedGarbageLogCount++;
return;
}
} catch (e) {
}
try {
return oldChannelCall(name, ...args);
} catch (e) {
window.onerror(e.toString())
}
}
window.onerror = (e) => {
if (!enableIntercept) return false;
const msg = e.message || e;
collectedGarbageLogSize += msg.toString().length;
collectedGarbageLogCount++;
return true;
}
const originConsole = {};
for (const logFnName in console) {
originConsole[logFnName] = console[logFnName];
console[logFnName] = (...logs) => {
if (!enableIntercept) return originConsole[logFnName](...logs);
for (const log of logs) {
if (!log) continue;
if (typeof log === "string") collectedGarbageLogSize += log.length;
else if (JSON.stringify(log) && JSON.stringify(log)?.length) collectedGarbageLogSize += JSON.stringify(log).length;
collectedGarbageLogCount++;
}
}
}
};
plugin.onLoad(() => {
betterncm.app.exec("cmd /k cd /d %AppData%/../Local/NetEase/CloudMusic/ && for /L %i in () do @(del cloudmusic.elog 2>nul && del web.log 2>nul && if not exist cloudmusic.elog exit) & timeout /T 5", 0, 0)
if(loadedPlugins.BGEnhanced) enableIntercept = false;
performHook();
});
const updateStorage = () => {
localStorage[kLogCount] = collectedGarbageLogCount;
localStorage[kLogSize] = collectedGarbageLogCount;
localStorage[kEnableIntercept] = enableIntercept;
};
setInterval(updateStorage, 20 * 1000)
plugin.onConfig(() => {
const cleanedLogCountDom = dom('span', { class: ['cleanedLogCount'] });
const cleanedLogSizeDom = dom('span', { class: ['cleanedLogSize'] });
const updateUi = () => {
const formatBytes = (bytes) => {
if (bytes < 1024) {
return bytes + ' B';
} else if (bytes < 1024 * 1024) {
return (bytes / 1024).toFixed(2) + ' KB';
} else if (bytes < 1024 * 1024 * 1024) {
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
} else if (bytes < 1024 * 1024 * 1024 * 1024) {
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
} else {
return (bytes / (1024 * 1024 * 1024 * 1024)).toFixed(2) + ' TB';
}
}
cleanedLogSizeDom.innerText = formatBytes(collectedGarbageLogSize);
cleanedLogCountDom.innerText = collectedGarbageLogCount;
}
setInterval(updateUi, 1000);
updateUi();
return dom('div', {},
dom('div', { class: ['logCleanConfig'] },
dom('label', {},
dom('input', {
type: 'checkbox', checked: enableIntercept, onchange: (e) => {
enableIntercept = e.target.checked;
updateStorage();
}, style: {
display: 'none'
}
}),
dom('span', { innerText: '启用日志拦截', class: ['enableLogIntercept'] })
)
),
dom('div', { class: ['logCleaned'] },
dom('span', { innerText: '已为您拦截 ' }),
cleanedLogSizeDom,
dom('span', { innerText: ' ,共计 ' }),
cleanedLogCountDom,
dom('span', { innerText: ' 条日志' }),
),
dom('style', {
innerHTML: `
.logCleanConfig {
padding: 10px;
display: flex;
flex-direction: row;
}
.logCleanConfig input {
margin-right: 6px;
}
.logCleaned {
font-size: 15px;
}
span.cleanedLogSize {
font-size: 30px;
font-weight: 700;
}
span.cleanedLogCount {
font-size: 30px;
font-weight: 700;
}
.logCleaned > span:not([class]) {
opacity: 0.7;
}
input + .enableLogIntercept::before {
content: "×";
border: 1px solid #7b7b7ba1;
width: 11px;
height: 11px;
background: transparent;
display: inline-block;
box-sizing: content-box;
border-radius: 100%;
margin-right: 5px;
font-weight: 700;
text-align: center;
opacity: 0.6;
transition: .3s opacity, .2s color;
color: transparent;
}
label:hover .enableLogIntercept {
opacity: 0.8;
}
input:checked + .enableLogIntercept::before {
opacity: 1;
color: inherit;
}
` })
)
});