"use strict";const e=document.getElementById("send"),t=document.getElementById("error"),n=document.getElementById("selectService"),o=document.getElementById("pleasewait"),l=document.getElementById("googletranslate"),s=document.getElementById("cannotusegoogle"),a=document.getElementById("cannotDownload"),c=document.getElementById("conversion"),r=document.getElementById("conversionalert");async function d(e){console.error(e),n.style.display="none",c.style.display="none",r.style.display="none",o.style.display="none",a.style.display="block"}async function i(e){return new Promise(((t,n)=>{try{const l=new XMLHttpRequest;l.open("get",e,!0),l.responseType="arraybuffer",l.onprogress=e=>{if(e.lengthComputable){const t=e.loaded/e.total*100;o.textContent=chrome.i18n.getMessage("msgPleaseWait")+" "+t.toFixed(1)+"%"}},l.onload=e=>{t(e.target.response)},l.onerror=e=>{d(e),n(e)},l.send()}catch(e){d(e)}}))}async function y(t,n){const l=new File([n],"document.pdf",{type:"application/pdf",lastModified:Date.now()}),s=new DataTransfer;s.items.add(l);const a=document.getElementById("form_"+t);a.querySelector('[type="file"]').files=s.files,a.querySelector('[name="tl"]')&&(a.querySelector('[name="tl"]').value=twpConfig.get("targetLanguage")),o.style.display="none",e.style.display="block",e.onclick=e=>{a.querySelector('[type="submit"]').click(),window.close()}}function g(){if(!$("#lightModeElement")){const e=document.createElement("style");e.setAttribute("id","lightModeElement"),e.setAttribute("rel","stylesheet"),e.textContent="\n        body {\n            color: rgb(0, 0, 0);\n            background-color: rgb(224, 224, 224);\n        }\n        .servicebutton, .sendbutton, .title {\n            background-color: rgba(0, 0, 0, 0.2);\n        }\n        \n        .servicebutton:hover, .sendbutton:hover {\n            background-color: rgba(0, 0, 0, 0.4);\n        }\n        ",document.head.appendChild(e)}}function m(){$("#lightModeElement")&&$("#lightModeElement").remove()}n.onclick=async e=>{"google"!=e.target.dataset.name&&"translatewebpages"!=e.target.dataset.name||(n.style.display="none",o.style.display="block",c.style.display="none",r.style.display="none",s.style.display="none",chrome.tabs.query({active:!0,currentWindow:!0},(async t=>{try{const a=e.target.dataset.name;if(t[0].url.startsWith("file:"))return"google"==a?chrome.tabs.create({url:"https://translate.google.com/?op=docs"}):chrome.tabs.create({url:"https://translatewebpages.org/"}),window.close();const d=await i(t[0].url);o.style.display="none",d.byteLength>10485760&&"google"==a?(c.style.display="block",r.style.display="block",l.style.display="none",n.style.display="block",s.textContent=chrome.i18n.getMessage("msgFileLargerThan","10 MB"),s.style.display="block"):y(a,d)}catch(e){d(e)}})))},$=document.querySelector.bind(document),twpConfig.onReady((()=>{switch(twpConfig.get("darkMode")){case"auto":matchMedia("(prefers-color-scheme: dark)").matches?m():g();break;case"yes":m();break;case"no":g()}}));