Lazy userscript

TemulentTeatotaler
2023-06-17T02:47:59Z

Just a hack job, but I'd seen some comment about it being inconvenient to ignore posts so I made a quick userscript (tested with Tampermonkey on Firefox) that adds hotkeys for bookmarking/ignoring/voting on a post. It can also navigate to the link or comments or prev/next pages (←/→). Only implemented for posts 1-9 at the moment.

Ex:

i+2 ignores and hides the 2nd post (or restores, if ignored)

? shows a summary of hotkeys

// ==UserScript==
// @name         Tildes
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Quickie convenience hotkeys for tildes.net
// @author       TT
// @match        *://tildes.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tildes.net
// @grant        none
// @require      https://unpkg.com/hotkeys-js/dist/hotkeys.min.js
// ==/UserScript==
(function () {
    "use strict";
    let Action;
    (function (Action) {
        Action[Action["Invalid"] = 0] = "Invalid";
        Action[Action["Bookmark"] = 1] = "Bookmark";
        Action[Action["Comments"] = 2] = "Comments";
        Action[Action["Ignore"] = 3] = "Ignore";
        Action[Action["Open"] = 4] = "Open";
        Action[Action["Vote"] = 5] = "Vote";
    })(Action || (Action = {}));
    const regex = /tildes\.net(\/~(?<group>\w+))?/gi;
    let match = regex.exec(document.location);
    //In a group if I wanted to support hotkeys there?
    if (match.groups.group) {
        //alert(match.groups.group);
    }
    else {
        addArticleNumbers();
        addMainHotkeys();
    }
    //Route
    function routeAction(action, event, handler) {
        event.preventDefault();
        //Grab index.  Zero-index?
        let indexText = handler.key.substring(2);
        let index = parseInt(indexText) - 1;
        if (isNaN(index))
            return;
        actOnArticle(action, index);
    }
    function actOnArticle(action, index) {
        //Get article for index
        let article = document.querySelector(".topic-listing").children[index].children[0];
        //Do the thing
        switch (action) {
            case Action.Bookmark:
                if (article.style.borderStyle === 'solid')
                    article.style.borderStyle = 'none';
                else
                    article.style.borderStyle = 'solid';
                article.querySelector('button[data-ic-put-to$="bookmark"]').click();
                break;
            case Action.Comments:
                article.querySelector(".topic-info-comments a").click();
                break;
            case Action.Ignore:
                //Hide vs blank?
                if (article.style.visibility === "hidden")
                    article.style.visibility = "visible";
                else
                    article.style.visibility = "hidden";
                // article.style.display = 'none';
                article.querySelector('button[data-ic-put-to$="ignore"]').click();
                break;
            case Action.Open:
                article.querySelector(".topic-title a").click();
                break;
            case Action.Vote:
                article.querySelector(".topic-voting").click();
                break;
        }
    }
    function addArticleNumbers() {
        let titles = Array.from(document.querySelectorAll(".topic-title a"));
        for (let i = 1; i <= titles.length; i++) {
            let title = titles[i - 1];
            title.text = i + " - " + title.text;
        }
    }
    function addMainHotkeys() {
        //Set up handlers
        const handleBookmark = (event, handler) => routeAction(Action.Bookmark, event, handler);
        const handleComments = (event, handler) => routeAction(Action.Comments, event, handler);
        const handleIgnore = (event, handler) => routeAction(Action.Ignore, event, handler);
        const handleOpen = (event, handler) => routeAction(Action.Open, event, handler);
        const handleVote = (event, handler) => routeAction(Action.Vote, event, handler);
        hotkeys("shift+/", (e, h) => alert(getHelpText()));
        //Page nav
        hotkeys("left", (e, h) => Array.from(document.querySelectorAll(".pagination a")).find((e) => e.textContent == "Prev").click());
        hotkeys("right", (e, h) => Array.from(document.querySelectorAll(".pagination a")).find((e) => e.textContent == "Next").click());
        for (let i = 1; i <= 9; i++) {
            hotkeys("b+" + i, handleBookmark);
            hotkeys("c+" + i, handleComments);
            hotkeys("i+" + i, handleIgnore);
            hotkeys("o+" + i, handleOpen);
            hotkeys("v+" + i, handleVote);
        }
    }
    function getHelpText() {
        return `
    ←/→ = navigation
    b = Bookmark, i = Ignore, v = Vote,
    c = Open comments, o = Open link,

    Action+[1-9] calls that action on the corresponding article`;
    }
})();

Comments URL: https://tildes.net/~tildes/16h8/lazy_userscript

Votes: 13

Comments: 9