var background=(function(){function e(e){return e==null||typeof e==`function`?{main:e}:e}var t=globalThis.browser?.runtime?.id?globalThis.browser:globalThis.chrome,n=`ocr.locale.v1`,r=[`en`,`zh_CN`,`zh_TW`,`es`,`pt_BR`,`de`,`fr`,`it`,`nl`,`pl`,`ru`,`uk`,`tr`,`ja`,`ko`,`hi`,`id`,`th`,`vi`];async function i(){let e=(await t.storage.local.get(n))[n];return typeof e==`string`?e:``}function a(e,t,n){if(e&&n.includes(e))return e;let r=(t||`en`).replace(/-/g,`_`);if(n.includes(r))return r;let i=r.split(`_`)[0];return i===`zh`?/TW|HK|Hant/i.test(r)?`zh_TW`:`zh_CN`:n.find(e=>e.split(`_`)[0]===i)??`en`}async function o(e=r){let n=t.i18n?.getUILanguage?.()??`en`;return a(await i(),n,e)}var s={en:``,zh_CN:`zh-cn`,zh_TW:`zh-tw`,es:`es`,pt_BR:`pt-br`,de:`de`,fr:`fr`,it:`it`,nl:`nl`,pl:`pl`,ru:`ru`,uk:`uk`,tr:`tr`,ja:`ja`,ko:`ko`,hi:`hi`,id:`id`,th:`th`,vi:`vi`},c=`https://extensionsly.com`,l=`free-ocr-image-to-text`,u=`/bundle/`,d=`ext`;function f(e,t){let n=s[t]??``;return`${c}${n?`/`+n:``}${e}`}function p(e,{medium:t,content:n}){let r=new URL(e);return r.searchParams.set(`utm_source`,d),r.searchParams.set(`utm_medium`,t),r.searchParams.set(`utm_campaign`,l),n&&r.searchParams.set(`utm_content`,n),r.toString()}function m(e,t,n=u){return p(f(n,e),t)}var h={popupFooter:{medium:`footer`},optionsCard:{medium:`card`},welcome:{medium:`bundle`,content:`welcome`},uninstall:{medium:`bundle`,content:`uninstall`}};function g(e,t){return m(e,h[t])}var _={launch:`ocr:launch`,captureRegion:`ocr:capture-region`,openOptions:`ocr:open-options`},v=`/content-scripts/overlay.js`;async function y(e){await t.scripting.executeScript({target:{tabId:e},files:[v]}),await t.tabs.sendMessage(e,{type:_.launch})}function b(e){return e?/^https?:/.test(e)&&!e.startsWith(`https://chromewebstore.google.com`):!1}function x(e){let t=e.indexOf(`,`),n=atob(e.slice(t+1)),r=new Uint8Array(n.length);for(let e=0;e<n.length;e++)r[e]=n.charCodeAt(e);return r}function S(e,t=`image/png`){let n=``;for(let t=0;t<e.length;t++)n+=String.fromCharCode(e[t]);return`data:${t};base64,${btoa(n)}`}async function C(e,t,n){let r=await createImageBitmap(new Blob([x(e)],{type:`image/png`}));try{let e=Math.max(0,Math.round(t.x*n)),i=Math.max(0,Math.round(t.y*n)),a=Math.max(1,Math.min(Math.round(t.w*n),r.width-e)),o=Math.max(1,Math.min(Math.round(t.h*n),r.height-i)),s=new OffscreenCanvas(a,o),c=s.getContext(`2d`);if(!c)throw Error(`no 2d context`);c.drawImage(r,e,i,a,o,0,0,a,o);let l=await s.convertToBlob({type:`image/png`});return S(new Uint8Array(await l.arrayBuffer()))}finally{r.close()}}var w=`ocr.pendingCapture.v1`;function T(){return t.storage.session}async function E(e){await T().set({[w]:e})}var D=`ocr.errlog.v1`;async function O(e,n){try{let r=(n instanceof Error?n.message:String(n)).slice(0,300),i=[...(await t.storage.local.get(D))[D]??[],{t:new Date().toISOString(),where:e,msg:r}].slice(-25);await t.storage.local.set({[D]:i})}catch{}}async function k(){try{t.runtime.setUninstallURL(g(await o(),`uninstall`))}catch{}}async function A(){await t.tabs.create({url:t.runtime.getURL(`/workspace.html`)})}async function j(e,n){try{let r=await C(await t.tabs.captureVisibleTab({format:`png`}),e.rect,e.dpr),i;try{i=n?new URL(n).hostname:void 0}catch{}return await E({dataUrl:r,source:i}),await A(),{ok:!0}}catch(e){return await O(`background`,e),{ok:!1,error:String(e)}}}var M=e(()=>{k(),t.storage.onChanged.addListener((e,t)=>{t===`local`&&`ocr.locale.v1`in e&&k()}),t.runtime.onInstalled.addListener(e=>{e.reason===`install`&&t.tabs.create({url:t.runtime.getURL(`/onboarding.html`)}).catch(e=>O(`background`,e))}),t.commands.onCommand.addListener(e=>{e===`launch-capture`&&(async()=>{try{let[e]=await t.tabs.query({active:!0,currentWindow:!0});if(e?.id===void 0||!b(e.url)){await A();return}await y(e.id)}catch(e){await O(`background`,e)}})()}),t.runtime.onMessage.addListener((e,n,r)=>{let i=e;if(i?.type===_.captureRegion)return j(e,n.tab?.url).then(r),!0;i?.type===_.openOptions&&t.runtime.openOptionsPage().catch(e=>O(`background`,e))})}),N={debug:(...e)=>([...e],void 0),log:(...e)=>([...e],void 0),warn:(...e)=>([...e],void 0),error:(...e)=>([...e],void 0)},P;try{P=M.main(),P instanceof Promise&&console.warn(`The background's main() function return a promise, but it must be synchronous`)}catch(e){throw N.error(`The background crashed on startup!`),e}return P})();