Duolingo keyboard shortcuts

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

目前為 2024-05-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.8
// @match    https://www.duolingo.com/*
// @grant    none
// @author szupie [email protected]
// @namespace szupie
// @license Unlicense
// ==/UserScript==
(function () {
  'use strict';
  
  const speakerButtonSelector = '[dir][lang] [dir="ltr"] > button, button._2iVKu, button.L2pR0, button._3MUaZ, button._2UR3y, button.hWH3-, button._3TlAm, button._29LnD, button._2sNVM, button._1oX8u, 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);
})();