Duolingo keyboard shortcuts

Press Shift+Space to play sentence audio (plus adds number keys support for more word bank exercises)

当前为 2022-09-15 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name     Duolingo keyboard shortcuts
// @description Press Shift+Space to play sentence audio (plus adds number keys support for more word bank exercises)
// @version  1.1.4
// @match    https://www.duolingo.com/*
// @grant    none
// @author szupie [email protected]
// @namespace szupie
// @license Unlicense
// ==/UserScript==
(function () {
  'use strict';
  
  const speakerButtonSelector = 'button._1KXUd, button._3nOBS, label.sgs9X button, [data-test="speaker-button"]';
  
  function handleKeyboard(e) {
    if (e.shiftKey === true && e.key === ' ') {
      const speakButton = document.querySelector(speakerButtonSelector);
      if (speakButton) {
        speakButton.click();
        e.preventDefault();
      }
    }
    
    // use number keys for sentence completion with word bank
    if (e.key >= "1" && e.key <= "9") {
      if (document.querySelector('[data-test="word-bank"]')) {
        const wordBankButtonSelector = `:nth-child(${e.key}) > [data-test="challenge-tap-token"]`;
        const wordBankButton = document.querySelector(`[data-test="word-bank"] ${wordBankButtonSelector}:not([aria-disabled])`);
        if (wordBankButton) {
          wordBankButton.click();
        } else {
          // remove word from sentence
          const disabledClass = '_2Hlc9';
          document.querySelectorAll(`._1gad7 :not(.${disabledClass})${wordBankButtonSelector}`).forEach(node => node.click());
        }
      }
    }
  }
  
  document.addEventListener('keypress', handleKeyboard, false);
  document.addEventListener('keyup', handleKeyboard, false);
})();