Skip to content

Instantly share code, notes, and snippets.

@pacochi
Last active September 8, 2017 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pacochi/4295d00d6368c53921047d2a90dca8b5 to your computer and use it in GitHub Desktop.
Save pacochi/4295d00d6368c53921047d2a90dca8b5 to your computer and use it in GitHub Desktop.
安全なオートにゃーん(音声認識ロック付き)
// ==UserScript==
// @name auto nya-n (unlock by speech recognition)
// @namespace hen.acho.co
// @include https://*/web/*
// @version 1.170908
// @description nya-n
// @downloadURL https://gist.github.com/pacochi/4295d00d6368c53921047d2a90dca8b5/raw/auto_nya-n_s_rec.user.js
// @run-at document-idle
// @grant none
// ==/UserScript==
/*
ボタンに対して右クリック的な動作をするとマイク許可のダイアログが出るので、
許可して「にゃーん」と録音すると自動的に「にゃーん」とトゥートされる。
まだ Chrome でしか使えなかった。
Firefox、media.webspeech.recognition.enable を true にしてもだめ、何故かモバイル版でもだめ。
*/
{
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 recognition = new speechRecognition();
recognition.lang = 'ja';
recognition.addEventListener('result', e => {
const resultText = e.results.item(0).item(0).transcript;
console.log(resultText);
if (/にゃ[あーん]/.test(resultText)) tootTextarea.value = 'にゃーん';
// マストドンのテキスト入力欄に入れたテキストが消えなくなるおまじない
// 参考 : https://gist.github.com/unarist/bf89a5f054fb1d58a39067f61626b9c2
const event = document.createEvent('HTMLEvents');
event.initEvent('input', true, false);
tootTextarea.dispatchEvent(event);
tootButton.click();
}, false);
tootButton.addEventListener('contextmenu', e => {
recognition.start();
e.preventDefault();
}, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment