Skip to content

Instantly share code, notes, and snippets.

@netwjx
Last active August 18, 2023 16:57
Show Gist options
  • Save netwjx/3ca22d9cafec5af1c12d1b0503114c4b to your computer and use it in GitHub Desktop.
Save netwjx/3ca22d9cafec5af1c12d1b0503114c4b to your computer and use it in GitHub Desktop.
discord batch delete
// 断点代码特征:W.rU)(t.id
window.ddd = W.rU // 断点后抽取函数
// 断点代码特征:me.WO)(O,
window.uuu = me.WO // 断点后抽取函数
// ==UserScript==
// @name 批量删除midjourney图片
// @namespace rm-mid
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://discord.com/channels/*
// @grant unsafeWindow
// ==/UserScript==
(function() {
'use strict';
let cont = document.createElement('div')
cont.style.cssText = `position: absolute; right: 256px; bottom: 73px;`
cont.innerHTML = `<button class="start-rm">开始删除</button> <button class="stop-rm" style="display: none">停止删除</button>`
let tid = 0;
let aniD = {
"id": null,
"name": "❌",
"animated": false
}
let aniU = {
"id": null,
"name": "❌"
}
let st = 0;
let start = cont.querySelector('.start-rm')
start.addEventListener('click', async () => {
tid = location.pathname.replace(/^(\/\w+){2}\//,'')
if (unsafeWindow.ddd == null) {
alert('请先断点并抽出删除消息函数')
return
}
start.style.display = 'none';
stop.style.display = '';
try{
await startRm()
}finally{
start.style.display = '';
stop.style.display = 'none';
}
})
let stop = cont.querySelector('.stop-rm')
stop.addEventListener('click', () => {
st = 1;
})
document.body.append(cont)
async function startRm(){
let ol = document.querySelector('ol[data-list-id=chat-messages]')
let prev = ol.lastElementChild.previousElementSibling
let e;
let n=1;
while(true){
e = prev;
if (e == null) {
alert('已删除完')
}
prev = e.previousElementSibling
let eid = e.id.replace(/^(\w+-){3}/, '')
if (!eid.match(/^\d+$/)) {
continue
}
if (!(e.querySelector('span.mention')?.innerHTML == '@Flynn0521的服务器')) {
continue
}
let retry = 1
while (true) {
if (st) break;
if (e.querySelector('img[alt=❌]') != null) {
unsafeWindow.uuu && unsafeWindow.uuu(tid, eid, aniU, void 0, 'Message')
await sleep(1000)
}
unsafeWindow.ddd(tid, eid, aniD)
await sleep(4000)
if (e.isConnected) {
if (retry < 3) {
console.log(`删除第${n}个未成功,重新尝试...`)
retry++;
} else {
console.log(`删除第${n++}个3次未成功,跳过`)
break;
}
} else {
console.log(`已删除${n++}`)
break;
}
}
if (st) {
console.log('人工中止')
window.st = 0
break;
}
}
}
function sleep(s) {
return new Promise(r => setTimeout(r, s))
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment