Discourseのトピックを閉じたりアーカイブしたりするユーザースクリプト

Dev に収まるかもしれませんが、確信がなかったのでここに置きました)

管理しているフォーラムの受信トレイを整理していて、古いグループメンバーシップリクエストのプライベートメッセージを閉じてアーカイブしていたのですが、手動で行うのが面倒になりました。

そこで、それを自動化するユーザースクリプトをここに公開します!右クリックして「Tampermonkey」オプションを選択し、「Close and Archive」スクリプトを実行するだけです!

コード:

// ==UserScript==
// @name         Close and Archive
// @namespace    http://tampermonkey.net/
// @version      2025-02-14
// @description  Closes and Archives the currently open topic/private message when run
// @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

aショートカットがPMページで使われる場合よりも、さらに多くの機能がありますか?

はい - それはまたPMも閉じます。

アーカイブされた投稿には返信できません。

わかりません。誤ってスタッフが見つけないように、単に閉じています。返信されないことがより明確になります。

あなたは管理者ですか、それともモデレーターだけですか?管理者であれば、ユーザースクリプトに依存するのではなく、テーマコンポーネントを作成できる可能性があります。

とはいえ、コードを編集できないサイトでは、ユーザースクリプトはさまざまな改善のための興味深いソリューションです。

私は多くのウェブサイトでそれらを使用しています。

meta.discourse.org では、コンポーザー内で選択したテキストを <kbd> タグで囲むための Ctrl + Shift + k ショートカットを追加するスクリプトを使用しています :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を作成するのが面倒だったからです。編集やテストがより迅速に行えました(元々はコンソールで数回実行してテストしました)。

おお、それは actually とても良いですね。共有してくれてありがとうございます!

ちょっとスクリプトを修正して、トピックでも動作するようにしました。それが何の役に立つかはわかりませんが(これにより、アーカイブされたプライベートメッセージを持つ奇妙な効果も引き起こします)。

「いいね!」 1