mogultv/background.js

281 lines
9.9 KiB
JavaScript

import {
d as dt
} from './parse_token.util-ed270559.js';
import {
n
} from './router.interface-6cdbc015.js';
import {
e
} from './fetch_youtube-cfbafc47.js';
import {
s as s$1,
m
} from './storage-a8ac7bd3.js';
class s extends Error {
constructor(t, e) {
super(e), this.code = t, this.message = e;
}
}
const r = (t, e = {}, n) => ({
body: e,
meta: {
isSuccess: !1,
code: t,
nonce: n
}
});
let i;
const c = t => s$1.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)
},
token: async () => await m.auth.get("token"),
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 n = await c(t.href),
a = await fetch("https://v2.mogultv.org/auth/youtube", {
method: "POST",
body: JSON.stringify(Object.assign(Object.assign({}, t), {
cookies: n
}))
});
if (200 !== a.status) return null;
const {
jwt: o
} = await a.json();
return await m.auth.set("token", o), o
},
link: async t => {
const n = await m.auth.get("token"),
o = await fetch(`https://v2.mogultv.org/link/${t}`, {
method: "GET",
headers: {
Authorization: `Bearer ${n}`
}
});
if (200 !== o.status) return;
const s = await o.text();
await chrome.cookies.set({
url: "https://v2.mogultv.org",
name: "Authorization",
value: n,
httpOnly: !0,
path: `/link/${t}/callback`,
expirationDate: Math.floor(Date.now() / 1e3) + 3600
}), await chrome.windows.create({
url: s,
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 a, o;
(null === (a = e.sender.tab) || void 0 === a ? void 0 : a.id) && s$1.tabs.remove(null === (o = e.sender.tab) || void 0 === o ? void 0 : o.id), null == i || i(t);
},
"disconnect-link": async t => {
const n = await m.auth.get("token"),
o = await fetch(`https://v2.mogultv.org/link/${t}`, {
method: "DELETE",
headers: {
Authorization: `Bearer ${n}`
}
});
if (200 !== o.status) return;
const {
jwt: s
} = await o.json();
return await m.auth.set("token", s), dt(s)
}
};
const l = t => async () => {
var n, a;
const o = await m.cache.get(t);
let s;
if (o && o.expiresAt > Date.now()) s = o.value;
else {
const o = await fetch("https://v2.mogultv.org" + t),
r = Number(o.headers.get("age")),
i = Number((null === (a = null === (n = o.headers.get("cache-control")) || void 0 === n ? void 0 : n.match(/max-age=(\d+)/)) || void 0 === a ? void 0 : a[1]) || 300) - r,
c = Date.now() + 1e3 * i;
s = await o.json(), await m.cache.set(t, {
expiresAt: c,
value: s
});
}
return s
};
async function d(t, e) {
const n = {
sourceType: t,
sourceId: e
};
try {
const t = await async function(t, e, n, {
throwIfErrors: a
} = {}) {
var o;
const s = await fetch(t, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: e,
variables: n
})
}),
r = await s.json();
if (a && (null === (o = r.data) || void 0 === o ? void 0 : o.errors)) throw new Error(`spore graphql error ${r.data.errors}`);
return r.data
}("https://zygote.spore.build/graphql", "query GetExtensionMappingConnection($sourceType: String, $sourceId: String) {\n\textensionMappingConnection(sourceType: $sourceType, sourceId: $sourceId) {\n\t\tnodes {\n\t\t\tid\n\t\t\tslug\n\t\t\tiframeUrl\n\t\t\tdomAction\n\t\t\tquerySelector\n\t\t\tiframeQuerySelector\n\t\t}\n\t}\n}", n);
return t.extensionMappingConnection.nodes
} catch (t) {
console.error(t);
}
}
const h = {
youtube: {
"get-stream": async () => {
const t = await m.cache.get("get-stream");
let n;
if (t && t.expiresAt > Date.now()) n = t.value;
else {
const t = Date.now() + 6e4;
n = 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(),
n = /(?:window\s*\[\s*["']ytInitialData["']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;/.exec(e);
if (n) {
const t = JSON.parse(n[1]),
e = t.currentVideoEndpoint.watchEndpoint.videoId,
a = t.contents.twoColumnWatchNextResults.results.results.contents[0].videoPrimaryInfoRenderer,
o = t.contents.twoColumnWatchNextResults.results.results.contents[1].videoSecondaryInfoRenderer.owner.videoOwnerRenderer.thumbnail.thumbnails[0].url,
s = a.viewCount.videoViewCountRenderer.viewCount.runs.find((t => /^[0-9,]+$/.test(t.text))).text;
return {
title: a.title.runs[0].text,
viewersCount: parseInt(s.replace(/,/g, "")),
previewImageURL: `https://i.ytimg.com/vi/${e}/mqdefault.jpg`,
profileImageURL: o
}
}
} catch (t) {
console.warn(t);
}
return null
}(), await m.cache.set("get-stream", {
expiresAt: t,
value: n
});
}
return n
},
"get-user": async t => {
const e$1 = await c(t.href),
n = await e(Object.assign(Object.assign({}, t), {
cookies: e$1
}));
if (!n.success) throw new s(n.code, n.message);
return n.data
}
},
gateway: {
users: l("/gateway/users"),
emotes: l("/gateway/emotes"),
badges: l("/gateway/badges"),
"set-settings": async t => {
const n = await m.auth.get("token"),
o = await fetch("https://v2.mogultv.org/gateway/settings", {
method: "PUT",
headers: {
Authorization: `Bearer ${n}`
},
body: JSON.stringify(t)
}),
{
jwt: s
} = await o.json();
return m.auth.set("token", s), dt(s)
}
},
auth: u,
extension: {
popup: () => s$1.action.openPopup(),
"open-tab": async t => {
await s$1.tabs.create({
url: t
});
}
},
spore: {
"fetch-extension-mappings": async t => await d("youtube", t)
}
},
w = async (e, n$1) => {
var a;
const {
path: o,
body: s
} = e;
if (!o) return;
e.meta ? e.meta.sender = n$1 : e.meta = {
sender: n$1,
isPort: !1
}, e.meta.isPort || (e.meta.isPort = !1);
const i = t => {},
c = o.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 n$1 = ((e, n$1) => ({
body: e,
meta: {
isSuccess: !0,
code: n.Success,
nonce: n$1
}
}))(await l(s, e.meta), e.nonce);
return i(n$1), n$1
} catch (n$1) {
const o = n$1;
return r(null !== (a = o.code) && void 0 !== a ? a : n.Unknown, {
message: o.message
}, e.nonce)
}
};
s$1.runtime.onConnect.addListener((t => {
const e = t.sender;
e && t.onMessage.addListener((async n => {
n.meta = {
isPort: !0
};
const a = await w(n, e);
t.postMessage(a);
}));
})), s$1.runtime.onMessage.addListener(w);