# Discourse close & archive topic userscript

**URL:** https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211
**Category:** General
**Created:** [February 14, 2025, 4:23am UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211 "2025-02-14T04:23:57Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 4:23am UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/1 "2025-02-14T04:23:57Z")

</div>

(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:

```javascript
// ==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"
            });
        })
    })
})();

```

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [February 14, 2025, 5:06am UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/2 "2025-02-14T05:06:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 5:20am UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/3 "2025-02-14T05:20:30Z")

</div>

It’s a script written for the Tampermonkey browser extension

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [February 14, 2025, 1:27pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/4 "2025-02-14T13:27:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 4:02pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/5 "2025-02-14T16:02:52Z")

</div>

Yes - It also closes the PM.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [February 14, 2025, 4:45pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/6 "2025-02-14T16:45:28Z")

</div>

Archived posts can’t be replied to:

> [@Difference between Archived and Closed](https://meta.discourse.org/t/difference-between-archived-and-closed/32789/2):
>
> In Archived, you can’t like, edit, or otherwise interact with the posts.

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 5:55pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/7 "2025-02-14T17:55:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [February 14, 2025, 6:07pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/8 "2025-02-14T18:07:45Z")

</div>

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](http://meta.discourse.org), I have one that adds a Ctrl+Shift+k shortcut to wrap my selected text with `<kbd>` tags inside the composer 🙂

```js
// ==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](https://violentmonkey.github.io/).

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 6:18pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/9 "2025-02-14T18:18:10Z")

</div>

> [@Canapin](#):
>
> 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.

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)

> [@Canapin](#):
>
> On [meta.discourse.org](http://meta.discourse.org), I have one that adds a Ctrl+Shift+k shortcut to wrap my selected text with `<kbd>` tags inside the composer

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

---

<div class="post-metadata">

### Author: ![Firepup650](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/firepup650/32/465200_2.png) [@Firepup650](https://meta.discourse.org/u/Firepup650)
#### Post date: [February 14, 2025, 6:19pm UTC](https://meta.discourse.org/t/discourse-close-archive-topic-userscript/352211/10 "2025-02-14T18:19:28Z")

</div>

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).
