chrome.runtime.onMessage.addListener((c,b)=>{chrome.storage.local.get(["blacklist","whitelist","cache","showpopup"]).then(a=>{a.cache||(a.cache={});var d=a.blacklist||[],f=a.whitelist||[];Array.isArray(f)&&!f.some(e=>"object"===typeof e&&null!==e)&&(f=f.map(e=>({domain:e,limit:10})),chrome.storage.local.set({whitelist:f},function(){}));let g=!1;const h=(new URL(c.website)).hostname;var k=!1;if(d.some(e=>{e=(new URL(e)).hostname;return h.endsWith(e)}))g=!0,chrome.action.setPopup({tabId:b.tab.id,popup:"popup.html#settings"});
else{if(d=f.find(e=>{console.log(e);e=(new URL(e.domain)).hostname;return h.endsWith(e)}))""!=d.limit&&c.time>d.limit&&(chrome.action.setBadgeBackgroundColor({color:"red"}),chrome.action.setBadgeText({text:c.time.toString(),tabId:b.tab.id}),chrome.action.setPopup({tabId:b.tab.id,popup:"popup.html"}),k=!0,chrome.action.setBadgeTextColor({color:"white"}),1==a.showpopup&&chrome.tabs.sendMessage(b.tab.id,{action:"showlimit",limit:d.limit,requesttime:c.time},e=>{}));else if(0<f.length){g=!0;chrome.action.setPopup({tabId:b.tab.id,
popup:"popup.html#settings"});return}g||(a.cache["tab"+b.tab.id]=c.timing,chrome.storage.local.set(a).then(()=>{chrome.action.setBadgeText({text:c.time,tabId:b.tab.id});chrome.action.setPopup({tabId:b.tab.id,popup:"popup.html"});k||chrome.action.setBadgeBackgroundColor({color:"#4287f4"});chrome.action.setBadgeTextColor({color:"white"});updateOrAddEntry(c.time,c.website)}))}})});chrome.tabs.onRemoved.addListener(c=>{chrome.storage.local.get("cache").then(b=>{b.cache&&delete b.cache["tab"+c];chrome.storage.local.set(b)})});
function updateOrAddEntry(c,b){chrome.storage.local.get(["websiteTimes","dublicate"],function(a){let d=a.websiteTimes||[];const f=Math.floor(Date.now()/1E3),g=d.findIndex(h=>h.website===b);a.dublicate||-1===g?d.push({time:c,website:b,timestamp:f}):(d[g].time=c,d[g].timestamp=f);100<d.length&&d.shift();chrome.storage.local.set({websiteTimes:d},function(){console.log("Website times updated or added successfully.")})})}const newAPiUrl="https://selectorshub.info/nodeapp/api/",EXT_SLUG="PLT";
var browserType=chrome;let platform="chrome",isFirefox="undefined"!==typeof InstallTrigger,isEdge=-1<navigator.userAgent.indexOf("Edg/")?!0:!1,isChrome=!!chrome&&(!!chrome.webstore||!!chrome.runtime),isOpera=0<=navigator.userAgent.indexOf(" OPR/");isChrome&&(platform="chrome");isFirefox&&(platform="firefox");isEdge&&(platform="edge");isOpera&&(platform="opera");
const installedListener=async c=>{let b="";try{if("install"===c.reason){chrome.storage.local.set({dublicate:!1});b="on_install";const a=await (await fetch(`${newAPiUrl}link-ads?type=${b}&extension=${EXT_SLUG}&browsers=${platform}`)).json();Array.isArray(a[b])&&a[b].forEach(d=>openLink(d));Array.isArray(a.on_uninstall)&&0<a.on_uninstall.length&&browserType.runtime.setUninstallURL(a.on_uninstall[0])}else"update"===c.reason&&browserType.storage.local.get(["ttlt"],async a=>{if("undefined"==typeof a.ttlt||
"undefined"==a.ttlt||null==a.ttlt||""==a.ttlt)b="on_update",a=await (await fetch(`${newAPiUrl}link-ads?type=${b}&extension=${EXT_SLUG}&browsers=${platform}`)).json(),Array.isArray(a[b])&&a[b].forEach(d=>openLink(d)),Array.isArray(a.on_uninstall)&&0<a.on_uninstall.length&&browserType.runtime.setUninstallURL(a.on_uninstall[0])})}catch(a){console.error(`Error during ${b} fetch:`,a)}};function openLink(c){chrome.tabs.create({url:c})}browserType.runtime.onInstalled.addListener(installedListener);
