var background=function(s){"use strict";var d,w;function f(){var a,i;const r=chrome.runtime.getManifest(),n=(a=r.oauth2)==null?void 0:a.client_id;if(!n)throw new Error("Invalid client ID");const e=new URL("https://accounts.google.com/o/oauth2/auth"),o=chrome.identity.getRedirectURL("oauth2"),t=((i=r.oauth2)==null?void 0:i.scopes)||["https://www.googleapis.com/auth/userinfo.email"];return e.searchParams.set("client_id",n),e.searchParams.set("redirect_uri",o),e.searchParams.set("response_type","token"),e.searchParams.set("scope",t.join(" ")),e.searchParams.set("prompt","select_account"),{url:e}}function k(r){return new Promise((n,e)=>{chrome.identity.launchWebAuthFlow({url:r,interactive:!0},o=>{if(chrome.runtime.lastError){e(chrome.runtime.lastError.message);return}n(o||"")})})}const c={name:"getCookies",handler:async(r,n)=>{const e=await chrome.cookies.get({url:"https://www.overleaf.com",name:"overleaf_session2"}),o=await chrome.cookies.get({url:"https://www.overleaf.com",name:"GCLB"});n({session:e==null?void 0:e.value,gclb:o==null?void 0:o.value})}},l={name:"revokeGoogleAuthToken",handler:async(r,n)=>{await chrome.identity.removeCachedAuthToken({token:r});const e=await fetch(`https://accounts.google.com/o/oauth2/revoke?token=${r}`);n(e.ok)}},h={name:"getGoogleAuthToken",handler:async(r,n)=>{const{url:e}=f(),o=await k(e.href);if(!o)throw new Error("Authentication failed: no redirect URL received");const t=new URLSearchParams(new URL(o).hash.substring(1)),a=t.get("access_token"),i=t.get("error");if(a)n({token:a});else throw i?new Error(`OAuth error: ${i}`):new Error("No access token found in redirect URL")}},u={name:"getManifest",handler:async(r,n)=>{n(chrome.runtime.getManifest())}},m={name:"getUrl",handler:async(r,n)=>{n(chrome.runtime.getURL(r))}},g={name:"getOrCreateSessionId",handler:async(r,n)=>{const o=Date.now();let{sessionData:t}=await chrome.storage.session.get("sessionData");t&&t.timestamp&&((o-t.timestamp)/6e4>30?t=null:(t.timestamp=o,await chrome.storage.session.set({sessionData:t}))),t||(t={session_id:o.toString(),timestamp:o.toString()},await chrome.storage.session.set({sessionData:t})),n(t.session_id)}};return(w=(d=chrome==null?void 0:chrome.runtime)==null?void 0:d.onMessage)==null||w.addListener((r,n,e)=>{const t=[c,h,l,u,m,g].find(a=>a.name===r.action);return t&&(async()=>{var a;try{await((a=t.handler)==null?void 0:a.call(t,r.args,e))}catch(i){e({error:i})}})(),!0}),s.getCookiesHandler=c,s.getGoogleAuthTokenHandler=h,s.getManifestHandler=u,s.getOrCreateSessionIdHandler=g,s.getUrlHandler=m,s.revokeGoogleAuthTokenHandler=l,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"}),s}({});
