0

Description

When using the keyboard shortcuts to navigate post lists (U - top post; J - next post; K - previous post) this userscript will also set the focus on the hyperlink to the post.

This allows using Enter / Ctrl+Enter / Shift+Enter to open the question without reaching for the mouse or excessively pressing Tab to get to that element. As well as other direct interactions with the link element.

Only selects post titles in lists that link to a Q&A, such as /questions or /users/{user id}/{user name}?tab=answers The userscript has no effect when navigating posts in a Q&A.

Installation

Direct install (GitHub)

See the code on GitHub

// ==UserScript==
// @name            Focus question title with keyboard selection
// @description     Focuses the title of a keyboard selected post. Allows for Ctrl+Click to open in new tab
// @author          VLAZ
// @grant           none
// @inject-into     page
// @match           https://stackoverflow.com/*
// @match           https://serverfault.com/*
// @match           https://superuser.com/*
// @match           https://*.stackexchange.com/*
// @match           https://askubuntu.com/*
// @match           https://stackapps.com/*
// @match           https://mathoverflow.net/*
// @match           https://pt.stackoverflow.com/*
// @match           https://ja.stackoverflow.com/*
// @match           https://ru.stackoverflow.com/*
// @match           https://es.stackoverflow.com/*
// @match           https://meta.stackoverflow.com/*
// @match           https://meta.serverfault.com/*
// @match           https://meta.superuser.com/*
// @match           https://meta.askubuntu.com/*
// @match           https://meta.mathoverflow.net/*
// @match           https://pt.meta.stackoverflow.com/*
// @match           https://ja.meta.stackoverflow.com/*
// @match           https://ru.meta.stackoverflow.com/*
// @match           https://es.meta.stackoverflow.com/*
// @match           https://stackoverflowteams.com/c/*
// @namespace       https://github.com/PurpleMagick/
// @run-at          document-end
// @version         1.0.0
// ==/UserScript==
(() => {
    "use strict";
    const findKeyboardSelectChange = mutation => 
        mutation.type === "attributes"
            && mutation.attributeName === "class"
            && mutation.target.classList.contains("keyboard-selected");

    const callback = mutationList => {
        const keyboardSelectedContainer = Array.prototype.find
            .call(mutationList, findKeyboardSelectChange)
            ?.target;

        if (!keyboardSelectedContainer)
            return;

        keyboardSelectedContainer.querySelector("a.s-link, a.answer-hyperlink")?.focus();
    };

    const observer = new MutationObserver(callback);

    observer.observe(document.body, { 
        attributes: true,
        attributeFilter: ["class"],
        subtree: true
    });
})();

0

You must log in to answer this question.

Browse other questions tagged .