Discourse 关闭和存档主题用户脚本

(也许可以放在 Dev,但不确定,所以就放在这里了)

在管理论坛时,我通过关闭和归档旧的群组成员资格请求 PM 来清理收件箱,手动操作让我感到厌烦。

我在此呈上用户脚本,为您代劳!您只需右键单击,选择“Tampermonkey”选项,然后运行“关闭和归档”脚本!

代码:

// ==UserScript==
// @name         关闭和归档
// @namespace    http://tampermonkey.net/
// @version      2025-02-14
// @description  运行脚本时关闭和归档当前打开的主题/私人消息
// @author       Firepup Sixfifty
// @match        https://=hostname=/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=amcforum.wiki
// @grant        none
// @run-at       context-menu
// ==/UserScript==

(function() {
    'use strict';

    fetch(`https://${window.location.hostname}/t/${window.location.pathname.split('/')[3]}/status`, {
        "headers": {
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            "accept": "application/json",
            "x-csrf-token": document.querySelector("[name='csrf-token']").content
        },
        "body": "status=closed&enabled=true",
        "method": "PUT"
    }).then((r)=>{
        fetch(`https://${window.location.hostname}/t/${window.location.pathname.split('/')[3]}/status`, {
            "headers": {
                "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
                "accept": "application/json",
                "x-csrf-token": document.querySelector("[name='csrf-token']").content
            },
            "body": "status=archived&enabled=true",
            "method": "PUT"
        }).then((r)=>{
            fetch(`https://${window.location.hostname}/t/${window.location.pathname.split('/')[3]}/archive-message`, {
                "headers": {
                    "accept": "application/json",
                    "x-csrf-token": document.querySelector("[name='csrf-token']").content
                },
                "method": "PUT"
            });
        })
    })
})();
1 个赞

等等……那么我该怎么运行这个?我应该把它放在哪里?

这是为Tampermonkey浏览器扩展程序编写的脚本

1 个赞

在 PM 页面上,它是否比 a 快捷键做得更多?

是的——它也在关闭PM。

归档帖子无法回复:

我不知道,我只是在有人可能不小心看到它们时将它们关闭,这样更清楚它们不应该被回复。

您是管理员还是仅版主?如果您是管理员,您或许可以创建一个主题组件,而不是依赖用户脚本。

不过,对于无法编辑代码的网站,用户脚本仍然是实现各种改进的有趣解决方案。

我为许多网站使用它们。

meta.discourse.org 上,我有一个用户脚本,它可以在编辑器中添加一个 Ctrl + Shift + k 快捷键,用于将选定的文本用 <kbd> 标签包裹起来 :slight_smile:

// ==UserScript==
// @name         meta.discourse.org 的 <kbd> 快捷键
// @namespace    Violentmonkey Scripts
// @version      1.0
// @description  使用 Ctrl + Shift + k 在 meta.discourse.org 的文本区域中插入 <kbd> 标签
// @author       chatGPT
// @match        https://meta.discourse.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.shiftKey && event.key === 'K') {
            const activeElement = document.activeElement;
            if (activeElement.tagName === 'TEXTAREA' && activeElement.classList.contains('ember-text-area')) {
                const start = activeElement.selectionStart;
                const end = activeElement.selectionEnd;
                const text = activeElement.value;
                const insertText = '<kbd>';
                const closeText = '</kbd>';

                const selectedText = text.slice(start, end);
                activeElement.value = text.slice(0, start) + insertText + selectedText + closeText + text.slice(end);

                const cursorPosition = end === start
                    ? start + insertText.length
                    : start + insertText.length + selectedText.length + closeText.length;

                activeElement.selectionStart = activeElement.selectionEnd = cursorPosition;

                event.preventDefault();
            }
        }
    });
})();

另外,我不使用 Tampermonkey,因为它不是开源的,我更喜欢使用开源软件,而且有几个竞争者(它们看起来都一样,脚本也是跨兼容的)。我认为最常用的是 Violentmonkey

1 个赞

我只是把它放进了一个用户脚本,因为我太懒得为它做一个TC,而且这样编辑和测试更快(我最初只是在控制台中运行了几次来测试它)

哦哦哦,这实际上是个非常好的功能,谢谢分享!

刚刚稍微修改了一下脚本,这样它也能在主题上工作,尽管这个作用不大(这确实会导致一个奇怪的效果,即拥有一个存档的私人信息)。

1 个赞