Greasy Fork

TAB shortcut-key play voice for Duolingo

TAB shortcut-key play voice for Duolingo (タブキーで発音を聞く)

目前为 2020-07-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         TAB shortcut-key play voice for Duolingo
// @namespace    TAB shortcut-key play voice for Duolingo
// @version      0.6
// @description  TAB shortcut-key play voice for Duolingo (タブキーで発音を聞く)
// @author       meguru
// @match        https://www.duolingo.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function playSound(evt) {
        const key = {
            tab: 9,
            a: 65
        }

        if (evt.keyCode === key.tab) {
            const ae = document.activeElement;

            // ちいさい再生ボタン(英語->日本語)
            // class="_2cYBf _2dIjg XepLJ _1bJB- vy3TL _3iIWE _1Mkpg _1Dtxl _1sVAI sweRn _1BWZU _2bW5I _3ZpUo"
            const b1 = document.evaluate('//*[@id="root"]/div/div/div/div/div[2]/div/div/div/div/div[2]/div[1]/div/label/button', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

            // でかい再生ボタン(英語->英語)
            // class="_3t_HP _2dIjg XepLJ _1bJB- vy3TL _3iIWE _1Mkpg _1Dtxl _1sVAI sweRn _1BWZU _2bW5I _3ZpUo _2odwU"
            //const b2 = document.evaluate('//*[@id="root"]/div/div/div/div/div[2]/div/div/div/div/div[2]/div[1]/div/label/button', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            const b2 = null

            let clickEvent = new Event('click', {bubbles: true})
            if (b1.snapshotLength > 0) {
                if (b1.snapshotItem(0).dispatchEvent(clickEvent) == false) {
                    console.log("playSound: fail_1")
                } else {
                    //console.log("playSound: 1")
                }
            } else if (b2.snapshotLength > 0) {
                if (b2.snapshotItem(0).dispatchEvent(clickEvent) == false) {
                    console.log("playSound: fail_2")
                } else {
                    //console.log("playSound: 2")
                }
            }
            evt.preventDefault();
        }
    }

    document.addEventListener("keydown", playSound, false);
})();