-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
1 lines (1 loc) · 1.67 KB
/
service-worker.js
1
"use strict";async function sendMessageToActiveTab(t){let[e]=await chrome.tabs.query({active:!0,lastFocusedWindow:!0}),a=await chrome.tabs.sendMessage(e.id,t);return a}async function startimport(t,e){let a=chrome.tabs.create({url:t[1]});function s(a,o,r){if(a==r.id&&"complete"==o.status&&r.url===t[1]){chrome.tabs.sendMessage(r.id,{type:"startimport",data:{name:e,arr:t}}),chrome.tabs.onUpdated.removeListener(s);return}}console.log(t),chrome.tabs.onUpdated.addListener(s);let o=await chrome.storage.local.get(["imports"]);void 0==(o=o.imports)?await chrome.storage.local.set({imports:[a.id]}):await chrome.storage.local.set({imports:[...o,a.id]})}chrome.runtime.onInstalled.addListener(t=>{"install"===t.reason?chrome.tabs.create({url:"welcome.html"}):"update"===t.reason&&chrome.tabs.create({url:"https://github.com/frostdevelop/exportplaylist/releases"})}),chrome.runtime.setUninstallURL("https://chromewebstore.google.com/detail/exportplaylist/llckehcoicgfpdcjmkjgcfnbbgpalofn"),chrome.tabs.onRemoved.addListener(async t=>{let e=await chrome.storage.local.get(["imports"]);e=e.imports;let a=e.indexOf(t);-1!=a&&(e.splice(a,1),await chrome.storage.local.set({imports:e}))}),chrome.runtime.onMessage.addListener(async(t,e,a)=>{let{type:s,data:o}=t;if(console.log(s),"startimport"===s&&(startimport(o.arr,o.name),console.log("Starting import")),"importdone"===s&&(await chrome.tabs.remove(e.tab.id),console.log("Import finished",o.name)),"stopimport"===s){let r=await chrome.storage.local.get(["imports"]);if(r=r.imports,console.log(r),void 0!=r){for(let i=0;i<r.length;i++)try{await chrome.tabs.remove(r[i])}catch(l){console.error("Tab removing error:",l)}await chrome.storage.local.set({imports:[]})}}});