diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..01231cb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "discord.removeRemoteRepository": true +} diff --git a/background.js b/background.js index f4867c2..66e6da2 100644 --- a/background.js +++ b/background.js @@ -1,281 +1,314 @@ -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'; +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; - } + 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 - } + 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) +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}`, }, - 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 + }), + 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, + }) + ), }); - } - return s + 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 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 + 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, + }; } - }, - 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) - } + } catch (t) { + console.warn(t); + } + return null; + })()), + await m.cache.set("get-stream", { + expiresAt: t, + value: n, + }); + } + return n; }, - 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); \ No newline at end of file + "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), + }, +}; +const 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); diff --git a/content/youtube/inject.js b/content/youtube/inject.js index 8100f7c..bb05167 100644 --- a/content/youtube/inject.js +++ b/content/youtube/inject.js @@ -904,64 +904,74 @@ function ne(e, t) { return e; } } -//spore -async function se(e, t, n) { +//spore fetch data +async function fetchSporeData(e, t, n) { + // if no youtube config, just return if (!window.ytcfg) return; - const s$1 = await e.fetch("/auth/token"); - if (!s(s$1) || !s$1.body) return; + // fetch our auth token + const mogulAuth = await e.fetch("/auth/token"); + // todo find s + if (!s(mogulAuth) || !mogulAuth.body) return; const r = { sourceType: "google", - token: s$1.body, + token: mogulAuth.body, }; t.setCredentials(r); - const o = await e.fetch("/spore/fetch-extension-mappings", n); - if (s(o)) { - const e = o.body; - e && - (null == e ? void 0 : e.length) > 0 && - e.forEach((e) => { - re(e, { + const sporeData = await e.fetch("/spore/fetch-extension-mappings", n); + if (s(sporeData)) { + const body = sporeData.body; + if (body && body.length > 0) { + body.forEach((e) => { + sporeInject(e, { sourceType: "youtube", }); }); + } + nks; } } -// spore -function re(e, { sourceType: t, retries: n = 15 }) { +// spore injection +function sporeInject(e, { sourceType: t, retries: n = 15 }) { if (!e) return; if (n < 0) return; - const s = (function (e) { + const injectionSelector = (function (e) { var t, n; + // if the iframe exists already, lets select that instead if (e.iframeQuerySelector) { - const s = document.querySelector(e.querySelector), - r = - null !== (t = null == s ? void 0 : s.contentDocument) && void 0 !== t - ? t - : null === (n = null == s ? void 0 : s.contentWindow) || - void 0 === n - ? void 0 - : n.document; + // void 0 = undefined + const query = document.querySelector(e.querySelector); + + const r = + null !== (t = null == query ? void 0 : query.contentDocument) && + void 0 !== t + ? t + : null === (n = null == query ? void 0 : query.contentWindow) || + void 0 === n + ? void 0 + : n.document; if (!r) return; return r.querySelector(e.iframeQuerySelector); } return document.querySelector(e.querySelector); })(e); - if (s) { + if (injectionSelector) { const t = document.createElement("iframe"); - (t.src = e.iframeUrl), - t.setAttribute("key", e.slug), - (t.id = e.slug), - "append" === e.domAction - ? s.appendChild(t) - : "replace" === e.domAction && s.replaceWith(t); - } else + t.src = e.iframeUrl; + t.setAttribute("key", e.slug); + t.id = e.slug; + "append" === e.domAction + ? injectionSelector.appendChild(t) + : "replace" === e.domAction && injectionSelector.replaceWith(t); + } else { setTimeout(() => { - re(e, { + sporeInject(e, { sourceType: t, retries: n - 1, }); }, 250); + } } +// ottos code? var ie; async function oe(e, n) { await customElements.whenDefined(e); @@ -1259,7 +1269,7 @@ if (he) { const s = setInterval(() => { const t = n(); (null == t ? void 0 : t.channelId) && - (clearInterval(s), se(fe, e, t.channelId)); + (clearInterval(s), fetchSporeData(fe, e, t.channelId)); }, 1e3); setInterval(() => { const e = document.querySelector("ytd-live-chat-frame iframe"); diff --git a/sporeinjection.json b/sporeinjection.json new file mode 100644 index 0000000..3cf4676 --- /dev/null +++ b/sporeinjection.json @@ -0,0 +1,7 @@ +{ + "iframeQuerySelector": "", + "querySelector": "", + "slug": "ludwig", + "domAction": "append", + "iframeUrl": "https://ludwig.social/component-instance/4e8899d0-6d27-11ec-88da-c92b950649ef" +}