2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
import{M as A}from"../assets/message.service-DcR3euAR.js";import{f as x}from"../assets/facebook.service-v39yqJ7I.js";import{g as L}from"../assets/_commonjsHelpers-CqkleIqs.js";class O{msgService;constructor(){this.msgService=new A("background")}publishJob(e,t=3e4){return new Promise((a,l)=>{chrome.tabs.query({},()=>{const f=n=>{const o=(u,r)=>{u===n&&r.status==="complete"&&(chrome.tabs.onUpdated.removeListener(o),this.msgService.send("content","background-to-content-publish",{data:e}))};chrome.tabs.onUpdated.addListener(o),this.msgService.waitForMessage("content-to-background-publish",t).then(a).catch(l)};chrome.tabs.create({url:x.createItem},n=>{if(!n?.id){l(new Error("Không mở được tab"));return}console.log("Đã mở tab mới:",n.id),f(n.id)})})})}unlistJob(e,t=3e4){return new Promise((a,l)=>{const f=(n,o)=>{const u=(r,i)=>{r===n&&i.status==="complete"&&(chrome.tabs.onUpdated.removeListener(u),this.msgService.send("content","background-to-content-unlist",{data:e}))};chrome.tabs.onUpdated.addListener(u),this.msgService.waitForMessage("content-to-background-unlist",t).then(r=>{chrome.tabs.remove(n,()=>{console.log("Đã đóng tab:",n)}),a(r)}).catch(r=>{chrome.tabs.remove(n,()=>{console.warn("Đóng tab do timeout/lỗi:",n)}),l(r)})};chrome.tabs.query({url:`${x.sellingPath}*`},n=>{n.length>0&&n[0].id?(console.log("Dùng lại tab cũ:",n[0].id),this.msgService.send("content","background-to-content-unlist",{data:e}),this.msgService.waitForMessage("content-to-background-unlist",t).then(o=>{console.log("Hoàn thành, giữ tab cũ:",n[0].id),a(o)}).catch(o=>{console.warn("Lỗi khi xử lý tab cũ:",n[0].id),l(o)})):chrome.tabs.create({url:x.marketPath,active:!1},o=>{if(!o?.id){l(new Error("Không mở được tab"));return}console.log("Đã mở tab mới:",o.id),f(o.id)})})})}rePublistJob(e,t=3e4){return new Promise((a,l)=>{chrome.tabs.query({},()=>{const f=n=>{const o=(u,r)=>{u===n&&r.status==="complete"&&(chrome.tabs.onUpdated.removeListener(o),this.msgService.send("content","background-to-content-re-publist",{data:e}))};chrome.tabs.onUpdated.addListener(o),this.msgService.waitForMessage("content-to-background-re-publist",t).then(u=>{chrome.tabs.remove(n,()=>{console.log("Đã đóng tab:",n)}),a(u)}).catch(u=>{chrome.tabs.remove(n,()=>{console.warn("Đóng tab do timeout/lỗi:",n)}),l(u)})};chrome.tabs.create({url:x.marketPath},n=>{if(!n?.id){l(new Error("Không mở được tab"));return}console.log("Đã mở tab mới:",n.id),f(n.id)})})})}}var P={exports:{}},_;function q(){return _||(_=1,(function(h){var e=Object.prototype.hasOwnProperty,t="~";function a(){}Object.create&&(a.prototype=Object.create(null),new a().__proto__||(t=!1));function l(u,r,i){this.fn=u,this.context=r,this.once=i||!1}function f(u,r,i,s,p){if(typeof i!="function")throw new TypeError("The listener must be a function");var m=new l(i,s||u,p),d=t?t+r:r;return u._events[d]?u._events[d].fn?u._events[d]=[u._events[d],m]:u._events[d].push(m):(u._events[d]=m,u._eventsCount++),u}function n(u,r){--u._eventsCount===0?u._events=new a:delete u._events[r]}function o(){this._events=new a,this._eventsCount=0}o.prototype.eventNames=function(){var r=[],i,s;if(this._eventsCount===0)return r;for(s in i=this._events)e.call(i,s)&&r.push(t?s.slice(1):s);return Object.getOwnPropertySymbols?r.concat(Object.getOwnPropertySymbols(i)):r},o.prototype.listeners=function(r){var i=t?t+r:r,s=this._events[i];if(!s)return[];if(s.fn)return[s.fn];for(var p=0,m=s.length,d=new Array(m);p<m;p++)d[p]=s[p].fn;return d},o.prototype.listenerCount=function(r){var i=t?t+r:r,s=this._events[i];return s?s.fn?1:s.length:0},o.prototype.emit=function(r,i,s,p,m,d){var g=t?t+r:r;if(!this._events[g])return!1;var c=this._events[g],b=arguments.length,y,v;if(c.fn){switch(c.once&&this.removeListener(r,c.fn,void 0,!0),b){case 1:return c.fn.call(c.context),!0;case 2:return c.fn.call(c.context,i),!0;case 3:return c.fn.call(c.context,i,s),!0;case 4:return c.fn.call(c.context,i,s,p),!0;case 5:return c.fn.call(c.context,i,s,p,m),!0;case 6:return c.fn.call(c.context,i,s,p,m,d),!0}for(v=1,y=new Array(b-1);v<b;v++)y[v-1]=arguments[v];c.fn.apply(c.context,y)}else{var k=c.length,E;for(v=0;v<k;v++)switch(c[v].once&&this.removeListener(r,c[v].fn,void 0,!0),b){case 1:c[v].fn.call(c[v].context);break;case 2:c[v].fn.call(c[v].context,i);break;case 3:c[v].fn.call(c[v].context,i,s);break;case 4:c[v].fn.call(c[v].context,i,s,p);break;default:if(!y)for(E=1,y=new Array(b-1);E<b;E++)y[E-1]=arguments[E];c[v].fn.apply(c[v].context,y)}}return!0},o.prototype.on=function(r,i,s){return f(this,r,i,s,!1)},o.prototype.once=function(r,i,s){return f(this,r,i,s,!0)},o.prototype.removeListener=function(r,i,s,p){var m=t?t+r:r;if(!this._events[m])return this;if(!i)return n(this,m),this;var d=this._events[m];if(d.fn)d.fn===i&&(!p||d.once)&&(!s||d.context===s)&&n(this,m);else{for(var g=0,c=[],b=d.length;g<b;g++)(d[g].fn!==i||p&&!d[g].once||s&&d[g].context!==s)&&c.push(d[g]);c.length?this._events[m]=c.length===1?c[0]:c:n(this,m)}return this},o.prototype.removeAllListeners=function(r){var i;return r?(i=t?t+r:r,this._events[i]&&n(this,i)):(this._events=new a,this._eventsCount=0),this},o.prototype.off=o.prototype.removeListener,o.prototype.addListener=o.prototype.on,o.prefixed=t,o.EventEmitter=o,h.exports=o})(P)),P.exports}var N=q();const z=L(N);class C extends Error{constructor(e){super(e),this.name="TimeoutError"}}class F extends Error{constructor(e){super(),this.name="AbortError",this.message=e}}const I=h=>globalThis.DOMException===void 0?new F(h):new DOMException(h),T=h=>{const e=h.reason===void 0?I("This operation was aborted."):h.reason;return e instanceof Error?e:I(e)};function M(h,e){const{milliseconds:t,fallback:a,message:l,customTimers:f={setTimeout,clearTimeout}}=e;let n,o;const r=new Promise((i,s)=>{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:m}=e;m.aborted&&s(T(m)),o=()=>{s(T(m))},m.addEventListener("abort",o,{once:!0})}if(t===Number.POSITIVE_INFINITY){h.then(i,s);return}const p=new C;n=f.setTimeout.call(void 0,()=>{if(a){try{i(a())}catch(m){s(m)}return}typeof h.cancel=="function"&&h.cancel(),l===!1?i():l instanceof Error?s(l):(p.message=l??`Promise timed out after ${t} milliseconds`,s(p))},t),(async()=>{try{i(await h)}catch(m){s(m)}})()}).finally(()=>{r.clear(),o&&e.signal&&e.signal.removeEventListener("abort",o)});return r.clear=()=>{f.clearTimeout.call(void 0,n),n=void 0},r}function $(h,e,t){let a=0,l=h.length;for(;l>0;){const f=Math.trunc(l/2);let n=a+f;t(h[n],e)<=0?(a=++n,l-=f+1):l=f}return a}class D{#e=[];enqueue(e,t){t={priority:0,...t};const a={priority:t.priority,id:t.id,run:e};if(this.size===0||this.#e[this.size-1].priority>=t.priority){this.#e.push(a);return}const l=$(this.#e,a,(f,n)=>n.priority-f.priority);this.#e.splice(l,0,a)}setPriority(e,t){const a=this.#e.findIndex(f=>f.id===e);if(a===-1)throw new ReferenceError(`No promise function with the id "${e}" exists in the queue.`);const[l]=this.#e.splice(a,1);this.enqueue(l.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 J extends z{#e;#o;#s=0;#m;#c;#d=0;#r;#a;#t;#v;#n=0;#u;#i;#p;#y=1n;timeout;constructor(e){if(super(),e={carryoverConcurrencyCount:!1,intervalCap:Number.POSITIVE_INFINITY,interval:0,concurrency:Number.POSITIVE_INFINITY,autoStart:!0,queueClass:D,...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.#m=e.intervalCap,this.#c=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.#i=e.autoStart===!1}get#w(){return this.#o||this.#s<this.#m}get#E(){return this.#n<this.#u}#x(){this.#n--,this.#h(),this.emit("next")}#P(){this.#b(),this.#g(),this.#a=void 0}get#S(){const e=Date.now();if(this.#r===void 0){const t=this.#d-e;if(t<0)this.#s=this.#e?this.#n:0;else return this.#a===void 0&&(this.#a=setTimeout(()=>{this.#P()},t)),!0}return!1}#h(){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.#i){const e=!this.#S;if(this.#w&&this.#E){const t=this.#t.dequeue();return t?(this.emit("active"),t(),e&&this.#g(),!0):!1}}return!1}#g(){this.#o||this.#r!==void 0||(this.#r=setInterval(()=>{this.#b()},this.#c),this.#d=Date.now()+this.#c)}#b(){this.#s===0&&this.#n===0&&this.#r&&(clearInterval(this.#r),this.#r=void 0),this.#s=this.#e?this.#n:0,this.#l()}#l(){for(;this.#h(););}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.#l()}async#_(e){return new Promise((t,a)=>{e.addEventListener("abort",()=>{a(e.reason)},{once:!0})})}setPriority(e,t){this.#t.setPriority(e,t)}async add(e,t={}){return t.id??=(this.#y++).toString(),t={timeout:this.timeout,throwOnTimeout:this.#p,...t},new Promise((a,l)=>{this.#t.enqueue(async()=>{this.#n++,this.#s++;try{t.signal?.throwIfAborted();let f=e({signal:t.signal});t.timeout&&(f=M(Promise.resolve(f),{milliseconds:t.timeout})),t.signal&&(f=Promise.race([f,this.#_(t.signal)]));const n=await f;a(n),this.emit("completed",n)}catch(f){if(f instanceof C&&!t.throwOnTimeout){a();return}l(f),this.emit("error",f)}finally{this.#x()}},t),this.emit("add"),this.#h()})}async addAll(e,t){return Promise.all(e.map(async a=>this.add(a,t)))}start(){return this.#i?(this.#i=!1,this.#l(),this):this}pause(){this.#i=!0}clear(){this.#t=new this.#v}async onEmpty(){this.#t.size!==0&&await this.#f("empty")}async onSizeLessThan(e){this.#t.size<e||await this.#f("next",()=>this.#t.size<e)}async onIdle(){this.#n===0&&this.#t.size===0||await this.#f("idle")}async#f(e,t){return new Promise(a=>{const l=()=>{t&&!t()||(this.off(e,l),a())};this.on(e,l)})}get size(){return this.#t.size}sizeBy(e){return this.#t.filter(e).length}get pending(){return this.#n}get isPaused(){return this.#i}}const S=new J({concurrency:1}),w=new O;w.msgService.on("popup-to-content",h=>{switch(console.log("Background nhận từ popup:",h),w.msgService.send("content","popup-to-content",h),h?.type){case"publist":{S.add(()=>w.publishJob(h.data,6e4));break}case"unlist":{S.add(()=>w.unlistJob(h.data,6e4));break}case"re-publist":{S.add(()=>w.rePublistJob(h.data,6e4));break}}});w.msgService.on("content-to-popup",h=>{console.log("Background nhận từ content:",h),w.msgService.send("popup","content-to-popup",h)});
|