Skip to content

Instantly share code, notes, and snippets.

@CFiggers
Last active February 1, 2022 03:39
Show Gist options
  • Save CFiggers/1df40b78aef2c667540242ddb3ea834b to your computer and use it in GitHub Desktop.
Save CFiggers/1df40b78aef2c667540242ddb3ea834b to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Read Scribd Aloud
// @namespace http://tampermonkey.net/
// @version 0.3
// @description When reading a Scribd book, press 'l' to start reading and 'x' to stop.
// @author Caleb Figgers
// @match https://www.scribd.com/read/*
// @icon https://s-f.scribdassets.com/favicon.ico?02e8b6ef6?v=4
// @grant none
// ==/UserScript==
async function readpage(dom) {
// To make the narrator read faster/slower:
// Update this ↓ (number between 1-10) and then save (File -> Save or Ctrl + S) and refresh open Scribd tabs.
let speakSpeed = 6; //
// To change the narrator voice:
// Update this ↓ and then save (File -> Save or Ctrl + S) and refresh open Scribd tabs. May be different voices on different computers.
let voiceIndex = 0;
let lang = "en-US";
let myNodelist = dom.querySelectorAll(".text_line");
let text = [];
let curNode = null;
let curNodeTexts = [];
for (let i = 0; i < myNodelist.length; i++) {
if (myNodelist[i].childNodes.length == 2) {
text.push(myNodelist[i].childNodes[0].data);
} else {
curNode = myNodelist[i].childNodes;
curNodeTexts = [];
for (let j = 0; j < curNode.length; j++) {
curNodeTexts.push(curNode[j].innerText);
}
text.push(curNodeTexts.join(''));
// text.push("hello");
};
};
let utterance = new SpeechSynthesisUtterance(text.join(" "));
utterance.rate = speakSpeed;
utterance.voice = await chooseVoice(lang, voiceIndex);
speechSynthesis.speak(utterance);
}
const chooseVoice = async (lang, voiceIndex) => {
const voices = (await getVoices()).filter(voice => voice.lang == lang)
return new Promise(resolve => {
resolve(voices[voiceIndex])
})
}
const getVoices = () => {
return new Promise(resolve => {
let voices = speechSynthesis.getVoices()
if (voices.length) {
resolve(voices)
return
}
speechSynthesis.onvoiceschanged = () => {
voices = speechSynthesis.getVoices()
resolve(voices)
}
})
}
function doc_keyUp(e) {
switch (e.keyCode) {
case 76:
// l
readpage(document);
break;
case 88:
// x
speechSynthesis.cancel();
break;
default:
break;
}
}
document.addEventListener('keyup', doc_keyUp, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment