←back to thread

634 points david927 | 1 comments | | HN request time: 0s | source

What are you working on? Any new ideas that you're thinking about?
Show context
westcort ◴[] No.41346561[source]
I made this bookmarklet, which will speak highlighted text in the browser regardless of platform. It also makes the initial letters of each word bold. This is the code:

javascript:void function(){ javascript:(function(){ var selection = window.getSelection().toString(); if (!selection) { alert("Please select some text on the page."); return; } var encodedSelection = document.createElement("div"); encodedSelection.textContent = selection; var processedContent = encodedSelection.innerHTML.replace(/\n/g, " <br></br> "); var words = processedContent.split(" "); var formattedText = ""; var speechContent = ""; for (var i = 0; i < words.length; i++) { var word = words[i]; var chunkSize = Math.floor(word.length / 3) + 1; var boldPart = "<span style='font-weight:bolder'>" + word.substring(0, chunkSize) + "</span>"; var lightPart = "<span style='font-weight:lighter'>" + word.substring(chunkSize, word.length) + "</span>"; var formattedWord = boldPart + lightPart; if (word.endsWith(".")) { formattedWord += "<span style='color:red'> *</span>"; } formattedText += formattedWord + " "; speechContent += word + " "; } var newWindow = window.open("", "_blank"); newWindow.document.write("<html><head><title>Spoken Content</title></head><body><input type='range' min='0.1' max='10' value='1' step='0.1' id='rate-slider'><p id='content' style='background-color:#EDD1B0;font-size:40;line-height:200%25;font-family:Arial'>"%20+%20formattedText%20+%20"</p></body></html>");%20var%20rateSlider%20=%20newWindow.document.getElementById("rate-slider");%20var%20utterance%20=%20new%20SpeechSynthesisUtterance(speechContent);%20rateSlider.addEventListener("input",%20function()%20{%20utterance.rate%20=%20rateSlider.value;%20window.speechSynthesis.cancel();%20window.speechSynthesis.speak(utterance);%20});%20window.speechSynthesis.speak(utterance);%20})();}();

Also: https://pastebin.com/zuRVpiVh

replies(1): >>41346715 #
SuperHeavy256 ◴[] No.41346715[source]
How do I get it to stop narrating? It's going on and on
replies(1): >>41346832 #
1. westcort ◴[] No.41346832[source]
Just close the original article's window and it will stop narrating.