-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.min.js
1 lines (1 loc) · 7.18 KB
/
popup.min.js
1
const mainDiv=get("main"),fontsStyle=get("wudoohCustomFontsStyle"),sizeSlider=get("size"),heightSlider=get("height"),onOffSwitch=get("onOffSwitch"),fontSelect=get("font-select"),overrideSiteSwitch=get("overrideSettingsSwitch"),whiteListSwitch=get("whitelistSwitch"),sizeValue=get("sizeValue"),heightValue=get("heightValue"),overrideSettingsValue=get("overrideSettingsLabel"),whitelistedValue=get("whitelistedLabel"),websiteText=get("website"),websiteIcon=get("websiteIcon"),exportButton=get("exportButton"),exportAnchor=get("exportAnchor"),importButton=get("importButton"),importInput=get("importInput");async function initializeUI(){const t=await sync.get(keys),e=await tabs.queryCurrentTab();(await injectCustomFonts(t.customFonts)).forEach(t=>{const e=t.fontName,i=document.createElement("option");i.style.fontFamily=e,i.value=e,i.textContent=e,fontSelect.add(i)}),onOffSwitch.checked=t.onOff,t.onOff?mainDiv.style.maxHeight="100%":mainDiv.style.maxHeight="0";const i=e[0],n=new URL(i.url).hostname,s=t.customSettings,o=t.whitelisted,a=s.find(t=>t.url===n),l=!!a;let r,u,c;l?(r=a.textSize,u=a.lineHeight,c=a.font):(r=t.textSize,u=t.lineHeight,c=t.font),sizeSlider.value=r.toString(),sizeValue.innerHTML=r.toString()+"%",heightSlider.value=u.toString(),heightValue.innerHTML=u.toString()+"%",fontSelect.value=c,fontSelect.style.fontFamily=c,websiteText.innerText=n,websiteText.title=n,i.favIconUrl?websiteIcon.src=i.favIconUrl:websiteIcon.style.display="none",websiteIcon.title=n,websiteIcon.alt=n;const g=!!o.find(t=>t===n);whiteListSwitch.checked=!g,whitelistedValue.innerText=g?"This site is whitelisted":"Running on this site",overrideSiteSwitch.checked=l,overrideSettingsValue.innerText=l?"Using site specific settings":"Using global settings"}async function updateAllTabsText(){(await tabs.queryAllTabs()).forEach(t=>tabs.sendMessage(t.id,{reason:reasonUpdateAllText}))}async function toggleOnOff(){if(await sync.set({onOff:onOffSwitch.checked}),onOffSwitch.checked)mainDiv.style.maxHeight="100%",updateAllTabsText();else{mainDiv.style.maxHeight="0",(await tabs.queryAllTabs()).forEach(t=>tabs.sendMessage(t.id,{reason:reasonToggleOff}))}}async function updateTextSize(){const t=parseInt(sizeSlider.value),e=await tabs.queryCurrentTab(),i=await sync.get([keyCustomSettings]),n=new URL(e[0].url).hostname,s=i.customSettings,o=s.find(t=>t.url===n);o?(o.textSize=t,s[s.indexOf(o)]=o,await sync.set({customSettings:s})):await sync.set({textSize:t}),updateAllTabsText()}async function updateLineHeight(){const t=parseInt(heightSlider.value),e=await tabs.queryCurrentTab(),i=await sync.get([keyCustomSettings]),n=new URL(e[0].url).hostname,s=i.customSettings,o=s.find(t=>t.url===n);o?(o.lineHeight=t,s[s.indexOf(o)]=o,await sync.set({customSettings:s})):await sync.set({lineHeight:t}),updateAllTabsText()}async function changeFont(){const t=fontSelect.value,e=await tabs.queryCurrentTab(),i=await sync.get([keyCustomSettings]),n=new URL(e[0].url).hostname,s=i.customSettings,o=s.find(t=>t.url===n);fontSelect.style.fontFamily=t,o?(o.font=t,s[s.indexOf(o)]=o,await sync.set({customSettings:s})):await sync.set({font:t}),updateAllTabsText()}async function toggleOverrideSiteSettings(){const t=await tabs.queryCurrentTab(),e=new URL(t[0].url).hostname;let i,n,s,o=await sync.get([keyCustomSettings]),a=o.customSettings;overrideSiteSwitch.checked?(a.push(new CustomSetting(e,parseInt(sizeSlider.value),parseInt(heightSlider.value),fontSelect.value)),overrideSettingsValue.textContent="Using site specific settings"):(a=a.filter(t=>t.url!==e),overrideSettingsValue.textContent="Using global settings"),await sync.set({customSettings:a});let l=(a=(o=await sync.get([keyTextSize,keyLineHeight,keyFont,keyCustomSettings])).customSettings).find(t=>t.url===e);l?(i=l.textSize,n=l.lineHeight,s=l.font):(i=o.textSize,n=o.lineHeight,s=o.font),sizeSlider.value=i.toString(),sizeValue.innerHTML=i.toString()+"%",heightSlider.value=n.toString(),heightValue.innerHTML=n.toString()+"%",fontSelect.value=s,fontSelect.style.fontFamily=s,updateAllTabsText()}async function toggleWhitelist(){const t=await tabs.queryCurrentTab(),e=new URL(t[0].url).hostname;let i=(await sync.get([keyWhitelisted])).whitelisted;whiteListSwitch.checked?(i=i.filter(t=>t!=e),whitelistedValue.textContent="Running on this site"):(i.push(e),whitelistedValue.textContent="This site is whitelisted"),await sync.set({whitelisted:i}),updateAllTabsText()}async function exportSettings(){const t=await sync.get(keys),e=JSON.stringify(t,null,4);exportAnchor.href="data:application/octet-stream,"+encodeURIComponent(e),exportAnchor.download="wudooh.settings.json",exportAnchor.click()}async function importSettings(){const t=importInput.files[0],e=new FileReader;e.onload=(async t=>{const e=t.target.result;let i;try{i=JSON.parse(e)}catch(t){if(t instanceof SyntaxError)return void alert("Import Failed!\n\nMalformed JSON\n\nEnsure settings file contains valid JSON")}const n=[],s=i[keyTextSize],o=i[keyLineHeight],a=i[keyOnOff],l=i[keyFont],r=i[keyWhitelisted],u=i[keyCustomSettings],c=i[keyCustomFonts];null===s?n.push('Field "textSize" is missing! It must be a number between 100 and 300'):("number"!=typeof s||s<100||s>300)&&n.push('Field "textSize" must be a number between 100 and 300'),null===o?n.push('Field "lineHeight" is missing! It must be a number between 100 and 300'):("number"!=typeof o||o<100||o>300)&&n.push('Field "lineHeight" must be a number between 100 and 300'),null===a?n.push('Field "onOff" is missing! It must be a boolean'):"boolean"!=typeof a&&n.push('Field "onOff" must be a boolean'),null===l?n.push('Field "font" is missing! It must be a string'):"string"!=typeof l&&n.push('Field "font" must be a string'),null===r?n.push('Field "whitelisted" is missing! It must be an array of strings'):(!Array.isArray(r)||r.length>0&&"string"!=typeof r[0])&&n.push('Field "whitelisted" must be an array of strings'),null===u?n.push('Field "customSettings" is missing! It must be an array of CustomSetting objects'):Array.isArray(u)&&CustomSetting.isCustomSettingsArray(u)||n.push('Field "customSettings" must be an array of CustomSetting objects'),null===c?n.push('Field "customFonts" is missing! It must be an array of CustomFont objects'):Array.isArray(c)&&CustomFont.isCustomFontsArray(c)||n.push('Field "customFonts" must be an array of CustomFont objects'),n.length>0?alert("Import Failed!\n\n"+n.join("\n")+"\n\nClick Help to find the guides at the extension website"):(await sync.set({textSize:s,lineHeight:o,onOff:a,font:l,whitelisted:r,customSettings:u,customFonts:c}),alert("Imported settings successfully!"),initializeUI())}),e.readAsText(t),importInput.value=null}function popupAddListeners(){document.addEventListener("DOMContentLoaded",initializeUI),onOffSwitch.onclick=(()=>toggleOnOff()),fontSelect.oninput=(()=>changeFont()),sizeSlider.oninput=(()=>{sizeValue.textContent=sizeSlider.value+"%",sizeSlider.postDelayed(defaultDelay,updateTextSize)}),heightSlider.oninput=(()=>{heightValue.textContent=heightSlider.value+"%",heightSlider.postDelayed(defaultDelay,updateLineHeight)}),whiteListSwitch.onclick=(()=>toggleWhitelist()),overrideSiteSwitch.onclick=(()=>toggleOverrideSiteSettings()),exportButton.onclick=(()=>exportSettings()),importInput.oninput=(()=>importSettings()),importButton.onclick=(()=>importInput.click())}analytics("/popup.html"),popupAddListeners();