Discourse close & archive topic userscript

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