-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
1 lines (1 loc) · 15.6 KB
/
Copy pathreader.js
File metadata and controls
1 lines (1 loc) · 15.6 KB
1
const defaultSettings={font:"default",fontSize:18,lineSpacing:1.7,bgColor:"#111111",textColor:"#fafafa"};function getSettings(){try{return JSON.parse(localStorage.getItem("readerSettings"))||{...defaultSettings}}catch{return{...defaultSettings}}}function saveSettings(e){localStorage.setItem("readerSettings",JSON.stringify(e))}function applySettingsToContent(e){const t=document.getElementById("readerContent");let n="",i=e.bold?"bold":e.weight?e.weight:"normal",o=e.italic?"italic":"normal";if("opendyslexic"===e.font&&!document.getElementById("opendyslexic-css")){const e=document.createElement("link");e.id="opendyslexic-css",e.rel="stylesheet",e.href="fonts/opendyslexic.css",document.head.appendChild(e)}n={opendyslexic:'"OpenDyslexic", Arial, sans-serif',lexend:'"Lexend", Arial, sans-serif',atkinson:'"Atkinson Hyperlegible", Arial, sans-serif',arial:"Arial, Helvetica, sans-serif",helvetica:"Helvetica, Arial, sans-serif",verdana:"Verdana, Geneva, sans-serif",tahoma:"Tahoma, Geneva, sans-serif",trebuchet:'"Trebuchet MS", Helvetica, sans-serif',sans:"sans-serif",times:'"Times New Roman", Times, serif',georgia:"Georgia, serif",garamond:"Garamond, serif",serif:"serif",comic:'"Comic Sans MS", "Comic Sans", cursive, sans-serif',comic_neue:'"Comic Neue", cursive, sans-serif',courier:'"Courier New", Courier, monospace',monospace:"monospace","Segoe UI":'"Segoe UI", Arial, sans-serif',Calibri:"Calibri, Arial, sans-serif",Candara:"Candara, Arial, sans-serif",Corbel:"Corbel, Arial, sans-serif",Cambria:"Cambria, serif",Constantia:"Constantia, serif","Palatino Linotype":'"Palatino Linotype", serif',"Book Antiqua":'"Book Antiqua", serif',"Franklin Gothic Medium":'"Franklin Gothic Medium", Arial, sans-serif',"Century Gothic":'"Century Gothic", Arial, sans-serif',"Gill Sans MT":'"Gill Sans MT", Arial, sans-serif',"Sitka Text":'"Sitka Text", serif',Sylfaen:"Sylfaen, serif","Microsoft Sans Serif":'"Microsoft Sans Serif", Arial, sans-serif',"Segoe UI Semibold":'"Segoe UI Semibold", Arial, sans-serif',Consolas:"Consolas, monospace","Lucida Console":'"Lucida Console", monospace',"Lucida Sans Typewriter":'"Lucida Sans Typewriter", monospace',Impact:"Impact, Arial Black, sans-serif","Arial Black":'"Arial Black", Arial, sans-serif',"Copperplate Gothic Bold":'"Copperplate Gothic Bold", Arial, sans-serif',"Berlin Sans FB":'"Berlin Sans FB", Arial, sans-serif',Gabriola:"Gabriola, cursive","Ink Free":'"Ink Free", cursive',"Segoe Script":'"Segoe Script", cursive',"Segoe Print":'"Segoe Print", cursive',default:"inherit"}[e.font]||e.font||"inherit",t.style.fontFamily=n,t.style.fontWeight=i,t.style.fontStyle=o,t.style.fontSize=e.fontSize+"px",t.style.lineHeight=e.lineSpacing,t.style.background=e.bgColor,t.style.color=e.textColor}document.addEventListener("DOMContentLoaded",async()=>{!function(){if(!document.getElementById("opendyslexic-css")){const e=document.createElement("link");e.id="opendyslexic-css",e.rel="stylesheet",e.href="fonts/opendyslexic.css",document.head.appendChild(e)}const e=(e,t,n="normal",i="normal")=>{const o=`font-${e.replace(/\s/g,"")}-${n}-${i}`;if(document.getElementById(o))return;const l=document.createElement("style");l.id=o,l.textContent=`@font-face { font-family: '${e}'; src: url('${t}'); font-weight: ${n}; font-style: ${i}; font-display: swap; }`,document.head.appendChild(l)};e("Fast Sans","fonts/Fast_Sans.ttf","normal","normal"),e("Fast Sans","fonts/kindle/Fast_Sans/Fast_Sans-Bold.otf","bold","normal"),e("Fast Sans","fonts/kindle/Fast_Sans/Fast_Sans-Italic.otf","normal","italic"),e("Fast Sans","fonts/kindle/Fast_Sans/Fast_Sans-BoldItalic.otf","bold","italic"),e("Fast Serif","fonts/Fast_Serif.ttf","normal","normal"),e("Fast Serif","fonts/kindle/Fast_Serif/Fast_Serif-Bold.otf","bold","normal"),e("Fast Serif","fonts/kindle/Fast_Serif/Fast_Serif-Italic.otf","normal","italic"),e("Fast Serif","fonts/kindle/Fast_Serif/Fast_Serif-BoldItalic.otf","bold","italic"),e("Fast Mono","fonts/Fast_Mono.ttf","normal","normal"),e("Fast Mono","fonts/kindle/Fast_Mono/Fast_Mono-Bold.otf","bold","normal"),e("Fast Mono","fonts/kindle/Fast_Mono/Fast_Mono-Italic.otf","normal","italic"),e("Fast Mono","fonts/kindle/Fast_Mono/Fast_Mono-BoldItalic.otf","bold","italic")}();const e=document.getElementById("themeToggle");function t(e){document.body.classList.toggle("light-theme","light"===e),localStorage.setItem("readerTheme",e)}t(localStorage.getItem("readerTheme")||"dark"),e&&e.addEventListener("click",()=>{t(document.body.classList.contains("light-theme")?"dark":"light")});const n=document.getElementById("readerTitle"),i=document.getElementById("readerContent"),o=document.getElementById("backBtn"),l=document.getElementById("exportBtn"),a=document.getElementById("settingsBtn"),s=document.getElementById("settingsDrawer"),c=document.getElementById("closeSettingsDrawer"),d=document.getElementById("ctlFont"),r=document.getElementById("ctlFontSize"),m=document.getElementById("ctlBold"),g=document.getElementById("ctlItalic"),f=document.getElementById("ctlUnderline"),u=document.getElementById("ctlUnderlineDotted"),p=document.getElementById("ctlUnderlineDashed"),h=document.getElementById("ctlHighlight"),y=document.getElementById("ctlStrength"),v=document.getElementById("ctlStrengthVal"),S=document.getElementById("ctlOpacity"),x=document.getElementById("ctlOpacityVal"),k=document.getElementById("ctlLineHeight"),C=document.getElementById("ctlLineHeightVal"),b=document.getElementById("ctlWeight"),E=document.getElementById("ctlWeightVal"),L=document.getElementById("ctlSkipMinus"),B=document.getElementById("ctlSkipPlus"),I=document.getElementById("ctlSkipValue"),w=document.getElementById("ctlHighlightColor"),F=document.getElementById("ctlInkColor"),M=document.getElementById("ctlFadeColor"),$=document.getElementById("ctlBackgroundColor"),T=document.getElementById("ctlInk"),U=document.getElementById("ctlFade"),A=document.getElementById("ctlBackground"),_=document.getElementById("ctlDone"),O=r,D=document.getElementById("exportModal"),H=document.getElementById("closeExport"),R=document.getElementById("exportFormat"),P=document.getElementById("exportFilename"),j=document.getElementById("exportConfirm");function G(){s.style.display="none"}function z(e){const t=Object.assign({},getSettings(),e);saveSettings(t),applySettingsToContent(t),J(t)}function J(e){const t=i.querySelectorAll("p, li, h1, h2, h3, h4, h5, h6, div"),n=/[\p{Emoji_Presentation}\p{Extended_Pictographic}]/u;t.forEach(t=>{const i=t.textContent.split(/(\s+)/);let o=0;t.innerHTML=i.map((t,i)=>{if(/^\s+$/.test(t))return t;const l=n.test(t)&&0===t.replace(n,"").length,a=!/^\s+$/.test(t)&&!l;let s=!1;s=0===e.skip?a:a&&o%e.skip===0;let c="";if(s){const n=Math.max(0,Math.min(100,e.strength||100)),i=Math.ceil(t.length*(n/100)),o=t.slice(0,i),l=t.slice(i);let a="";if("ink"===e.mode&&(a+=`color:${e.inkColor||"#d32f2f"};`),"fade"===e.mode&&(a+=`color:${e.fadeColor||"#f44336"};opacity:0.5;`),e.highlight){const t=function(e){const t=e.replace("#",""),n=parseInt(3===t.length?t.split("").map(e=>e+e).join(""):t,16);return[n>>16&255,n>>8&255,255&n]}(e.highlightColor||"#fff176"),n="fade"===e.mode?.25:.6,i=Math.max(.05,n*((e.strength||100)/100)*((e.opacity||100)/100));a+=`background:${`rgba(${t[0]}, ${t[1]}, ${t[2]}, ${i})`};padding:2px 4px;border-radius:4px;`}a+=`font-weight:${e.bold?"bold":e.weight||400};`,e.italic&&(a+="font-style:italic;"),e.underlineStyle&&(a+=`text-decoration:underline; text-decoration-style:${e.underlineStyle};`),a+=`word-spacing:${e.skip||0}px;`;let s="";l&&void 0!==e.opacity&&100!==e.opacity&&(s+=`opacity:${e.opacity/100};`),s+=`font-weight:normal;font-style:normal;word-spacing:${e.skip||0}px;`,c=`<span style="${a}">${o}</span>`,l&&(c+=`<span style="${s}">${l}</span>`)}else{let n="font-weight:normal;font-style:normal;";n+=`word-spacing:${e.skip||0}px;`,c=`<span style="${n}">${t}</span>`}return a&&o++,c}).join(""),t.style.opacity=1}),e.background?i.style.background=e.backgroundColor||e.bgColor||"#fffbe6":i.style.background=e.bgColor||""}if(chrome.storage.local.get(["readerFile"],async e=>{if(!e.readerFile)return n.textContent="No file loaded",void(i.textContent="Please use the Converter tab to upload a file.");const{name:t,type:o,data:l}=e.readerFile;n.textContent=t;const a=new Uint8Array(l).buffer;try{if("text/plain"===o||t.endsWith(".txt")){const e=window.parseTxt(a);i.textContent=e}else if("application/vnd.openxmlformats-officedocument.wordprocessingml.document"===o||t.endsWith(".docx")){i.innerHTML='<div class="loading">Parsing DOCX...</div>';const e=await window.parseDocx(a);i.innerHTML=e}else if("application/epub+zip"===o||t.endsWith(".epub")){i.innerHTML='<div class="loading">Parsing EPUB...</div>';const e=await window.parseEpub(a);i.innerHTML=e}else i.textContent="Unsupported file type."}catch(e){i.textContent="Error parsing file: "+e.message}const s=Object.assign({bold:!1,italic:!1,underline:!1,highlight:!1,strength:100,opacity:100,weight:400,skip:0,highlightColor:"#fff176",mode:"ink",background:!1},getSettings());applySettingsToContent(s),J(s)}),a.addEventListener("click",function(){const e=Object.assign({},getSettings());e.bold=e.bold||!1,e.italic=e.italic||!1,e.underline=e.underline||!1,e.highlight=e.highlight||!1,e.strength=e.strength||100,e.opacity=e.opacity||100,e.weight=e.weight||400,e.skip=e.skip||0,e.inkColor=e.inkColor||"#d32f2f",e.fadeColor=e.fadeColor||"#f44336",e.backgroundColor=e.backgroundColor||"#fffbe6",e.highlightColor=e.highlightColor||"#fff176",e.underlineStyle=e.underlineStyle||"solid",e.mode=e.mode||"ink",e.background=!!e.background,d&&(d.value=e.font||"inherit"),O&&(O.value=e.fontSize?e.fontSize+"px":"18px"),m&&m.classList.toggle("active",!!e.bold),g&&g.classList.toggle("active",!!e.italic),f&&f.classList.toggle("active","solid"===e.underlineStyle),u&&u.classList.toggle("active","dotted"===e.underlineStyle),p&&p.classList.toggle("active","dashed"===e.underlineStyle),h&&h.classList.toggle("active",!!e.highlight),y&&(y.value=e.strength,v.textContent=e.strength+"%"),S&&(S.value=e.opacity,x.textContent=e.opacity+"%"),k&&(k.value=e.lineSpacing||1.7,C.textContent=(e.lineSpacing||1.7)+"x"),b&&(b.value=e.weight||400,E.textContent=e.weight||400),I&&(I.textContent=e.skip||0),F&&(F.value=e.inkColor||"#d32f2f"),M&&(M.value=e.fadeColor||"#f44336"),$&&($.value=e.backgroundColor||"#fffbe6"),w&&(w.value=e.highlightColor||"#fff176"),T&&T.classList.toggle("active","ink"===e.mode),U&&U.classList.toggle("active","fade"===e.mode),A&&(A.checked=!!e.background),s.style.display="flex"}),c.addEventListener("click",G),d&&d.addEventListener("change",()=>{const e=getSettings();z({font:d.value,bold:e.bold,italic:e.italic})}),O&&O.addEventListener("change",()=>{z({fontSize:parseInt(O.value,10)||18})}),m&&m.addEventListener("click",()=>{m.classList.toggle("active");const e=getSettings();z({bold:m.classList.contains("active"),font:e.font,italic:e.italic})}),g&&g.addEventListener("click",()=>{g.classList.toggle("active");const e=getSettings();z({italic:g.classList.contains("active"),font:e.font,bold:e.bold})}),f&&f.addEventListener("click",()=>{f.classList.add("active"),u.classList.remove("active"),p.classList.remove("active"),z({underlineStyle:"solid"})}),u&&u.addEventListener("click",()=>{f.classList.remove("active"),u.classList.add("active"),p.classList.remove("active"),z({underlineStyle:"dotted"})}),p&&p.addEventListener("click",()=>{f.classList.remove("active"),u.classList.remove("active"),p.classList.add("active"),z({underlineStyle:"dashed"})}),h&&h.addEventListener("click",()=>{h.classList.toggle("active"),z({highlight:h.classList.contains("active")})}),y&&y.addEventListener("input",()=>{v.textContent=y.value+"%",z({strength:parseInt(y.value,10)})}),S&&S.addEventListener("input",()=>{x.textContent=S.value+"%",z({opacity:parseInt(S.value,10)})}),k&&k.addEventListener("input",()=>{C.textContent=k.value+"x",z({lineSpacing:parseFloat(k.value)})}),b&&b.addEventListener("input",()=>{E.textContent=b.value,z({weight:parseInt(b.value,10)})}),L&&L.addEventListener("click",()=>{const e=Math.max(0,parseInt(I.textContent,10)-1);I.textContent=e,z({skip:e})}),B&&B.addEventListener("click",()=>{const e=Math.min(30,parseInt(I.textContent,10)+1);I.textContent=e,z({skip:e})}),F&&F.addEventListener("input",()=>z({inkColor:F.value})),M&&M.addEventListener("input",()=>z({fadeColor:M.value})),$&&$.addEventListener("input",()=>z({backgroundColor:$.value})),w&&w.addEventListener("input",()=>z({highlightColor:w.value})),T&&T.addEventListener("click",()=>{T.classList.add("active"),U.classList.remove("active"),z({mode:"ink"})}),U&&U.addEventListener("click",()=>{U.classList.add("active"),T.classList.remove("active"),z({mode:"fade"})}),A&&A.addEventListener("change",()=>z({background:A.checked})),_&&_.addEventListener("click",()=>G()),l.onclick=null,H.onclick=null,j.onclick=null,l.addEventListener("click",()=>{P.value=n.textContent||"exported",D.style.display="flex"}),H.addEventListener("click",()=>{D.style.display="none"}),D.addEventListener("mousedown",e=>{e.target===D&&(D.style.display="none")}),R&&!R.querySelector('option[value="epub"]')){const e=document.createElement("option");e.value="epub",e.textContent="EPUB",R.appendChild(e)}j.addEventListener("click",()=>{const e=R.value||"html",t=(P.value||n.textContent||"exported").replace(/\.[^/.]+$/,"");if("html"===e){getSettings();const e=`<!doctype html><html><head><meta charset="utf-8"><title>${t}</title></head><body>${i.innerHTML}</body></html>`,n=new Blob([e],{type:"text/html"}),o=URL.createObjectURL(n),l=document.createElement("a");l.href=o,l.download=t+".html",document.body.appendChild(l),l.click(),setTimeout(()=>{document.body.removeChild(l),URL.revokeObjectURL(o)},200)}else if("txt"===e){const e=i.textContent||"",n=new Blob([e],{type:"text/plain"}),o=URL.createObjectURL(n),l=document.createElement("a");l.href=o,l.download=t+".txt",document.body.appendChild(l),l.click(),setTimeout(()=>{document.body.removeChild(l),URL.revokeObjectURL(o)},200)}else if("epub"===e)if(window.JSZip||"undefined"==typeof JSZip||(window.JSZip=JSZip),window.JSZip){const e=`<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html>\n<html xmlns="http://www.w3.org/1999/xhtml"><head><title>${t}</title><meta charset="utf-8"/></head><body>${i.innerHTML}</body></html>`,n="<?xml version='1.0' encoding='utf-8'?>\n<container version='1.0' xmlns='urn:oasis:names:tc:opendocument:xmlns:container'>\n<rootfiles>\n<rootfile full-path='OEBPS/content.xhtml' media-type='application/xhtml+xml'/></rootfiles>\n</container>",o=new window.JSZip;o.file("mimetype","application/epub+zip"),o.folder("META-INF").file("container.xml",n);const l=o.folder("OEBPS");l.file("content.xhtml",e),l.file("content.opf",`<?xml version='1.0' encoding='utf-8'?>\n<package xmlns='http://www.idpf.org/2007/opf' unique-identifier='BookId' version='2.0'><metadata xmlns:dc='http://purl.org/dc/elements/1.1/'><dc:title>${t}</dc:title></metadata><manifest><item id='content' href='content.xhtml' media-type='application/xhtml+xml'/></manifest><spine toc='ncx'><itemref idref='content'/></spine></package>`),o.generateAsync({type:"blob"}).then(function(e){const n=URL.createObjectURL(e),i=document.createElement("a");i.href=n,i.download=t+".epub",document.body.appendChild(i),i.click(),setTimeout(()=>{document.body.removeChild(i),URL.revokeObjectURL(n)},200)})}else alert("EPUB export requires JSZip.");else if("docx"===e)if(window.htmlDocx||"undefined"==typeof htmlDocx||(window.htmlDocx=htmlDocx),window.htmlDocx){const e=`<!doctype html><html><head><meta charset='utf-8'><title>${t}</title></head><body>${i.innerHTML}</body></html>`,n=window.htmlDocx.asBlob(e),o=URL.createObjectURL(n),l=document.createElement("a");l.href=o,l.download=t+".docx",document.body.appendChild(l),l.click(),setTimeout(()=>{document.body.removeChild(l),URL.revokeObjectURL(o)},200)}else alert("DOCX export requires html-docx-js.");D.style.display="none"}),o.addEventListener("click",()=>{window.close()}),applySettingsToContent(getSettings())});