-
-
Save unarist/d71b04702091fe5a8143af26b78410da to your computer and use it in GitHub Desktop.
安全なオートにゃーん(音声認識ロック付き)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name auto nya-n (unlock by speech recognition) | |
// @namespace https://github.com/unarist/ | |
// @include https://*/web/* | |
// @conrtibutor pacochi https://gist.github.com/pacochi/4295d00d6368c53921047d2a90dca8b5 | |
// @version 1.170908 | |
// @description nya-n | |
// @downloadURL https://gist.github.com/unarist/d71b04702091fe5a8143af26b78410da/raw/auto_nya-n_s_rec.user.js | |
// @run-at document-idle | |
// @grant none | |
// ==/UserScript== | |
/* | |
pacochi氏版からの変更点 | |
* UI構築が完了する前に起動することがあったのでウェイトを入れてみる | |
* 発声待ち受け中に説明テキスト表示 | |
* 既になにか入力されている時や待ち受け中に入力された時は終了 | |
* 開始から2秒待っても発声を始めなければ終了 | |
* 「にゃーん」固定ではなく入力テキストを使うように | |
*/ | |
/* | |
ボタンに対して右クリック的な動作をするとマイク許可のダイアログが出るので、 | |
許可して「にゃーん」と録音すると自動的に「にゃーん」とトゥートされる。 | |
まだ Chrome でしか使えなかった。 | |
Firefox、media.webspeech.recognition.enable を true にしてもだめ、何故かモバイル版でもだめ。 | |
*/ | |
setTimeout(()=>{ | |
const tootButton = document.querySelector('.compose-form button.button'); | |
const tootTextarea = document.querySelector('.compose-form textarea'); | |
const speechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; | |
if (tootButton && tootTextarea && speechRecognition) { | |
const placeholderText = 'マイクに向かって「にゃーん」'; | |
const recognition = new speechRecognition(); | |
recognition.lang = 'ja'; | |
recognition.addEventListener('result', e => { | |
if (tootTextarea.value !== placeholderText) return; | |
const resultText = e.results.item(0).item(0).transcript; | |
console.log(resultText); | |
const match = resultText.match(/にゃ[あーん]+/); | |
if (match) { | |
tootTextarea.value = match[0]; | |
// マストドンのテキスト入力欄に入れたテキストが消えなくなるおまじない | |
// 参考 : https://gist.github.com/unarist/bf89a5f054fb1d58a39067f61626b9c2 | |
const event = document.createEvent('HTMLEvents'); | |
event.initEvent('input', true, false); | |
tootTextarea.dispatchEvent(event); | |
tootButton.click(); | |
} | |
}, false); | |
let timerId; | |
recognition.addEventListener('speechstart', e => { | |
clearTimeout(timerId); | |
}); | |
recognition.addEventListener('end', e => { | |
if (tootTextarea.value === placeholderText) { | |
tootTextarea.value = ''; | |
} | |
}, false); | |
tootButton.addEventListener('contextmenu', e => { | |
if (tootTextarea.value !== '') return; | |
timerId = setTimeout(() => recognition.abort(), 2000); | |
recognition.start(); | |
tootTextarea.value = placeholderText; | |
e.preventDefault(); | |
}, false); | |
} | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment