-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.user.js
More file actions
48 lines (48 loc) · 2.43 KB
/
script.user.js
File metadata and controls
48 lines (48 loc) · 2.43 KB
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
// ==UserScript==
// @name Add delete/restore post button
// @version 1.1
// @author ShlomoCode
// @match *://*/*
// @description Add delete/restore post button for own posts (for admin - for all posts) for nodebb forums
// ==/UserScript==
if (typeof $ === 'function' && typeof app === 'object' && app?.user?.uid) {
async function main () {
const [api, translator, alerts] = await app.require(['api', 'translator', 'alerts']);
const translations = {
delete: await translator.translate('[[topic:delete]]'),
restore: await translator.translate('[[topic:restore]]')
};
const posts = $(app.user.isGlobalMod || app.user.isAdmin ? '[component="post"]' : '.self-post').not('.delete-button-was-added');
for (const post of posts) {
const pid = parseInt($(post).attr('data-pid'), 10);
const pHeader = $(post).find('.post-header');
const isDeleted = $(post).hasClass('deleted');
const button = $('<button>')
.addClass(isDeleted ? 'fas fa-trash-restore-alt' : 'fa fa-fw fa-trash-o')
.attr('action', isDeleted ? 'restore' : 'delete')
.css({ background: 'none', border: 'none' })
.tooltip({
title: translations[isDeleted ? 'restore' : 'delete'],
placement: 'top',
trigger: 'hover'
})
.click(async () => {
const action = $(button).attr('action');
bootbox.confirm(`[[topic:post_${action}_confirm]]`, async (confirm) => {
if (!confirm) return;
try {
await api[action === 'delete' ? 'del' : 'put'](`/api/v3/posts/${pid}/state`);
$(button).attr('action', action === 'delete' ? 'restore' : 'delete');
$(button).toggleClass('fa fa-fw fa-trash-o fas fa-trash-restore-alt');
$(button).attr('data-original-title', translations[action === 'delete' ? 'restore' : 'delete']);
} catch (err) {
alerts.error(err.message);
}
});
});
pHeader.append(button);
$(post).addClass('delete-button-was-added');
}
}
$(window).on('action:posts.loaded action:topic.loaded', main);
}