auto-post-marketplace-facebook/auto-post-facebook-extensio.../background/background.js

2 lines
11 KiB
JavaScript

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