-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-from-user.user.js
391 lines (339 loc) · 11.6 KB
/
delete-from-user.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
/*
Wikidot user-specific message deleter userscript
For installation instructions, see https://scpwiki.com/usertools
*/
/* CHANGELOG
v1.3.1 (2024-10-10)
- Created userscript.
*/
// ==UserScript==
// @name Wikidot user PM deleter
// @description Adds a button to delete PMs from a user from your Wikidot inbox.
// @author Croquembouche
// @version v1.3.1
// @updateURL https://github.com/croque-scp/delete-applications/raw/main/delete-from-user.user.js
// @downloadURL https://github.com/croque-scp/delete-applications/raw/main/delete-from-user.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("User PM 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)
}
/**
* 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
const from = messageElement.querySelector("td .from .printuser")
this.sender = from.innerText
}
select() {
this.selector.checked = true
}
deselect() {
this.selector.checked = false
}
get isSelected() {
return this.selector.checked
}
}
/* ===== */
async function deleteMessagesFromUser(username = "") {
if (!username) return
const messagesToDelete = []
const messageElement = document.getElementById("message-area")
let thereAreMorePages = true
const scanningModal = new OZONE.dialogs.WaitBox()
scanningModal.content = `Scanning your inbox for messages from ${username}...`
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 from the user
messages.forEach(message => {
if (message.sender !== username) message.deselect()
})
// Save all selected messages
const selectedMessages = messages.filter(message => message.isSelected)
deleterDebug(`Found ${selectedMessages.length} messages`)
messagesToDelete.push(selectedMessages)
thereAreMorePages = await nextPage(messageElement)
} while (thereAreMorePages)
// Reset UI back to the first page
await firstPage(messageElement)
// Delete all saved messages
createDeleteConfirmationModal(messagesToDelete.flat())
}
/**
* @param {Message[]} messages
*/
function createDeleteConfirmationModal(messages) {
const messagesCount = messages.length
// Produce a confirmation modal with the number of applications to delete
const confirmModal = new OZONE.dialogs.ConfirmationDialog()
confirmModal.content = `
<p>Delete ${messagesCount} messages?</p>
<p>This is <strong>not reversible.</strong></p>
<p><em>Please report any issues during the deletion process to ${supportUser(
true
)}.</em></p>
`
confirmModal.buttons = ["cancel", "delete messages"]
confirmModal.addButtonListener("cancel", confirmModal.close)
confirmModal.addButtonListener("delete messages", async () => {
const progressModal = new OZONE.dialogs.SuccessBox()
progressModal.content = `
<p>Deleting ${messagesCount} messages...</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} messages)
`
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} messages.<p>
`
successModal.show()
} else {
const errorModal = new OZONE.dialogs.ErrorDialog()
errorModal.content = `
<p>Failed to delete messages.</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-from-user-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"
deleteButtonsContainer.innerHTML = `
<p style="font-size: smaller">
<a href="https://scpwiki.com/usertools#delete-applications">Delete PMs from user</a> by ${supportUser()}
</p>
`
scriptControlContainer.appendChild(deleteButtonsContainer)
const deleteForm = document.createElement("form")
deleteForm.id = "delete-from-user-buttons"
deleteForm.style.display = "flex"
deleteForm.style.flexDirection = "row"
deleteForm.style.flexWrap = "wrap"
deleteForm.style.gap = "0.5rem"
deleteButtonsContainer.appendChild(deleteForm)
const usernameField = document.createElement("input")
usernameField.classList.add("form-control")
usernameField.style.width = "auto"
usernameField.style.flex = "1"
usernameField.placeholder = "Enter username"
deleteForm.appendChild(usernameField)
const deleteButton = document.createElement("input")
deleteButton.type = "submit"
deleteButton.value = "Delete"
deleteButton.classList.add("red", "btn", "btn-danger")
deleteButton.title = `Delete all messages from the specified user.`
deleteForm.appendChild(deleteButton)
deleteForm.addEventListener("submit", submitEvent => {
deleteMessagesFromUser(usernameField.value)
submitEvent.preventDefault()
submitEvent.stopPropagation()
return false
})
// Detect clicks to messages and inbox tabs and hide/show buttons as appropriate
addEventListener("click", () =>
setTimeout(() => {
deleteButtonsContainer.style.display = shouldShowDeleteButtons()
? "flex"
: "none"
}, 500)
)
})()