-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-applications.user.js
448 lines (388 loc) · 14.2 KB
/
delete-applications.user.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
Wikidot applications deleter userscript
For installation instructions, see https://scpwiki.com/usertools
*/
/* CHANGELOG
v1.3.1 (2024-10-10)
- Userscript controls are now stored in a generic container available to other userscripts.
v1.3.0 (2023-09-08)
- Added changelog.
- Removed extra commas from the confirmation popup when deleting applications from more than one site.
- Deletes now execute in batches of 100 separated by a short delay to bypass Wikidot's single-request limit of 996.
- Made buttons larger and added more support links.
v1.2.0 (2023-07-07)
- Added a list of sites to the deletion confirmation popup that tells you which Wikidot sites the applications come from, and how many there are per site.
v1.1.0 (2022-04-11)
- Added new feature 'delete recent applications' that deletes applications page-by-page until encountering a page with no applications.
- Removed feature 'delete applications on current page'.
- After scanning pages of messages, script now puts you back on the first page instead of leaving you wherever it stopped.
- The delete buttons are now visible on all pages of the inbox instead of just the first.
v1.0.1 (2022-03-06)
- Hid buttons when reading a message.
- Fixed deletion confirmation popup interfering with message composer UI.
v1.0.0 (2022-03-01)
- Created userscript.
*/
// ==UserScript==
// @name Wikidot applications deleter
// @description Adds a button to delete applications from your Wikidot inbox.
// @author Croquembouche
// @version v1.3.1
// @updateURL https://github.com/croque-scp/delete-applications/raw/main/delete-applications.user.js
// @downloadURL https://github.com/croque-scp/delete-applications/raw/main/delete-applications.user.js
// @supportURL https://www.wikidot.com/account/messages#/new/2893766
// @match https://www.wikidot.com/account/messages*
// ==/UserScript==
/* global WIKIDOT, OZONE */
/* ===== Utilities ===== */
const deleterDebug = log => console.debug("Applications deleter:", log)
const supportUser = showAvatar => `
${
showAvatar
? `<span class="printuser avatarhover" style="white-space: nowrap">`
: ""
}
<a href="https://www.wikidot.com/user:info/croquembouche" onclick="WIKIDOT.page.listeners.userInfo(2893766); return false;" >
${
showAvatar
? `<img
class="small"
src="https://www.wikidot.com/avatar.php?userid=2893766" style="background-image:url(https://www.wikidot.com/userkarma.php?u=2893766)"
>`
: ""
}Croquembouche
</a>
${showAvatar ? `</span>` : ""}
`
function getMessagesOnPage() {
return Array.from(document.querySelectorAll("tr.message")).map(
el => new Message(el)
)
}
function countSelected(messages) {
return messages.reduce((a, b) => a + b.isSelected, 0)
}
class Counter {
constructor(array) {
array.forEach(val => (this[val] = (this[val] || 0) + 1))
}
}
/**
* Waits for the given number of milliseconds.
* @param {Number} ms
*/
async function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
class Message {
/**
* Collates details about a message based on its little preview.
* @param {HTMLElement} messageElement - Inbox container.
*/
constructor(messageElement) {
/** @type {HTMLInputElement} */
this.selector = messageElement.querySelector("input[type=checkbox]")
/** @type {String} */
this.id = this.selector.value
// Extract the sender and the subject
const from = messageElement.querySelector("td .from .printuser")
this.fromWikidot =
!from.classList.contains("avatarhover") && from.innerText === "Wikidot"
this.subject = messageElement.querySelector(".subject").innerText
this.previewText = messageElement.querySelector(".preview").innerText
// Is this message an application?
this.isApplication =
this.fromWikidot &&
this.subject === "You received a membership application"
if (this.isApplication) {
// Which wiki is the application for?
const wikiMatch = this.previewText.match(
/applied for membership on (.*), one of your sites/
)
if (wikiMatch) this.applicationWiki = wikiMatch[1]
else this.isApplication = false
}
}
select() {
this.selector.checked = true
}
deselect() {
this.selector.checked = false
}
get isSelected() {
return this.selector.checked
}
}
/* ===== */
async function deleteApplications(deleteAll = false) {
const applications = []
const messageElement = document.getElementById("message-area")
let goToNextPage = true
let thereAreMorePages = true
const scanningModal = new OZONE.dialogs.WaitBox()
scanningModal.content = "Scanning your inbox for applications..."
scanningModal.show()
await firstPage(messageElement)
do {
const messages = getMessagesOnPage()
// If no messages are selected, select all messages
if (countSelected(messages) === 0) {
messages.forEach(message => message.select())
}
// Deselect all messages that are not applications
messages.forEach(message => {
if (!message.isApplication) message.deselect()
})
// Save all selected messages
const selectedMessages = messages.filter(message => message.isSelected)
deleterDebug(`Found ${selectedMessages.length} applications`)
applications.push(selectedMessages)
// If there were no selected messages, and we are only deleting recent messages (i.e. deleteAll is false), don't go to the next page
if (selectedMessages.length === 0 && !deleteAll) goToNextPage = false
if (goToNextPage) thereAreMorePages = await nextPage(messageElement)
} while (goToNextPage && thereAreMorePages)
// Reset UI back to the first page
await firstPage(messageElement)
// Delete all saved messages
createDeleteConfirmationModal(applications.flat())
}
/**
* @param {Message[]} messages
*/
function createDeleteConfirmationModal(messages) {
const messagesCount = messages.length
// Collate the wikis that the applications were for
const wikiCounter = new Counter(messages.map(m => m.applicationWiki))
// Produce a confirmation modal with the number of applications to delete
const confirmModal = new OZONE.dialogs.ConfirmationDialog()
const applicationSitesList = Object.entries(wikiCounter).map(
([wiki, count]) => `<li>${wiki}: ${count}</li>`
)
confirmModal.content = `
<p>Delete ${messagesCount} applications?</p>
<p><em>Please report any issues during the deletion process to ${supportUser(
true
)}.</em></p>
<ul>${applicationSitesList.join("")}</ul>
`
confirmModal.buttons = ["cancel", "delete applications"]
confirmModal.addButtonListener("cancel", confirmModal.close)
confirmModal.addButtonListener("delete applications", async () => {
const progressModal = new OZONE.dialogs.SuccessBox()
progressModal.content = `
<p>Deleting ${messagesCount} applications...</p>
<p id="delete-progress-text"></p>
<progress id="delete-progress" style="width: 100%"></progress>
`
progressModal.timeout = null
progressModal.show()
const success = await deleteMessagesBatches(
messages,
async (batchIndex, batchCount, batchSize) => {
if (batchCount === 1) return
document.getElementById("delete-progress-text").textContent = `
Batch ${batchIndex + 1} of ${batchCount} (${batchSize} applications)
`
document.getElementById("delete-progress").max = batchCount
document.getElementById("delete-progress").value = batchIndex + 1
await wait(1500)
}
)
WIKIDOT.modules.DashboardMessagesModule.app.refresh()
if (success) {
const successModal = new OZONE.dialogs.SuccessBox()
successModal.content = `
<p>Deleted ${messagesCount} applications.<p>
`
successModal.show()
} else {
const errorModal = new OZONE.dialogs.ErrorDialog()
errorModal.content = `
<p>Failed to delete applications.</p>
<p>Please send a message to ${supportUser(true)}.</p>
`
errorModal.show()
}
})
confirmModal.focusButton = "cancel"
confirmModal.show()
}
/**
* @callback deleteMessagesBatches_beforeBatch
* @param {Number} batchIndex
* @param {Number} batchCount
* @param {Number} batchSize
* @return {Promise<void>}
*/
/**
* Deletes the given messages in batches.
* @param {Message[]} messages
* @param {deleteMessagesBatches_beforeBatch} beforeBatch - Callback that receives deletion progress info.
* @return {Promise<Boolean>} True when all deletes succeeded.
*/
async function deleteMessagesBatches(messages, beforeBatch) {
const batchSize = 100
const batchCount = Math.ceil(messages.length / batchSize)
let batchIndex = 0
while (messages.length) {
const batch = messages.splice(0, batchSize)
await beforeBatch(batchIndex, batchCount, batch.length)
try {
await deleteMessages(batch.map(message => message.id))
} catch (error) {
deleterDebug("Deletes failed")
console.error(error)
return false
}
batchIndex += 1
}
return true
}
/**
* Delete the messages with the given IDs.
* @param {Number[]} messageIds
*/
function deleteMessages(messageIds) {
return new Promise((resolve, reject) => {
try {
OZONE.ajax.requestModule(
null,
{
action: "DashboardMessageAction",
event: "removeMessages",
messages: messageIds,
},
resolve
)
} catch (error) {
reject(error)
}
})
}
/**
* Whether to show the deletion buttons, based on the current URL.
* @returns {Boolean}
*/
function shouldShowDeleteButtons() {
return /^(#(\/inbox(\/(p[0-9]+\/?)?)?)?)?$/.test(location.hash)
}
/**
* Go to the first page of messages.
* @param {HTMLElement} messageElement - Inbox container
* @returns {Promise<Boolean>}
*/
async function firstPage(messageElement) {
deleterDebug("Going to first page")
const pager = messageElement.querySelector(".pager")
if (pager == null) return false
const currentPageButton = pager.querySelector(".current")
if (currentPageButton == null) return false
if (currentPageButton.textContent.trim() === "1") return false
// The first page button should always be visible
const firstPageButton = pager.querySelector(".target [href='#/inbox/p1']")
if (firstPageButton == null) return false
// Click the button and return once the page has reloaded
await new Promise(resolve => {
const observer = new MutationObserver(() => {
observer.disconnect()
resolve()
})
observer.observe(messageElement, { childList: true })
firstPageButton.click()
})
return true
}
/**
* Iterate to the next page of messages.
*
* @param {HTMLElement} messageElement - Inbox container
* @returns {Promise<Boolean>} False if last page; otherwise wait for next page to load then true.
*/
async function nextPage(messageElement) {
deleterDebug("Going to next page")
const pager = messageElement.querySelector(".pager")
if (pager == null) return false
const nextButton = pager.querySelector(".target:last-child a")
if (nextButton == null) return false
if (nextButton.textContent.trim() !== "next »") return false
// Wait until the next page has finished loading
await new Promise(resolve => {
const observer = new MutationObserver(() => {
observer.disconnect()
resolve()
})
observer.observe(messageElement, { childList: true })
nextButton.click()
})
return true
}
;(function () {
// Set up container for userscript controls, unless another userscript already did
let scriptControlContainer = document.getElementById("messages-userscripts")
if (!scriptControlContainer) {
scriptControlContainer = document.createElement("div")
scriptControlContainer.id = "messages-userscripts"
scriptControlContainer.style.display = "flex"
scriptControlContainer.style.justifyContent = "end"
scriptControlContainer.style.flexWrap = "wrap"
scriptControlContainer.style.marginBlock = "1.5rem"
scriptControlContainer.style.gap = "1.5rem"
document
.getElementById("message-area")
.parentElement.prepend(scriptControlContainer)
}
const deleteButtonsContainer = document.createElement("div")
deleteButtonsContainer.id = "delete-applications-controls"
deleteButtonsContainer.style.border = "thin solid lightgrey"
deleteButtonsContainer.style.borderRadius = "0.5rem"
deleteButtonsContainer.style.display = shouldShowDeleteButtons()
? "flex"
: "none"
deleteButtonsContainer.style.flexDirection = "column"
deleteButtonsContainer.style.maxWidth = "max-content"
deleteButtonsContainer.style.padding = "1rem 1rem 0"
deleteButtonsContainer.innerHTML = `
<p style="font-size: smaller">
<a href="https://scpwiki.com/usertools#delete-applications">Delete applications</a> by ${supportUser()}
</p>
<p id="delete-applications-buttons" style="
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 0.5rem;
"></p>
`
scriptControlContainer.appendChild(deleteButtonsContainer)
// Create the buttons
const deleteRecentButton = document.createElement("button")
deleteRecentButton.innerText = "Delete recent applications"
deleteRecentButton.classList.add("red", "btn", "btn-danger")
deleteRecentButton.title = `
Delete recent applications.
Deletes applications on the first page, then the second, and so on, until a page with no applications is found.
`
.replace(/\s+/g, " ")
.trim()
deleteRecentButton.addEventListener("click", () => deleteApplications(false))
const deleteAllButton = document.createElement("button")
deleteAllButton.innerText = "Delete all applications"
deleteAllButton.classList.add("red", "btn", "btn-danger")
deleteAllButton.title = `
Delete all applications in your inbox.
May take a while if you have a lot.
`
.replace(/\s+/g, " ")
.trim()
deleteAllButton.addEventListener("click", () => deleteApplications(true))
deleteButtonsContainer
.querySelector("#delete-applications-buttons")
.append(deleteRecentButton, deleteAllButton)
// Detect clicks to messages and inbox tabs and hide/show buttons as appropriate
addEventListener("click", () =>
setTimeout(() => {
deleteButtonsContainer.style.display = shouldShowDeleteButtons()
? "flex"
: "none"
}, 500)
)
})()