Greasy Fork is available in English.

jisho-shortcuts

Shortcuts for Jisho.

Ajankohdalta 4.4.2020. Katso uusin versio.

// ==UserScript==
// @name         jisho-shortcuts
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Shortcuts for Jisho.
// @author       Long Huynh Huu
// @match        https://jisho.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var shift = 0;
    var tags = [];
    var details = document.getElementsByClassName("light-details_link");
    let keyword = document.getElementById("keyword");

    let install = function() {
        for (var i = 0; i < details.length; ++i) {
            let tag = document.createElement("div");
            tag.style = "background-color: yellow; color: red; font-size: 1.6rem; border: 1px solid red; padding: 0.2rem; margin-right: 0.2rem; text-align: center;";
            if (shift*10 <= i && i < (shift+1)*10) {
                tag.innerText = "" + (i - shift*10);

            } else {
                tag.innerText = "" + ((i < shift*10) ? "k" : "j");
            }
            tags.add(tag);
            details[i].prepend(tag);
            details[i].style = "text-decoration: none; opacity: 1;";
        }
    }

    let update = function() {
        for (var i = 0; i < tags.length; ++i) {
            let tag = tags[i]
            if (shift*10 <= i && i < (shift+1)*10) {
                tag.innerText = "" + (i - shift*10);

            } else {
                tag.innerText = "" + ((i < shift*10) ? "k" : "j");
            }
        }
    }

    let is_INSERT_mode = function() {
        return keyword == document.activeElement;
    }

    // assume we start off with INSERT mode
    keyword.focus();
    install();

    document.body.onkeydown = function(ev) {
        if (ev.key === "Escape") {
            keyword.blur();
            shift = 0;
            update();
        }
    }

    document.body.onkeypress = function(ev) {
        var no_modifier = !ev.altKey && !ev.ctrlKey && !ev.metaKey;
        if (no_modifier && !is_INSERT_mode()) {
            if (ev.key === "i") {
                keyword.focus();
                update();
            } else if (ev.key in "0 1 2 3 4 5 6 7 8 9".split(" ")) {
                let i = parseInt(ev.key) + 10*shift;
                if (i < details.length) {
                    details[i].click();
                }
                // shift tags up and down with k/j
            } else if (ev.key === "j") {
                if (10*(shift + 1) < details.length) {
                    shift += 1;
                    update();
                }
            } else if (ev.key === "k") {
                if (shift > 0) {
                    shift -= 1;
                    update();
                }
            }
        }
    };
})();