Skip to content

Instantly share code, notes, and snippets.

@houkanshan
Last active September 10, 2023 23:11
Show Gist options
  • Save houkanshan/a8426a7fb6f6bf5e529b6eca6e8319ba to your computer and use it in GitHub Desktop.
Save houkanshan/a8426a7fb6f6bf5e529b6eca6e8319ba to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name google sheet read.
// @namespace https://houkanshan.com
// @version 0.1
// @description try to take over the world!
// @author Houkanshan
// @match 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(); }
var msgSlow = new SpeechSynthesisUtterance(text);
utterances.push(msg);
msgSlow.rate = 0.5;
msgSlow.addEventListener('end', () => {
if (msg._stop) { return reject(); }
currMsg = null;
resolve();
});
currMsg = msgSlow;
window.speechSynthesis.speak(msgSlow);
});
currMsg = msg;
window.speechSynthesis.speak(msg);
});
}
(function() {
'use strict';
document.addEventListener('copy', function(e) {
if (currMsg) {
currMsg._stop = true;
currMsg = null;
}
utterances = [];
var text = e.clipboardData.getData('Text');
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