Wanikani s2speak

Highlight the text, and press s to speak, anywhere on the website

当前为 2020-05-04 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wanikani s2speak
// @namespace    http://polvcode.dev/
// @version      0.1
// @description  Highlight the text, and press s to speak, anywhere on the website
// @author       Pacharapol Withayasakpunt
// @match        https://*.wanikani.com/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/xregexp-all.js
// @grant        none
// ==/UserScript==

(function () {
  window.addEventListener("keydown", (ev) => {
    const lang = 'ja'
    const s = window.getSelection().toString();
    if (s && XRegExp('[\\p{Han}\\p{Hiragana}\\p{Katakana}]').test(s)) {
      speak(s, lang);
    }
  });

  const allVoices = {}
  speechSynthesis.getVoices().map(v => {
    allVoices[v.lang] = v.lang
  })
  speechSynthesis.onvoiceschanged = () => {
    speechSynthesis.getVoices().map(v => {
      allVoices[v.lang] = v.lang
    })
  }

  function speak (s, lang) {
    const voices = Object.keys(allVoices)
    const stage1 = () => voices.filter((v) => v === lang)[0]
    const stage2 = () => {
      const m1 = lang.substr(0, 2)
      const m2 = lang.substr(3, 2)
      const r1 = new RegExp(`^${m1}[-_]${m2}`, 'i')
      return voices.filter((v) => r1.test(v))[0]
    }
    const stage3 = () => {
      const m1 = lang.substr(0, 2).toLocaleLowerCase()
      return voices.filter((v) => v.toLocaleLowerCase().startsWith(m1))[0]
    }

    lang = stage1() || stage2() || stage3() || ''

    if (lang) {
      const utterance = new SpeechSynthesisUtterance(s)
      utterance.lang = lang
      speechSynthesis.speak(utterance)
    }
  }
})()