-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaderBookmarklet.min.js
1 lines (1 loc) · 4.7 KB
/
readerBookmarklet.min.js
1
javascript:!(()=>{const FIRST_ELEMENT=0,ONE=1,createDomElement=(a,b,c)=>{'use strict';if("string"!=typeof a&&a.length<ONE)return null;const d=document.createElement(a);if(b!==void 0&&0<Object.keys(b).length&&b.constructor===Object)for(const a in b)Object.prototype.hasOwnProperty.call(b,a)&&(d[a]=b[a]);if(void 0!==c&&0<Object.keys(c).length&&c.constructor===Object)for(const a in c)Object.prototype.hasOwnProperty.call(c,a)&&d.setAttribute(a,c[a]);return d},appendChildsToDomElement=(a,b)=>{a instanceof HTMLElement&&Array.isArray(b)&&!(b.length<ONE)&&b.forEach(b=>{b instanceof HTMLElement&&a.appendChild(b)})},readerBookmarklet=()=>{'use strict';const a=window.speechSynthesis;let b=new SpeechSynthesisUtterance,c=null,d=null,e=null,f=null,g=null,h=null,i=null;const j={};let k=null;const l=()=>{a.getVoices().forEach(a=>{const b=a.lang.split("-")[FIRST_ELEMENT];j[b]===void 0&&(j[b]=[]),j[b].push(a)})},m=()=>{d=createDomElement("style",{textContent:"\n body.reader-bookmarklet-searching *:not([id^=reader-bookmarklet]):hover {\n border: 1px dotted #f00;\n }\n\n #reader-bookmarklet {\n background: rgba(68,68,68,0.5);\n border: 1px solid #333;\n color: #333;\n top: 0;\n right: 0;\n padding: 20px;\n position: fixed;\n z-index: 9999;\n }\n\n #reader-bookmarklet-title {\n margin-bottom: 10px;\n margin-top: 0;\n\n font-size: 20px;\n }\n "},{id:"reader-bookmarklet-style",type:"text/css"}),document.querySelector("head").appendChild(d)},n=()=>{c=createDomElement("div",{},{id:"reader-bookmarklet"}),e=createDomElement("select",{},{id:"reader-bookmarklet-lang"}),e.appendChild(createDomElement("option",{textContent:"---"},{})),f=createDomElement("select",{},{id:"reader-bookmarklet-voice"}),f.appendChild(createDomElement("option",{textContent:"---"},{})),g=createDomElement("button",{innerHTML:"🔎"},{id:"reader-bookmarklet-search"}),h=createDomElement("button",{textContent:"\u23EF"},{id:"reader-bookmarklet-play-pause"}),i=createDomElement("button",{textContent:"\u23F9"},{id:"reader-bookmarklet-stop"}),appendChildsToDomElement(c,[createDomElement("h1",{textContent:"Reader Bookmarklet"},{id:"reader-bookmarklet-title"}),createDomElement("label",{textContent:"Language"},{for:"reader-bookmarklet-lang"}),e,createDomElement("br",{},{}),createDomElement("label",{textContent:"Voice"},{for:"reader-bookmarklet-voice"}),f,createDomElement("br",{},{}),g,h,i]),document.querySelector("body").appendChild(c)},o=()=>null!==document.getElementById("reader-bookmarklet-style")&&null!==document.getElementById("reader-bookmarklet"),p=()=>{for(const a in e.innerHTML="",j){if(!Object.prototype.hasOwnProperty.call(j,a))continue;const b=createDomElement("option",{textContent:a},{value:a});a===k&&(b.setAttribute("selected","selected"),q(a)),e.appendChild(b)}},q=a=>{if(f.innerHTML="",Object.prototype.hasOwnProperty.call(j,a))for(let b=0;b<j[a].length;b++){const c=j[a][b],d=createDomElement("option",{textContent:c.name},{value:b});b===FIRST_ELEMENT&&d.setAttribute("selected","selected"),f.appendChild(d)}},r=a=>{const b=a.target;for(const b in e.options)Object.prototype.hasOwnProperty.call(e.options,b)&&e.options[b].removeAttribute("selected");e.options[b.selectedIndex].setAttribute("selected","selected"),q(b.value)},s=()=>{const a=f.options[f.selectedIndex].value,c=e.options[e.selectedIndex].value;console.log(a),console.log(c),b.voice=j[c][a],b.lang=c},t=()=>{null===k&&(k=document.getElementsByTagName("html")[FIRST_ELEMENT].getAttribute("lang"),k=null===k?"en":k.split("-")[FIRST_ELEMENT]),m(),n(),g.addEventListener("click",()=>{document.querySelector("body").classList.add("reader-bookmarklet-searching");const a=document.querySelectorAll("body.reader-bookmarklet-searching *:not([id^=reader-bookmarklet])");a.forEach(c=>{c.addEventListener("click",c=>{c.stopPropagation(),console.log(c.target),b=new SpeechSynthesisUtterance(c.target.innerText),console.log(b.text),document.querySelector("body").classList.remove("reader-bookmarklet-searching"),a.forEach(a=>{a.removeEventListener("click",()=>{})})},{capture:!1,once:!0})})}),h.addEventListener("click",()=>{a.speaking?a.pause():a.paused?(s(),a.resume()):null!==b.text&&""!==b.text&&(s(),a.speak(b))}),i.addEventListener("click",()=>{a.cancel()}),window.speechSynthesis.onvoiceschanged=()=>{l(),p(),e.onchange=r,f.onchange=a=>{for(const b in f.options)Object.prototype.hasOwnProperty.call(f.options,b)&&f.options[b].removeAttribute("selected");f.options[a.target.selectedIndex].setAttribute("selected","selected")}}};return{execute:()=>{o()?p():t()}}};readerBookmarklet().execute();})();