Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- javascript:void function() { function mergeSort(arr) { if (arr.length < 2) return arr; const middle = Math.floor(arr.length / 2); const left = arr.slice(0, middle); const right = arr.slice(middle); return merge(mergeSort(left), mergeSort(right)); } function merge(left, right) { const result = []; while (left.length && right.length) { if (left[0] <= right[0]) result.push(left.shift()); else result.push(right.shift()); } return result.concat(left).concat(right); } function getTopWords(text) { text = text.replace(/\n/g, " ").toLowerCase().replace(/[^a-z\- ]/g, "").split(" "); const wordCounts = {}; text.forEach(word => { if (word) wordCounts[word] = (wordCounts[word] || 0) + 1; }); const sortedWords = Object.keys(wordCounts).sort((a, b) => wordCounts[b] - wordCounts[a]); return sortedWords; } function makeRegex(wordArray) { const commonWords = ["the", "of", "to", "and", "a", "in", "is", "it", "you", "that", "he", "was", "for", "on", "are", "with", "as", "i", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "hot", "word", "but", "what", "some", "we", "can", "out", "other", "were", "all", "there", "when", "up", "use", "your", "how", "said", "an", "each", "she", "which", "do", "their", "time", "if", "will", "way", "about", "many", "then", "them", "write", "would", "like", "so", "these", "her", "long", "make", "thing", "see", "him", "two", "has", "look", "more", "day", "could", "go", "come", "did", "number", "sound", "no", "most", "people", "my", "over", "know", "water", "than", "call", "first", "who", "may", "down", "side", "been", "now", "find", "any", "new", "work", "part", "take", "get", "place", "made", "live", "where", "after", "back", "little", "only", "round", "man", "year", "came", "show", "every", "good", "me", "give", "our", "under", "name", "very", "through", "just", "form", "sentence", "great", "think", "say", "help", "low", "line", "differ", "turn", "cause", "much", "mean", "before", "move", "right", "boy", "old", "too", "same", "tell", "does", "set", "three", "want", "air", "well", "also", "play", "small", "end", "put", "home", "read", "hand", "port", "large", "spell", "add", "even", "land", "here", "must", "big", "high", "such", "follow", "act", "why", "ask", "men", "change", "went", "light", "kind", "off", "need", "house", "picture", "try", "us", "again", "animal", "point", "mother", "world", "near", "build", "self", "earth", "father", "head"]; const filteredArray = wordArray.filter(word => !commonWords.includes(word)); return new RegExp(filteredArray.slice(0, 10).join("|"), "gi"); } function countMatches(content, re) { return (content.match(re) || []).length; } function rankContent(contentArray, re) { const rankedContent = contentArray.map(paragraph => { const matchCount = countMatches(paragraph, re); return { paragraph, matchCount }; }); rankedContent.sort((a, b) => b.matchCount - a.matchCount); return rankedContent.map(item => item.paragraph).join("\n"); } function doAction() { const selection = window.getSelection().toString(); if (!selection) { alert("Please select some text on the page."); return; } const paragraphs = selection.split(/\n+/); const sortedWords = getTopWords(selection); const regex = makeRegex(sortedWords); const rankedParagraphs = rankContent(paragraphs, regex); const encodedSelection = document.createElement("div"); encodedSelection.innerHTML = rankedParagraphs.replace(/\n/g, "<br>"); const words = encodedSelection.innerHTML.split(" "); let formattedText = ""; let speechContent = ""; for (const word of words) { if (word.trim() === "") continue; const chunkSize = Math.floor(word.length / 3) + 1; const boldPart = "<span style='font-weight:bolder'>" + word.substring(0, chunkSize) + "</span>"; const lightPart = "<span style='font-weight:lighter'>" + word.substring(chunkSize) + "</span>"; let formattedWord = boldPart + lightPart; if (word.endsWith(".")) { formattedWord += "<span style='color:red'> *</span>"; } formattedText += formattedWord + " "; speechContent += word + " "; } const 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:40px;line-height:200%;font-family:Arial'>" + formattedText + "</p></body></html>"); const rateSlider = newWindow.document.getElementById("rate-slider"); const utterance = new SpeechSynthesisUtterance(speechContent); rateSlider.addEventListener("input", function() { utterance.rate = rateSlider.value; window.speechSynthesis.cancel(); window.speechSynthesis.speak(utterance); }); window.speechSynthesis.speak(utterance); } doAction(); }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement