From 461331bace8ef7e3beb8c0434ede69dc9f0f3b3c Mon Sep 17 00:00:00 2001 From: Admin Date: Mon, 18 Aug 2025 09:21:01 +0700 Subject: [PATCH] update unlist when delete --- .../auto-listing-facebook-marketplace/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auto-listing-facebook-marketplace/auto-listing-facebook-marketplace/background.js b/auto-listing-facebook-marketplace/auto-listing-facebook-marketplace/background.js index d86df64..0cc2929 100644 --- a/auto-listing-facebook-marketplace/auto-listing-facebook-marketplace/background.js +++ b/auto-listing-facebook-marketplace/auto-listing-facebook-marketplace/background.js @@ -1 +1 @@ -function N(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var E={exports:{}},L;function O(){return L||(L=1,function(r){var e=Object.prototype.hasOwnProperty,t="~";function o(){}Object.create&&(o.prototype=Object.create(null),new o().__proto__||(t=!1));function a(l,n,s){this.fn=l,this.context=n,this.once=s||!1}function c(l,n,s,i,p){if(typeof s!="function")throw new TypeError("The listener must be a function");var d=new a(s,i||l,p),m=t?t+n:n;return l._events[m]?l._events[m].fn?l._events[m]=[l._events[m],d]:l._events[m].push(d):(l._events[m]=d,l._eventsCount++),l}function f(l,n){--l._eventsCount===0?l._events=new o:delete l._events[n]}function h(){this._events=new o,this._eventsCount=0}h.prototype.eventNames=function(){var n=[],s,i;if(this._eventsCount===0)return n;for(i in s=this._events)e.call(s,i)&&n.push(t?i.slice(1):i);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(s)):n},h.prototype.listeners=function(n){var s=t?t+n:n,i=this._events[s];if(!i)return[];if(i.fn)return[i.fn];for(var p=0,d=i.length,m=new Array(d);pglobalThis.DOMException===void 0?new $(r):new DOMException(r),P=r=>{const e=r.reason===void 0?S("This operation was aborted."):r.reason;return e instanceof Error?e:S(e)};function k(r,e){const{milliseconds:t,fallback:o,message:a,customTimers:c={setTimeout,clearTimeout}}=e;let f,h;const n=new Promise((s,i)=>{if(typeof t!="number"||Math.sign(t)!==1)throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${t}\``);if(e.signal){const{signal:d}=e;d.aborted&&i(P(d)),h=()=>{i(P(d))},d.addEventListener("abort",h,{once:!0})}if(t===Number.POSITIVE_INFINITY){r.then(s,i);return}const p=new C;f=c.setTimeout.call(void 0,()=>{if(o){try{s(o())}catch(d){i(d)}return}typeof r.cancel=="function"&&r.cancel(),a===!1?s():a instanceof Error?i(a):(p.message=a??`Promise timed out after ${t} milliseconds`,i(p))},t),(async()=>{try{s(await r)}catch(d){i(d)}})()}).finally(()=>{n.clear(),h&&e.signal&&e.signal.removeEventListener("abort",h)});return n.clear=()=>{c.clearTimeout.call(void 0,f),f=void 0},n}function D(r,e,t){let o=0,a=r.length;for(;a>0;){const c=Math.trunc(a/2);let f=o+c;t(r[f],e)<=0?(o=++f,a-=c+1):a=c}return o}class M{#e=[];enqueue(e,t){t={priority:0,...t};const o={priority:t.priority,id:t.id,run:e};if(this.size===0||this.#e[this.size-1].priority>=t.priority){this.#e.push(o);return}const a=D(this.#e,o,(c,f)=>f.priority-c.priority);this.#e.splice(a,0,o)}setPriority(e,t){const o=this.#e.findIndex(c=>c.id===e);if(o===-1)throw new ReferenceError(`No promise function with the id "${e}" exists in the queue.`);const[a]=this.#e.splice(o,1);this.enqueue(a.run,{priority:t,id:e})}dequeue(){return this.#e.shift()?.run}filter(e){return this.#e.filter(t=>t.priority===e.priority).map(t=>t.run)}get size(){return this.#e.length}}class z extends q{#e;#o;#i=0;#d;#a;#m=0;#r;#c;#t;#v;#n=0;#u;#s;#p;#b=1n;timeout;constructor(e){if(super(),e={carryoverConcurrencyCount:!1,intervalCap:Number.POSITIVE_INFINITY,interval:0,concurrency:Number.POSITIVE_INFINITY,autoStart:!0,queueClass:M,...e},!(typeof e.intervalCap=="number"&&e.intervalCap>=1))throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${e.intervalCap?.toString()??""}\` (${typeof e.intervalCap})`);if(e.interval===void 0||!(Number.isFinite(e.interval)&&e.interval>=0))throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${e.interval?.toString()??""}\` (${typeof e.interval})`);this.#e=e.carryoverConcurrencyCount,this.#o=e.intervalCap===Number.POSITIVE_INFINITY||e.interval===0,this.#d=e.intervalCap,this.#a=e.interval,this.#t=new e.queueClass,this.#v=e.queueClass,this.concurrency=e.concurrency,this.timeout=e.timeout,this.#p=e.throwOnTimeout===!0,this.#s=e.autoStart===!1}get#g(){return this.#o||this.#i{this.#I()},t)),!0}return!1}#l(){if(this.#t.size===0)return this.#r&&clearInterval(this.#r),this.#r=void 0,this.emit("empty"),this.#n===0&&this.emit("idle"),!1;if(!this.#s){const e=!this.#_;if(this.#g&&this.#E){const t=this.#t.dequeue();return t?(this.emit("active"),t(),e&&this.#y(),!0):!1}}return!1}#y(){this.#o||this.#r!==void 0||(this.#r=setInterval(()=>{this.#w()},this.#a),this.#m=Date.now()+this.#a)}#w(){this.#i===0&&this.#n===0&&this.#r&&(clearInterval(this.#r),this.#r=void 0),this.#i=this.#e?this.#n:0,this.#h()}#h(){for(;this.#l(););}get concurrency(){return this.#u}set concurrency(e){if(!(typeof e=="number"&&e>=1))throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${e}\` (${typeof e})`);this.#u=e,this.#h()}async#L(e){return new Promise((t,o)=>{e.addEventListener("abort",()=>{o(e.reason)},{once:!0})})}setPriority(e,t){this.#t.setPriority(e,t)}async add(e,t={}){return t.id??=(this.#b++).toString(),t={timeout:this.timeout,throwOnTimeout:this.#p,...t},new Promise((o,a)=>{this.#t.enqueue(async()=>{this.#n++,this.#i++;try{t.signal?.throwIfAborted();let c=e({signal:t.signal});t.timeout&&(c=k(Promise.resolve(c),{milliseconds:t.timeout})),t.signal&&(c=Promise.race([c,this.#L(t.signal)]));const f=await c;o(f),this.emit("completed",f)}catch(c){if(c instanceof C&&!t.throwOnTimeout){o();return}a(c),this.emit("error",c)}finally{this.#T()}},t),this.emit("add"),this.#l()})}async addAll(e,t){return Promise.all(e.map(async o=>this.add(o,t)))}start(){return this.#s?(this.#s=!1,this.#h(),this):this}pause(){this.#s=!0}clear(){this.#t=new this.#v}async onEmpty(){this.#t.size!==0&&await this.#f("empty")}async onSizeLessThan(e){this.#t.sizethis.#t.size{const a=()=>{t&&!t()||(this.off(e,a),o())};this.on(e,a)})}get size(){return this.#t.size}sizeBy(e){return this.#t.filter(e).length}get pending(){return this.#n}get isPaused(){return this.#s}}let T=[];const I=new z({concurrency:1});chrome.runtime.onConnect.addListener(r=>{T.push(r),r.onDisconnect.addListener(()=>{T=T.filter(e=>e!==r)})});async function R(r,e=5*60*1e3){return new Promise(t=>{chrome.tabs.create({url:"https://www.facebook.com/marketplace/create/item",active:!0},o=>{if(!o?.id)return t();const a=o.id;let c=!1;const f=()=>{c||(c=!0,chrome.runtime.onConnect.removeListener(h),chrome.tabs.onRemoved.removeListener(l),clearTimeout(n),t(),_())},h=s=>{s.sender?.tab?.id===a&&(s.postMessage({type:"PUBLIST_EVENT",payload:r}),chrome.runtime.onConnect.removeListener(h))};chrome.runtime.onConnect.addListener(h);const l=s=>{s===a&&f()};chrome.tabs.onRemoved.addListener(l);const n=setTimeout(()=>{console.warn(`Tab ${a} timeout (${e}ms) - auto resolve`),a&&chrome.tabs.remove(a),f()},e)})})}async function U(r,e=5*60*1e3){return new Promise((t,o)=>{(async()=>{try{if(!r.prev?.publist_id){const a=await F({type:"GET_PUBLIST_ID",data:r},3e4);console.log({response:a}),r.prev.publist_id=a.publist_id}chrome.tabs.create({url:`https://www.facebook.com/marketplace/edit/?listing_id=${r.prev.publist_id}`,active:!0},a=>{a?.id||t(r);const c=a.id;let f=!1;const h=()=>{f||(f=!0,chrome.runtime.onConnect.removeListener(l),chrome.tabs.onRemoved.removeListener(n),clearTimeout(s),t(r),_())},l=i=>{i.sender?.tab?.id===c&&(i.postMessage({type:"EDIT_EVENT",payload:r}),chrome.runtime.onConnect.removeListener(l))};chrome.runtime.onConnect.addListener(l);const n=i=>{i===c&&h()};chrome.tabs.onRemoved.addListener(n);const s=setTimeout(()=>{console.warn(`Tab ${c} timeout (${e}ms) - auto resolve`),c&&chrome.tabs.remove(c),h()},e)})}catch(a){o(a)}})()})}const B=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/publist-stream");r.onmessage=e=>{const t=JSON.parse(e.data);console.log("[PUBLIST] New event:",t),I.add(()=>R(t,3e4))},r.onerror=e=>{console.error("EventSource failed:",e)}};function F(r,e=1e4){return new Promise((t,o)=>{chrome.tabs.query({url:"*://www.facebook.com/marketplace/you/selling*"},a=>{if(a.length>0&&a[0].id!==void 0){const c=a[0].id,f=n=>{n?.type==="GET_PUBLIST_ID_DONE"&&(h(),t(n))},h=()=>{chrome.runtime.onMessage.removeListener(f),clearTimeout(l)};chrome.runtime.onMessage.addListener(f);const l=setTimeout(()=>{h(),o(new Error("Timeout chờ phản hồi từ content script"))},e);chrome.tabs.sendMessage(c,r)}else o(new Error("Không tìm thấy tab Selling"))})})}const V=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/delete-stream");r.onmessage=e=>{try{const t=JSON.parse(e.data);console.log("[SSE] New event:",t),I.add(()=>new Promise(o=>{const a=c=>{c?.type==="delete-done"&&(console.log("[QUEUE] Delete done signal received."),chrome.runtime.onMessage.removeListener(a),o())};chrome.runtime.onMessage.addListener(a),chrome.tabs.query({url:"https://www.facebook.com/marketplace/you/selling"},c=>{for(const f of c)f.id&&chrome.tabs.sendMessage(f.id,{type:"DELETE_STREAM_DATA",payload:t})})}))}catch(t){console.error("[SSE] Parse error:",t)}},r.onerror=e=>{console.error("[SSE] Connection failed:",e)},console.log("[SSE] Listening for delete-stream events...")},Q=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/edit-stream");r.onmessage=e=>{const t=JSON.parse(e.data);console.log("[EDIT] New event:",t),I.add(()=>U(t,3e4))},r.onerror=e=>{console.error("EventSource failed:",e)}};function _(){const r="https://www.facebook.com/marketplace/you/selling";console.log({targetUrl:r}),chrome.tabs.query({},e=>{const t=e.find(o=>o.url&&o.url.startsWith(r));t?chrome.tabs.reload(t.id):chrome.tabs.create({url:r})})}const Y=async()=>{B(),V(),Q(),_()};chrome.runtime.onMessage.addListener((r,e)=>{if(r.type==="close-tab"){const t=e.tab?.id;t&&chrome.tabs.remove(t)}});Y(); +function k(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var T={exports:{}},L;function A(){return L||(L=1,function(r){var e=Object.prototype.hasOwnProperty,t="~";function o(){}Object.create&&(o.prototype=Object.create(null),new o().__proto__||(t=!1));function a(l,n,s){this.fn=l,this.context=n,this.once=s||!1}function c(l,n,s,i,p){if(typeof s!="function")throw new TypeError("The listener must be a function");var d=new a(s,i||l,p),m=t?t+n:n;return l._events[m]?l._events[m].fn?l._events[m]=[l._events[m],d]:l._events[m].push(d):(l._events[m]=d,l._eventsCount++),l}function f(l,n){--l._eventsCount===0?l._events=new o:delete l._events[n]}function h(){this._events=new o,this._eventsCount=0}h.prototype.eventNames=function(){var n=[],s,i;if(this._eventsCount===0)return n;for(i in s=this._events)e.call(s,i)&&n.push(t?i.slice(1):i);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(s)):n},h.prototype.listeners=function(n){var s=t?t+n:n,i=this._events[s];if(!i)return[];if(i.fn)return[i.fn];for(var p=0,d=i.length,m=new Array(d);pglobalThis.DOMException===void 0?new $(r):new DOMException(r),P=r=>{const e=r.reason===void 0?S("This operation was aborted."):r.reason;return e instanceof Error?e:S(e)};function D(r,e){const{milliseconds:t,fallback:o,message:a,customTimers:c={setTimeout,clearTimeout}}=e;let f,h;const n=new Promise((s,i)=>{if(typeof t!="number"||Math.sign(t)!==1)throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${t}\``);if(e.signal){const{signal:d}=e;d.aborted&&i(P(d)),h=()=>{i(P(d))},d.addEventListener("abort",h,{once:!0})}if(t===Number.POSITIVE_INFINITY){r.then(s,i);return}const p=new C;f=c.setTimeout.call(void 0,()=>{if(o){try{s(o())}catch(d){i(d)}return}typeof r.cancel=="function"&&r.cancel(),a===!1?s():a instanceof Error?i(a):(p.message=a??`Promise timed out after ${t} milliseconds`,i(p))},t),(async()=>{try{s(await r)}catch(d){i(d)}})()}).finally(()=>{n.clear(),h&&e.signal&&e.signal.removeEventListener("abort",h)});return n.clear=()=>{c.clearTimeout.call(void 0,f),f=void 0},n}function z(r,e,t){let o=0,a=r.length;for(;a>0;){const c=Math.trunc(a/2);let f=o+c;t(r[f],e)<=0?(o=++f,a-=c+1):a=c}return o}class F{#e=[];enqueue(e,t){t={priority:0,...t};const o={priority:t.priority,id:t.id,run:e};if(this.size===0||this.#e[this.size-1].priority>=t.priority){this.#e.push(o);return}const a=z(this.#e,o,(c,f)=>f.priority-c.priority);this.#e.splice(a,0,o)}setPriority(e,t){const o=this.#e.findIndex(c=>c.id===e);if(o===-1)throw new ReferenceError(`No promise function with the id "${e}" exists in the queue.`);const[a]=this.#e.splice(o,1);this.enqueue(a.run,{priority:t,id:e})}dequeue(){return this.#e.shift()?.run}filter(e){return this.#e.filter(t=>t.priority===e.priority).map(t=>t.run)}get size(){return this.#e.length}}class U extends M{#e;#o;#i=0;#d;#a;#m=0;#r;#c;#t;#v;#n=0;#u;#s;#p;#b=1n;timeout;constructor(e){if(super(),e={carryoverConcurrencyCount:!1,intervalCap:Number.POSITIVE_INFINITY,interval:0,concurrency:Number.POSITIVE_INFINITY,autoStart:!0,queueClass:F,...e},!(typeof e.intervalCap=="number"&&e.intervalCap>=1))throw new TypeError(`Expected \`intervalCap\` to be a number from 1 and up, got \`${e.intervalCap?.toString()??""}\` (${typeof e.intervalCap})`);if(e.interval===void 0||!(Number.isFinite(e.interval)&&e.interval>=0))throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${e.interval?.toString()??""}\` (${typeof e.interval})`);this.#e=e.carryoverConcurrencyCount,this.#o=e.intervalCap===Number.POSITIVE_INFINITY||e.interval===0,this.#d=e.intervalCap,this.#a=e.interval,this.#t=new e.queueClass,this.#v=e.queueClass,this.concurrency=e.concurrency,this.timeout=e.timeout,this.#p=e.throwOnTimeout===!0,this.#s=e.autoStart===!1}get#w(){return this.#o||this.#i{this.#I()},t)),!0}return!1}#l(){if(this.#t.size===0)return this.#r&&clearInterval(this.#r),this.#r=void 0,this.emit("empty"),this.#n===0&&this.emit("idle"),!1;if(!this.#s){const e=!this.#_;if(this.#w&&this.#E){const t=this.#t.dequeue();return t?(this.emit("active"),t(),e&&this.#y(),!0):!1}}return!1}#y(){this.#o||this.#r!==void 0||(this.#r=setInterval(()=>{this.#g()},this.#a),this.#m=Date.now()+this.#a)}#g(){this.#i===0&&this.#n===0&&this.#r&&(clearInterval(this.#r),this.#r=void 0),this.#i=this.#e?this.#n:0,this.#h()}#h(){for(;this.#l(););}get concurrency(){return this.#u}set concurrency(e){if(!(typeof e=="number"&&e>=1))throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${e}\` (${typeof e})`);this.#u=e,this.#h()}async#L(e){return new Promise((t,o)=>{e.addEventListener("abort",()=>{o(e.reason)},{once:!0})})}setPriority(e,t){this.#t.setPriority(e,t)}async add(e,t={}){return t.id??=(this.#b++).toString(),t={timeout:this.timeout,throwOnTimeout:this.#p,...t},new Promise((o,a)=>{this.#t.enqueue(async()=>{this.#n++,this.#i++;try{t.signal?.throwIfAborted();let c=e({signal:t.signal});t.timeout&&(c=D(Promise.resolve(c),{milliseconds:t.timeout})),t.signal&&(c=Promise.race([c,this.#L(t.signal)]));const f=await c;o(f),this.emit("completed",f)}catch(c){if(c instanceof C&&!t.throwOnTimeout){o();return}a(c),this.emit("error",c)}finally{this.#T()}},t),this.emit("add"),this.#l()})}async addAll(e,t){return Promise.all(e.map(async o=>this.add(o,t)))}start(){return this.#s?(this.#s=!1,this.#h(),this):this}pause(){this.#s=!0}clear(){this.#t=new this.#v}async onEmpty(){this.#t.size!==0&&await this.#f("empty")}async onSizeLessThan(e){this.#t.sizethis.#t.size{const a=()=>{t&&!t()||(this.off(e,a),o())};this.on(e,a)})}get size(){return this.#t.size}sizeBy(e){return this.#t.filter(e).length}get pending(){return this.#n}get isPaused(){return this.#s}}let I=[];const w=new U({concurrency:1}),x="https://www.facebook.com/marketplace/you/selling";chrome.runtime.onConnect.addListener(r=>{I.push(r),r.onDisconnect.addListener(()=>{I=I.filter(e=>e!==r)})});async function R(r,e=5*60*1e3){return new Promise(t=>{chrome.tabs.create({url:"https://www.facebook.com/marketplace/create/item",active:!0},o=>{if(!o?.id)return t();const a=o.id;let c=!1;const f=()=>{c||(c=!0,chrome.runtime.onConnect.removeListener(h),chrome.tabs.onRemoved.removeListener(l),clearTimeout(n),t(),_())},h=s=>{s.sender?.tab?.id===a&&(s.postMessage({type:"PUBLIST_EVENT",payload:r}),chrome.runtime.onConnect.removeListener(h))};chrome.runtime.onConnect.addListener(h);const l=s=>{s===a&&f()};chrome.tabs.onRemoved.addListener(l);const n=setTimeout(()=>{console.warn(`Tab ${a} timeout (${e}ms) - auto resolve`),a&&chrome.tabs.remove(a),f()},e)})})}async function Q(r,e=5*60*1e3){return new Promise((t,o)=>{(async()=>{try{if(!r.prev?.publist_id){const a=await V({type:"GET_PUBLIST_ID",data:r},3e4);console.log({response:a}),r.prev.publist_id=a.publist_id}chrome.tabs.create({url:`https://www.facebook.com/marketplace/edit/?listing_id=${r.prev.publist_id}`,active:!0},a=>{a?.id||t(r);const c=a.id;let f=!1;const h=()=>{f||(f=!0,chrome.runtime.onConnect.removeListener(l),chrome.tabs.onRemoved.removeListener(n),clearTimeout(s),t(r),_())},l=i=>{i.sender?.tab?.id===c&&(i.postMessage({type:"EDIT_EVENT",payload:r}),chrome.runtime.onConnect.removeListener(l))};chrome.runtime.onConnect.addListener(l);const n=i=>{i===c&&h()};chrome.tabs.onRemoved.addListener(n);const s=setTimeout(()=>{console.warn(`Tab ${c} timeout (${e}ms) - auto resolve`),c&&chrome.tabs.remove(c),h()},e)})}catch(a){o(a)}})()})}const B=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/publist-stream");r.onmessage=e=>{const t=JSON.parse(e.data);console.log("[PUBLIST] New event:",t),w.add(()=>R(t,3e4))},r.onerror=e=>{console.error("EventSource failed:",e)}};function V(r,e=1e4){return new Promise((t,o)=>{chrome.tabs.query({url:"*://www.facebook.com/marketplace/you/selling*"},a=>{if(a.length>0&&a[0].id!==void 0){const c=a[0].id,f=n=>{n?.type==="GET_PUBLIST_ID_DONE"&&(h(),t(n))},h=()=>{chrome.runtime.onMessage.removeListener(f),clearTimeout(l)};chrome.runtime.onMessage.addListener(f);const l=setTimeout(()=>{h(),o(new Error("Timeout chờ phản hồi từ content script"))},e);chrome.tabs.sendMessage(c,r)}else o(new Error("Không tìm thấy tab Selling"))})})}const Y=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/delete-stream");r.onmessage=e=>{try{const t=JSON.parse(e.data);console.log("[SSE] New event:",t),w.add(()=>new Promise(o=>{const a=c=>{c?.type==="delete-done"&&(console.log("[QUEUE] Delete done signal received."),chrome.runtime.onMessage.removeListener(a),o())};chrome.runtime.onMessage.addListener(a),chrome.tabs.query({url:"https://www.facebook.com/marketplace/you/selling"},c=>{for(const f of c)f.id&&chrome.tabs.sendMessage(f.id,{type:"DELETE_STREAM_DATA",payload:t})})}))}catch(t){console.error("[SSE] Parse error:",t)}},r.onerror=e=>{console.error("[SSE] Connection failed:",e)},console.log("[SSE] Listening for delete-stream events...")},J=()=>{const r=new EventSource("http://localhost:4000/api/v1/products/edit-stream");r.onmessage=e=>{const t=JSON.parse(e.data);console.log("[EDIT] New event:",t),w.add(()=>Q(t,3e4))},r.onerror=e=>{console.error("EventSource failed:",e)}};function _(){const r="https://www.facebook.com/marketplace/you/selling";console.log({targetUrl:r}),chrome.tabs.query({},e=>{const t=e.find(o=>o.url&&o.url.startsWith(r));t?chrome.tabs.reload(t.id):chrome.tabs.create({url:r})})}function G(){return new Promise(r=>{chrome.tabs.query({url:x},t=>{if(t.length>0&&t[0].status==="complete")return r()});const e=(t,o,a)=>{o.status==="complete"&&a.url===x&&(chrome.tabs.onUpdated.removeListener(e),r())};chrome.tabs.onUpdated.addListener(e)})}const H=async()=>{B(),Y(),J(),_(),N()};chrome.runtime.onMessage.addListener((r,e)=>{if(r.type==="close-tab"){const t=e.tab?.id;t&&chrome.tabs.remove(t)}});function N(){const r=30+Math.floor(Math.random()*11);chrome.alarms.create("checkQueue",{delayInMinutes:r/60})}async function K(){console.log("🔄 Reloading extension..."),chrome.runtime.reload(),chrome.storage.local.set({waitingForTab:!0})}chrome.runtime.onStartup.addListener(async()=>{chrome.storage.local.get("waitingForTab",async r=>{r.waitingForTab&&(console.log("⏳ Extension restarted, waiting for target tab..."),await G(),console.log("✅ Target tab loaded → task done"),chrome.storage.local.remove("waitingForTab"))})});chrome.alarms.onAlarm.addListener(r=>{r.name==="checkQueue"&&(console.log("Alarm triggered, checking queue..."),w.size===0&&w.pending===0?(console.log("Queue is empty → enqueue reload task"),w.add(K)):console.log("Queue still busy → skip reload"),N())});H();