Discourse close & archive topic userscript

AI-generated summary

The discussion is about a userscript called “Close and Archive” created by Firepup650 to close and archive old private messages (PMs) on a Discourse forum. The script is designed to work with the Tampermonkey browser extension. NateDhaliwal asked how to run the script, and Firepup650 explained that it’s a Tampermonkey script.

Canapin compared the script’s functionality to the a shortcut on PM pages and noted that archived posts can’t be replied to. Firepup650 replied that the script also closes the PM, and Canapin quoted a post from another topic stating that archived posts can’t be interacted with.

Firepup650 explained that they close PMs to make it clear they’re not supposed to be replied to. Canapin suggested that if Firepup650 is an admin, they could create a theme component instead of a userscript. Canapin also shared a userscript they use on meta.discourse.org to add a shortcut for wrapping text with <kbd> tags.

Firepup650 replied that they created the script because it was faster to edit and test, and Firepup650 later modified the script to work on topics as well.

(Maybe could fit in Development, but shoved it here since I wasn’t sure)

While cleaning up my inbox on forum I manage a bit by closing and archiving old group membership request PMs, I got annoyed with doing it manually.

I hereby present the userscript to do it for you! You just right click, go to the “Tampermonkey” option, then run the “Close and Archive” script!

Code:

// ==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 Like

Wait… so how do I run this? Where should I place it?

It’s a script written for the Tampermonkey browser extension

1 Like

Does it do more than the a shortcut when we’re on a PM page?

Yes - It also closes the PM.

Archived posts can’t be replied to:

Idk I just close them in case some staff member stumbles upon them by mistake it’s more clear that they’re not supposed to be replied to.

Are you an admin or only a moderator? If you are admin, you could probably create a theme component instead of relying on a userscript.

That said, for sites on which you can’t edit the code, userscripts are still an interesting solution for various improvements.

I use them for many websites.

On meta.discourse.org, I have one that adds a Ctrl+Shift+k shortcut to wrap my selected text with <kbd> tags inside the composer :slight_smile:

// ==UserScript==
// @name         <kbd> shortcut for meta.discourse.org
// @namespace    Violentmonkey Scripts
// @version      1.0
// @description  Insert <kbd> tags into textareas on meta.discourse.org with Ctrl + Shift + k
// @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();
            }
        }
    });
})();

Also, I don’t user Tampermonkey as it’s closed source, I prefer using open source software, and there are several contender (they all look the same and the scripts are cross-compatibles). I think the most used is Violentmonkey.

1 Like

I just threw it in a userscript because I was too lazy to make a TC for it, and this was faster to edit and test (I originally just ran it in the console a handful of times to test it)

Oooooooh that’s actually really nice to have, ty for sharing!

Just modified the script a bit so it also works on topics, for what that’s worth (This does cause the strange effect of having an archived PM though).

1 Like