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

2 lines
12 KiB
JavaScript

import{M as A,f as x}from"../assets/message.service-C0RCxylm.js";import{g as L}from"../assets/_commonjsHelpers-CqkleIqs.js";import"../assets/app-C7VSS-q9.js";async function O(h){return fetch(h).then(e=>e.blob()).then(e=>new Promise((t,i)=>{const a=new FileReader;a.onloadend=()=>{typeof a.result=="string"?t(a.result):i("Failed to convert blob to base64")},a.onerror=i,a.readAsDataURL(e)}))}class q{msgService;constructor(){this.msgService=new A("background")}publishJob(e,t=3e4){return new Promise((i,a)=>{chrome.tabs.query({},()=>{const f=r=>{const u=(s,n)=>{s===r&&n.status==="complete"&&(chrome.tabs.onUpdated.removeListener(u),this.msgService.send("content","background-to-content-publish",{data:e}))};chrome.tabs.onUpdated.addListener(u),this.msgService.waitForMessage("content-to-background-publish",t).then(s=>{this.msgService.send("content","background-to-content-actions",{data:s,type:"publist"}),i(s)}).catch(s=>{this.msgService.send("content","background-to-content-actions",{data:null,type:"publist"}),a(s)})};chrome.tabs.create({url:x.createItem},r=>{if(!r?.id){a(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((i,a)=>{const f=(r,u)=>{const s=(n,o)=>{n===r&&o.status==="complete"&&(chrome.tabs.onUpdated.removeListener(s),this.msgService.send("content","background-to-content-unlist",{data:e}))};chrome.tabs.onUpdated.addListener(s),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"}),i(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"}),a(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(u=>{console.log("Hoàn thành, giữ tab cũ:",r[0].id),i(u)}).catch(u=>{console.warn("Lỗi khi xử lý tab cũ:",r[0].id),a(u)})):chrome.tabs.create({url:x.marketPath,active:!1},u=>{if(!u?.id){a(new Error("Không mở được tab"));return}console.log("Đã mở tab mới:",u.id),f(u.id)})})})}rePublistJob(e,t=3e4){return new Promise((i,a)=>{chrome.tabs.query({},()=>{const f=r=>{const u=(s,n)=>{s===r&&n.status==="complete"&&(chrome.tabs.onUpdated.removeListener(u),this.msgService.send("content","background-to-content-re-publist",{data:e}))};chrome.tabs.onUpdated.addListener(u),this.msgService.waitForMessage("content-to-background-re-publist",t).then(s=>{chrome.tabs.remove(r,()=>{console.log("Đã đóng tab:",r)}),i(s)}).catch(s=>{chrome.tabs.remove(r,()=>{console.warn("Đóng tab do timeout/lỗi:",r)}),a(s)})};chrome.tabs.create({url:x.marketPath},r=>{if(!r?.id){a(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 i of e){const a=await O(i);t.push(a)}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 i(){}Object.create&&(i.prototype=Object.create(null),new i().__proto__||(t=!1));function a(s,n,o){this.fn=s,this.context=n,this.once=o||!1}function f(s,n,o,c,g){if(typeof o!="function")throw new TypeError("The listener must be a function");var d=new a(o,c||s,g),m=t?t+n:n;return s._events[m]?s._events[m].fn?s._events[m]=[s._events[m],d]:s._events[m].push(d):(s._events[m]=d,s._eventsCount++),s}function r(s,n){--s._eventsCount===0?s._events=new i:delete s._events[n]}function u(){this._events=new i,this._eventsCount=0}u.prototype.eventNames=function(){var n=[],o,c;if(this._eventsCount===0)return n;for(c in o=this._events)e.call(o,c)&&n.push(t?c.slice(1):c);return Object.getOwnPropertySymbols?n.concat(Object.getOwnPropertySymbols(o)):n},u.prototype.listeners=function(n){var o=t?t+n:n,c=this._events[o];if(!c)return[];if(c.fn)return[c.fn];for(var g=0,d=c.length,m=new Array(d);g<d;g++)m[g]=c[g].fn;return m},u.prototype.listenerCount=function(n){var o=t?t+n:n,c=this._events[o];return c?c.fn?1:c.length:0},u.prototype.emit=function(n,o,c,g,d,m){var p=t?t+n:n;if(!this._events[p])return!1;var l=this._events[p],b=arguments.length,w,v;if(l.fn){switch(l.once&&this.removeListener(n,l.fn,void 0,!0),b){case 1:return l.fn.call(l.context),!0;case 2:return l.fn.call(l.context,o),!0;case 3:return l.fn.call(l.context,o,c),!0;case 4:return l.fn.call(l.context,o,c,g),!0;case 5:return l.fn.call(l.context,o,c,g,d),!0;case 6:return l.fn.call(l.context,o,c,g,d,m),!0}for(v=1,w=new Array(b-1);v<b;v++)w[v-1]=arguments[v];l.fn.apply(l.context,w)}else{var C=l.length,E;for(v=0;v<C;v++)switch(l[v].once&&this.removeListener(n,l[v].fn,void 0,!0),b){case 1:l[v].fn.call(l[v].context);break;case 2:l[v].fn.call(l[v].context,o);break;case 3:l[v].fn.call(l[v].context,o,c);break;case 4:l[v].fn.call(l[v].context,o,c,g);break;default:if(!w)for(E=1,w=new Array(b-1);E<b;E++)w[E-1]=arguments[E];l[v].fn.apply(l[v].context,w)}}return!0},u.prototype.on=function(n,o,c){return f(this,n,o,c,!1)},u.prototype.once=function(n,o,c){return f(this,n,o,c,!0)},u.prototype.removeListener=function(n,o,c,g){var d=t?t+n:n;if(!this._events[d])return this;if(!o)return r(this,d),this;var m=this._events[d];if(m.fn)m.fn===o&&(!g||m.once)&&(!c||m.context===c)&&r(this,d);else{for(var p=0,l=[],b=m.length;p<b;p++)(m[p].fn!==o||g&&!m[p].once||c&&m[p].context!==c)&&l.push(m[p]);l.length?this._events[d]=l.length===1?l[0]:l:r(this,d)}return this},u.prototype.removeAllListeners=function(n){var o;return n?(o=t?t+n:n,this._events[o]&&r(this,o)):(this._events=new i,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=t,u.EventEmitter=u,h.exports=u})(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:i,message:a,customTimers:f={setTimeout,clearTimeout}}=e;let r,u;const n=new Promise((o,c)=>{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&&c(T(d)),u=()=>{c(T(d))},d.addEventListener("abort",u,{once:!0})}if(t===Number.POSITIVE_INFINITY){h.then(o,c);return}const g=new I;r=f.setTimeout.call(void 0,()=>{if(i){try{o(i())}catch(d){c(d)}return}typeof h.cancel=="function"&&h.cancel(),a===!1?o():a instanceof Error?c(a):(g.message=a??`Promise timed out after ${t} milliseconds`,c(g))},t),(async()=>{try{o(await h)}catch(d){c(d)}})()}).finally(()=>{n.clear(),u&&e.signal&&e.signal.removeEventListener("abort",u)});return n.clear=()=>{f.clearTimeout.call(void 0,r),r=void 0},n}function B(h,e,t){let i=0,a=h.length;for(;a>0;){const f=Math.trunc(a/2);let r=i+f;t(h[r],e)<=0?(i=++r,a-=f+1):a=f}return i}class U{#e=[];enqueue(e,t){t={priority:0,...t};const i={priority:t.priority,id:t.id,run:e};if(this.size===0||this.#e[this.size-1].priority>=t.priority){this.#e.push(i);return}const a=B(this.#e,i,(f,r)=>r.priority-f.priority);this.#e.splice(a,0,i)}setPriority(e,t){const i=this.#e.findIndex(f=>f.id===e);if(i===-1)throw new ReferenceError(`No promise function with the id "${e}" exists in the queue.`);const[a]=this.#e.splice(i,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 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,i)=>{e.addEventListener("abort",()=>{i(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((i,a)=>{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;i(r),this.emit("completed",r)}catch(f){if(f instanceof I&&!t.throwOnTimeout){i();return}a(f),this.emit("error",f)}finally{this.#x()}},t),this.emit("add"),this.#l()})}async addAll(e,t){return Promise.all(e.map(async i=>this.add(i,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(i=>{const a=()=>{t&&!t()||(this.off(e,a),i())};this.on(e,a)})}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));