-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
1 lines (1 loc) · 3.6 KB
/
background.js
File metadata and controls
1 lines (1 loc) · 3.6 KB
1
(()=>{"use strict";const e={exaKey:"",openaiKey:"",highlightsEnabled:!0,sidebarEnabled:!1,darkMode:!1,excludedDomains:[],maxVerificationsPerDay:10,enableCaching:!0,cacheDuration:7,useLLMExtraction:!0,usageCount:0,lastUsageReset:Date.now()};async function t(){return new Promise((t=>{chrome.storage.local.get(e,(e=>{t(e)}))}))}async function a(t){return new Promise((a=>{chrome.storage.local.get(t,(n=>{a(void 0!==n[t]?n[t]:e[t])}))}))}async function n(e){return new Promise((t=>{chrome.storage.local.set(e,(()=>{t()}))}))}async function s(){const e=await a("lastUsageReset");e&&new Date(e).toDateString()!==(new Date).toDateString()&&(console.log("New day detected, resetting usage counter"),await n({usageCount:0,lastUsageReset:Date.now()}))}async function o(){try{const e=await new Promise((e=>{chrome.storage.local.get(null,(t=>{e(t)}))})),{cacheDuration:t=7}=e,a=Object.keys(e).filter((e=>e.startsWith("cache:"))),n=[],s=Date.now(),o=24*t*60*60*1e3;for(const t of a){const a=e[t];a&&a.timestamp&&s-a.timestamp>o&&n.push(t)}n.length>0&&(console.log(`Removing ${n.length} expired cache entries`),await new Promise((e=>{chrome.storage.local.remove(n,(()=>{e()}))})))}catch(e){console.error("Error cleaning up caches:",e)}}console.log("Background service worker starting..."),chrome.runtime.onInstalled.addListener((()=>{console.log("Extension installed"),async function(){const t=await new Promise((t=>{chrome.storage.local.get(Object.keys(e),(e=>{t(e)}))})),a={};for(const[n,s]of Object.entries(e))void 0===t[n]&&(a[n]=s);Object.keys(a).length>0&&await n(a)}()})),chrome.runtime.onMessage.addListener(((e,o,r)=>"VERIFY_CLAIM"===e.type?(async function(e){try{await s();const o=await t(),{exaKey:r}=o;if(!r)throw new Error("Exa API key not found. Please set it in the extension options.");if(await async function(){const e=await t();return e.maxVerificationsPerDay>0&&e.usageCount>=e.maxVerificationsPerDay}()){const e=o.maxVerificationsPerDay;throw new Error(`Daily verification limit (${e}) reached. Please try again tomorrow.`)}const i=await async function(e){const a=await t();if(!a.enableCaching)return null;const n=`cache:${e}`;return new Promise((e=>{chrome.storage.local.get([n],(t=>{if(t&&t[n]){const s=t[n];Date.now()-s.timestamp<24*a.cacheDuration*60*60*1e3?(console.log("Returning cached result for claim"),e(s.results)):(console.log("Cache expired, fetching fresh results"),e(null))}else e(null)}))}))}(e);if(i)return i;const c=await fetch("https://api.exa.ai/search",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify({query:e,numResults:3,type:"neural"})});if(!c.ok)throw new Error(`Exa API error: ${c.status}`);const l=(await c.json()).results;return await async function(){await s();const e=(await a("usageCount")||0)+1;return await n({usageCount:e}),e}(),await async function(e,a){if(!(await t()).enableCaching)return;const n=`cache:${e}`,s={timestamp:Date.now(),results:a};return new Promise((e=>{chrome.storage.local.set({[n]:s},(()=>{e()}))}))}(e,l),l}catch(e){throw console.error("Error verifying claim:",e),e}}(e.claim.text).then((e=>{console.log("Verification results:",e),r({success:!0,results:e})})).catch((e=>{e.message&&e.message.includes("Daily verification limit")?(console.warn("Daily limit reached:",e.message),r({success:!1,error:"DAILY_LIMIT_REACHED",message:e.message,results:[]})):(console.error("Verification failed:",e),r({success:!1,error:"API_ERROR",results:[]}))})),!0):"GET_USAGE_STATS"!==e.type||(t().then((e=>{r({success:!0,usageCount:e.usageCount||0,maxVerificationsPerDay:e.maxVerificationsPerDay||10,lastUsageReset:e.lastUsageReset||Date.now()})})),!0))),setInterval(o,864e5),o()})();