-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteam-comment-cleaner.js
317 lines (265 loc) · 8.96 KB
/
steam-comment-cleaner.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
# Name: Steam Comment Cleaner (2020-11-08)
# Author: T!Mi (https://github.com/timi-owo)
*/
'use strict';
{
const STR =
{
CN:
{
BUTTON_OK: '好的',
BUTTON_YES: '确定',
BUTTON_NO: '取消',
TITLE_DONE: '运行完毕',
TITLE_ERROR: '发生错误',
TITLE_CONFIRM: '操作确认',
ERROR_INIT_FAILURE: '⛔ 无法读取 \"{?}\"',
ERROR_WRONG_PAGE: '❌ 请在 STEAM 资料页使用此代码。',
ERROR_NOT_LOGIN: '❌ 请先在网页上登录你的 STEAM 账号。',
ERROR_NOT_OWNER: '❌ 请在自己的 STEAM 资料页使用此代码。',
CONFIRM_MESSAGE: '⚡ 你确定要删除资料页上的全部 {?} 条留言吗?<br><br>⚠️ 警告:此操作不可逆,留言删除后无法恢复!',
PROGRESS_COUNTING: '♻️ 正在统计留言列表,请稍后 ···',
PROGRESS_DELETING: '♻️ 正在删除 {?}/{?} 条留言,请稍后 ···<br><br>💤 可能需等待较长时间,请不要刷新页面!',
DONE_COMMENT_EMPTY: '✔️ 你的资料页很干净,无需采取操作。',
DONE_FULLY_DELETED: '✔️ 已成功删除 {?} 条留言。',
DONE_SOMEONE_MISSED: '✔️ 已成功删除 {?}/{?} 条留言。<br><br>⚠️ 部分留言在删除时发生了错误,可以稍后再试。',
ERROR_MESSAGE: '请前往 https://github.com/timi-owo/steam-comment-cleaner<br><br>根据页面下方的说明提交反馈,以便尽快得到修复!'
},
EN:
{
BUTTON_OK: 'OK',
BUTTON_YES: 'YES',
BUTTON_NO: 'NO',
TITLE_DONE: 'DONE',
TITLE_ERROR: 'ERROR',
TITLE_CONFIRM: 'CONFIRM',
ERROR_INIT_FAILURE: '⛔ Can not read \"{?}\"',
ERROR_WRONG_PAGE: '❌ Using this code on your profile page.',
ERROR_NOT_LOGIN: '❌ You should login into steam before we can start.',
ERROR_NOT_OWNER: '❌ You are not owner of this profile.',
CONFIRM_MESSAGE: '⚡ Are you sure want to delete all of {?} comments on your profile?<br><br>⚠️ This operation is irreversible, the comments can not be restored after deletion!',
PROGRESS_COUNTING: '♻️ Counting comments on your profile, please wait ···',
PROGRESS_DELETING: '♻️ Deleting {?}/{?} of comments, please wait ···<br><br>💤 It may takes a long time, please do not refresh the page!',
DONE_COMMENT_EMPTY: '✔️ Your profile is pretty clean, no further action needed.',
DONE_FULLY_DELETED: '✔️ {?} comments has been successfully deleted.',
DONE_SOMEONE_MISSED: '✔️ {?}/{?} comments has been successfully deleted.<br><br>⚠️ There was an error when deleting some comments, you can try again later.',
ERROR_MESSAGE: 'Please visit https://github.com/timi-owo/steam-comment-cleaner<br><br>Submit according to feedback notes in order to be fixed as soon as possible!'
}
};
function $(str, ...args)
{
let buffer = (navigator.language.includes('zh') ? STR['CN'][str] : STR['EN'][str]);
for (let arg of args) { buffer = buffer.replace('{?}', arg); }
return buffer;
}
//----------------------------------------------------------------------------------------------------
function dismissAllDialog()
{
while (CModal.GetActiveModal() != null)
{ CModal.DismissActiveModal(); }
}
function showAlert(title, content)
{
dismissAllDialog();
return ShowAlertDialog(title, content, $('BUTTON_OK'));
}
function showConfirm(title, content)
{
dismissAllDialog();
return ShowConfirmDialog(title, content, $('BUTTON_YES'), $('BUTTON_NO'));
}
function showProgress(content)
{
dismissAllDialog();
return ShowDialog(null, content, { bExplicitDismissalOnly: true });
}
function updateProgress(dialog, content)
{
dialog.GetContent().find('.newmodal_content').html(content);
}
//----------------------------------------------------------------------------------------------------
function checkPage()
{
let steamsite = document.location.host.includes('steampowered.com');
let community = document.location.host.includes('steamcommunity.com');
if (!steamsite && !community) { return false; }
if (document.body.querySelector('.profile_page') == null)
{
showAlert($('TITLE_ERROR'), $('ERROR_WRONG_PAGE'));
return false;
}
else if (typeof g_steamID == 'boolean' && !g_steamID)
{
showAlert($('TITLE_ERROR'), $('ERROR_NOT_LOGIN'));
return false;
}
else if (typeof g_steamID != 'string')
{
showAlert($('TITLE_ERROR'), $('ERROR_INIT_FAILURE', 'SteamID')).done(() => {
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
});
return false;
}
else if (typeof g_sessionID != 'string')
{
showAlert($('TITLE_ERROR'), $('ERROR_INIT_FAILURE', 'SessionID')).done(() => {
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
});
return false;
}
else if (typeof g_rgProfileData != 'object')
{
showAlert($('TITLE_ERROR'), $('ERROR_INIT_FAILURE', 'ProfileData')).done(() => {
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
});
return false;
}
else if (typeof g_rgCommentThreads != 'object')
{
showAlert($('TITLE_ERROR'), $('ERROR_INIT_FAILURE', 'CommentThreads')).done(() => {
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
});
return false;
}
else if (g_rgProfileData.steamid != g_steamID)
{
showAlert($('TITLE_ERROR'), $('ERROR_NOT_OWNER'));
return false;
}
else { return true }
}
function loadComment(steamid)
{
return new Promise((success, failure) =>
{
let xhr = new XMLHttpRequest();
xhr.onload = () =>
{
if (xhr.readyState != 4) { return; }
if (xhr.status != 200)
{
failure(`HTTP ${xhr.statusText} (${xhr.status})`);
return;
}
else if (xhr.response == null)
{
failure('INVALID_RESPONSE');
return;
}
else if (!xhr.response.success)
{
failure('STEAM_NOT_SUCCESS');
return;
}
let comment = [];
let matched = null;
let pattern = new RegExp('(?<=comment_)[0-9]+', 'g');
while ((matched = pattern.exec(xhr.response.comments_html)) != null)
{ comment.push(matched[0]); }
success(comment);
};
xhr.ontimeout = () => { failure('REQUEST_TIMEOUT'); };
xhr.onerror = () => { failure('NETWORK_ERROR'); };
xhr.open('GET', `https://steamcommunity.com/comment/Profile/render/${steamid}/-1/?start=0&count=0`, true);
xhr.responseType = 'json';
xhr.timeout = 15000;
xhr.send();
});
}
function deleteComment(steamid, sessionid, comment)
{
return new Promise((success, failure) =>
{
let xhr = new XMLHttpRequest();
xhr.onload = () =>
{
if (xhr.readyState != 4) { return; }
if (xhr.status != 200)
{
failure(`HTTP ${xhr.statusText} (${xhr.status})`);
return;
}
else if (xhr.response == null)
{
failure('INVALID_RESPONSE');
return;
}
xhr.response.success ? success() : failure('STEAM_NOT_SUCCESS');
};
xhr.ontimeout = () => { failure('REQUEST_TIMEOUT'); };
xhr.onerror = () => { failure('NETWORK_ERROR'); };
xhr.open('POST', `https://steamcommunity.com/comment/Profile/delete/${steamid}/-1/`, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
let param = `gidcomment=${comment}&sessionid=${sessionid}`;
xhr.responseType = 'json';
xhr.timeout = 15000;
xhr.send(param);
});
}
function reloadComment()
{
let CommentThread = g_rgCommentThreads['Profile_' + g_steamID];
if (CommentThread != undefined)
{
CommentThread.m_cMaxPages = 1;
CommentThread.m_iCurrentPage = 1;
CommentThread.GoToPage(0);
}
}
//----------------------------------------------------------------------------------------------------
if (checkPage())
{
showProgress($('PROGRESS_COUNTING'));
loadComment(g_steamID).then((comments) =>
{
let total = comments.length, deleted = 0;
if (total == 0)
{
showAlert($('TITLE_DONE'), $('DONE_COMMENT_EMPTY'));
return;
}
showConfirm($('TITLE_CONFIRM'), $('CONFIRM_MESSAGE', total)).done(() =>
{
let dialog = showProgress($('PROGRESS_DELETING', deleted, total));
function warp(steamid, sessionid, comment)
{
return new Promise((done, never) =>
{
deleteComment(steamid, sessionid, comment).then(() =>
{
deleted ++;
updateProgress(dialog, $('PROGRESS_DELETING', deleted, total));
done();
}, (reason) =>
{
console.error('deleteComment :: ' + reason);
done();
});
});
}
let tasks = [];
for (let comment of comments)
{ tasks.push(warp(g_steamID, g_sessionID, comment)); }
Promise.all(tasks).then(() =>
{
reloadComment();
if (deleted == total)
{ showAlert($('TITLE_DONE'), $('DONE_FULLY_DELETED', deleted)); }
else
{ showAlert($('TITLE_DONE'), $('DONE_SOMEONE_MISSED', deleted, total)); }
});
});
}, (reason) =>
{
console.error('loadComment :: ' + reason);
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
}).catch((error) =>
{
console.warn('!!! Unknown Exception !!!');
console.error(error.stack != undefined ? error.stack : error);
showAlert($('TITLE_ERROR'), $('ERROR_MESSAGE'));
});
}
}
//...