import { d as dt } from './parse_token.util-ed270559.js'; import { e } from './fetch_youtube-71c76849.js'; import { n } from './router.interface-6cdbc015.js'; import { s, m } from './storage-a8ac7bd3.js'; class o extends Error { constructor(t, e) { super(e), this.code = t, this.message = e; } } const r = (t, e = {}, a) => ({ body: e, meta: { isSuccess: !1, code: t, nonce: a } }); let i; const c = t => s.cookies.getAll({ url: t }).then((t => t.filter((t => !t.name.startsWith("ST-"))).map((t => [t.name, t.value])))).then(Object.fromEntries), u = { "@me": async () => { const t = await m.auth.get("token"); return dt(t) }, logout: async () => { const t = await m.auth.get("token"); await fetch("https://v2.mogultv.org/auth/youtube", { method: "DELETE", headers: { Authorization: `Bearer ${t}` } }), await m.auth.remove("token"); }, login: async t => { const a = await c(t.href), s = await fetch("https://v2.mogultv.org/auth/youtube", { method: "POST", body: JSON.stringify(Object.assign(Object.assign({}, t), { cookies: a })) }); if (200 !== s.status) return null; const { jwt: n } = await s.json(); return await m.auth.set("token", n), n }, link: async t => { const a = await m.auth.get("token"), n = await fetch(`https://v2.mogultv.org/link/${t}`, { method: "GET", headers: { Authorization: `Bearer ${a}` } }); if (200 !== n.status) return; const o = await n.text(); await chrome.cookies.set({ url: "https://v2.mogultv.org", name: "Authorization", value: a, httpOnly: !0, path: `/link/${t}/callback`, expirationDate: Math.floor(Date.now() / 1e3) + 3600 }), await chrome.windows.create({ url: o, focused: !0, type: "popup", width: 850, height: 800 }); const r = await new Promise((t => { i = t; })); return m.auth.set("token", r), dt(r) }, "finish-link": async (t, e) => { var s$1, n; (null === (s$1 = e.sender.tab) || void 0 === s$1 ? void 0 : s$1.id) && s.tabs.remove(null === (n = e.sender.tab) || void 0 === n ? void 0 : n.id), null == i || i(t); }, "disconnect-link": async t => { const a = await m.auth.get("token"), n = await fetch(`https://v2.mogultv.org/link/${t}`, { method: "DELETE", headers: { Authorization: `Bearer ${a}` } }); if (200 !== n.status) return; const { jwt: o } = await n.json(); return await m.auth.set("token", o), dt(o) } }; const l = t => async () => { var a, s; const n = await m.cache.get(t); let o; if (n && n.expiresAt > Date.now()) o = n.value; else { const n = await fetch("https://v2.mogultv.org" + t), r = Number(n.headers.get("age")), i = Number((null === (s = null === (a = n.headers.get("cache-control")) || void 0 === a ? void 0 : a.match(/max-age=(\d+)/)) || void 0 === s ? void 0 : s[1]) || 300) - r, c = Date.now() + 1e3 * i; o = await n.json(), await m.cache.set(t, { expiresAt: c, value: o }); } return o }, h = { youtube: { "get-stream": async () => { const t = await m.cache.get("get-stream"); let a; if (t && t.expiresAt > Date.now()) a = t.value; else { const t = Date.now() + 6e4; a = await async function() { try { const t = await fetch("https://youtube.com/channel/UCrPseYLGpNygVi34QpGNqpA/live"); if (200 !== t.status) return null; const e = await t.text(), a = /(?:window\s*\[\s*["']ytInitialData["']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;/.exec(e); if (a) { const t = JSON.parse(a[1]), e = t.currentVideoEndpoint.watchEndpoint.videoId, s = t.contents.twoColumnWatchNextResults.results.results.contents[0].videoPrimaryInfoRenderer, n = t.contents.twoColumnWatchNextResults.results.results.contents[1].videoSecondaryInfoRenderer.owner.videoOwnerRenderer.thumbnail.thumbnails[0].url, o = s.viewCount.videoViewCountRenderer.viewCount.runs.find((t => /^[0-9,]+$/.test(t.text))).text; return { title: s.title.runs[0].text, viewersCount: parseInt(o.replace(/,/g, "")), previewImageURL: `https://i.ytimg.com/vi/${e}/mqdefault.jpg`, profileImageURL: n } } } catch (t) { console.warn(t); } return null }(), await m.cache.set("get-stream", { expiresAt: t, value: a }); } return a }, "get-user": async t => { const e$1 = await c(t.href), a = await e(Object.assign(Object.assign({}, t), { cookies: e$1 })); if (!a.success) throw new o(a.code, a.message); return a.data } }, gateway: { users: l("/gateway/users"), emotes: l("/gateway/emotes"), badges: l("/gateway/badges"), "set-settings": async t => { const a = await m.auth.get("token"), n = await fetch("https://v2.mogultv.org/gateway/settings", { method: "PUT", headers: { Authorization: `Bearer ${a}` }, body: JSON.stringify(t) }), { jwt: o } = await n.json(); return m.auth.set("token", o), dt(o) } }, auth: u, extension: { popup: () => s.action.openPopup(), "open-tab": async t => { await s.tabs.create({ url: t }); } } }, d = async (e, a) => { var s; const { path: n$1, body: o } = e; if (!n$1) return; e.meta ? e.meta.sender = a : e.meta = { sender: a, isPort: !1 }, e.meta.isPort || (e.meta.isPort = !1); const i = t => {}, c = n$1.split("/").filter(Boolean), u = c[0]; if (!(u in h)) { return r(n.NotFound, void 0, e.nonce) } const l = h[u][c.slice(1).join("/")]; if (!l) { return r(n.NotFound, void 0, e.nonce) } try { const a = ((e, a) => ({ body: e, meta: { isSuccess: !0, code: n.Success, nonce: a } }))(await l(o, e.meta), e.nonce); return i(a), a } catch (a) { const n$1 = a; return r(null !== (s = n$1.code) && void 0 !== s ? s : n.Unknown, { message: n$1.message }, e.nonce) } }; s.runtime.onConnect.addListener((t => { const e = t.sender; e && t.onMessage.addListener((async a => { a.meta = { isPort: !0 }; const s = await d(a, e); t.postMessage(s); })); })), s.runtime.onMessage.addListener(d);