jisho-shortcuts

Shortcuts for Jisho.

Tính đến 04-04-2020. Xem phiên bản mới nhất.

// ==UserScript==
// @name         jisho-shortcuts
// @namespace    http://tampermonkey.net/
// @version      0.3
// @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 < tags.length; ++i) {
            tags[i].remove();
        }
        tags = [];
        if (!INSERT) {
            for (i = 0; i < details.length; ++i) {
                if (shift*10 <= i && i < (shift+1)*10) {
                    let tag = document.createElement("div");
                    tag.style = "background-color: yellow; color: red; font-size: 1rem; border: 1px solid red; padding: 0.2rem; margin-right: 0.2rem; text-align: center; display: inline;";
                    tag.innerText = "" + (i - shift*10);
                    tags.add(tag);
                    details[i].prepend(tag);
                    details[i].style = "text-decoration: none; opacity: 1;";
                }
            }
        }
    }

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

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

    document.body.onkeypress = function(ev) {
        var no_modifier = !ev.altKey && !ev.ctrlKey && !ev.metaKey;
        if (no_modifier && !INSERT) {
            if (ev.key === "i") {
                keyword.focus();
                INSERT = true;
                install();
            } 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;
                    install();
                }
            } else if (ev.key === "k") {
                if (shift > 0) {
                    shift -= 1;
                    install();
                }
            }
        }
    };
})();