Skip to content

Instantly share code, notes, and snippets.

@houkanshan
Last active August 31, 2017 04:37
Show Gist options
  • Save houkanshan/405e9437b54a321cd9aa0ddd09e327c7 to your computer and use it in GitHub Desktop.
Save houkanshan/405e9437b54a321cd9aa0ddd09e327c7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Clipboard pronouncer
// @namespace https://houkanshan.com
// @version 0.1
// @description try to take over the world!
// @author Houkanshan
// @include http://*/*
// @include https:/*/*
// @exclude https://docs.google.com/spreadsheets*
// @grant none
// ==/UserScript==
var utterances = []; // Chrome Bug: https://stackoverflow.com/a/35935851
var currMsg = null;
function read(text) {
return new Promise((resolve, reject) => {
window.speechSynthesis.cancel();
var msg = new SpeechSynthesisUtterance(text);
utterances.push(msg);
msg.addEventListener('end', () => {
if (msg._stop) { return reject(); }
resolve();
});
currMsg = msg;
window.speechSynthesis.speak(msg);
});
}
(function() {
'use strict';
if (location.href.indexOf('https://https://docs.google.com/spreadsheets') === 0) { return; } // exclude doesn't work..
document.addEventListener('copy', function(e) {
if (currMsg) {
currMsg._stop = true;
currMsg = null;
}
utterances = [];
var text = window.getSelection().toString();
text.split(/\n+/).reduce((acc, word) => {
return acc.then(() => read(word));
}, Promise.resolve()).then(() => utterances = [], () => {});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment