Last active
August 31, 2017 04:37
-
-
Save houkanshan/405e9437b54a321cd9aa0ddd09e327c7 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 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