mogultv/content/youtube/inject.js

427 lines
18 KiB
JavaScript

import {
e
} from '../../index-6137f488.js';
import {
e as e$2
} from '../../background.injected-af36b849.js';
import {
n,
e as e$4
} from '../../get_stream_details-b6177000.js';
import {
d as dt,
t
} from '../../parse_token.util-ed270559.js';
import {
e as e$3
} from '../../fetch_youtube-71c76849.js';
import {
s
} from '../../router.interface-6cdbc015.js';
import {
e as e$1
} from '../../style-inject.es-a0e1a0ba.js';
function u(t) {
if (!t) return 0;
t = t.toLowerCase();
const e = /\((.+)\)/.exec(t);
if (!e) return 1;
const n = e[1].split(/\s/),
i = parseInt(n[0]);
return isNaN(i) ? 0 : n[1].startsWith("year") ? 12 * parseInt(n[0]) : parseInt(n[0])
}
function d(t, e) {
const n = e;
for (let e = 0; e < n.length; e++)
if (n[e][0] > t) return n[e - 1][1];
return n[n.length - 1][1]
}
function l(t, e, n, i) {
const a = e(t.authorExternalChannelId);
! function(t, e) {
(null == e ? void 0 : e.a) && (t.authorName = {
simpleText: e.a
});
}(t, a),
function(t, e) {
var n, i;
t.authorBadges || (t.authorBadges = []);
for (const a of t.authorBadges) {
const t = a.liveChatAuthorBadgeRenderer;
(null === (n = t.icon) || void 0 === n ? void 0 : n.iconType) ? t._mtvType = null === (i = t.icon) || void 0 === i ? void 0 : i.iconType.toLowerCase(): t.customThumbnail && (t._mtvType = "member"), "moderator" === t._mtvType && (delete t.icon, t._mtvType = "moderator", t.customThumbnail = {
thumbnails: [{
url: e,
width: 18,
height: 18
}]
});
}
}(t, i),
function(t, e, n) {
if ((null == n ? void 0 : n.b) && !(n.b <= 0))
for (const i of t.authorBadges) {
const t = i.liveChatAuthorBadgeRenderer;
if ("member" !== t._mtvType) continue;
const a = u(t.tooltip);
if (0 === a) continue;
const o = a + n.b,
r = `Member (${o} months)`,
s = d(o, e);
t.customThumbnail = s, delete t.icon, t.tooltip = r, t.accessibility.accessibilityData.label = r;
}
}(t, n, a);
}
const h = /[\s.,?!]/;
function f(t) {
const e = [];
let n = 0;
for (let i = 0; i < t.length - 1; i++) h.test(t[i]) !== h.test(t[i + 1]) && (e.push(t.substring(n, i + 1)), n = i + 1);
return e.push(t.substring(n)), e
}
function m(t$1) {
let e;
if (t$1.provider === t.Twitch) e = `https://static-cdn.jtvnw.net/emoticons/v2/${t$1.id}/static/dark/1.0`;
else if (t$1.provider === t.FFZ) e = `https://cdn.frankerfacez.com/emote/${t$1.id}/1`;
else {
if (t$1.provider !== t.BTTV) return;
e = `https://cdn.betterttv.net/emote/${t$1.id}/1x`;
}
return {
emojiId: "mogultv-" + t$1.name + "-" + t$1.id,
image: {
thumbnails: [{
url: e
}],
accessibility: {
accessibilityData: {
label: t$1.name
}
}
},
isCustomEmoji: !0,
searchTerms: [t$1.name],
shortcuts: [":" + t$1.name + ":", t$1.name]
}
}
const g = ["#ff0000", "#009000", "#b22222", "#ff7f50", "#9acd32", "#ff4500", "#2e8b57", "#daa520", "#d2691e", "#5f9ea0", "#1e90ff", "#ff69b4", "#00ff7f", "#a244f9"];
/*!
* cookie
* Copyright(c) 2012-2014 Roman Shtylman
* Copyright(c) 2015 Douglas Christopher Wilson
* MIT Licensed
*/
var v = function(t, e) {
if ("string" != typeof t) throw new TypeError("argument str must be a string");
for (var n = {}, i = e || {}, a = t.split(p), o = i.decode || y, r = 0; r < a.length; r++) {
var s = a[r],
c = s.indexOf("=");
if (!(c < 0)) {
var u = s.substr(0, c).trim(),
d = s.substr(++c, s.length).trim();
'"' == d[0] && (d = d.slice(1, -1)), null == n[u] && (n[u] = w(d, o));
}
}
return n
},
y = decodeURIComponent,
p = /; */;
function w(t, e) {
try {
return e(t)
} catch (e) {
return t
}
}
var b;
class A {
constructor(t) {
this.gatewayService = t;
}
addAliasesToMessage(t) {
l(t, (t => this.gatewayService.getUserInfo(t)), this.gatewayService.badgeList, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAABg2lDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV/TiiIV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zR/tV1O0FOk7dBblougE+Pl+6FKGijukGtWesZUZBZT5NcT2KdWT0hl8O2AK9ZT8M6xAZjzO8v3PFLkmVBTpsFuQgi2Nu/Ji3QXv0FPBynaffELJkAAAAASUVORK5CYII=");
}
}
class T {
constructor(t) {
this.gatewayService = t;
}
addTwitchEmotesToMessage(t) {
return function(t, e) {
const n = [];
for (const i of t.runs)
if ("string" == typeof i.text) {
const {
text: t
} = i;
let a = 0,
o = 0;
const r = f(t);
let s = !1;
for (const i of r) {
const r = e("🌝" === i ? "Kappa" : i);
r && (s = !0, a > 0 && n.push({
text: t.substring(o, a)
}), n.push({
emoji: r
}), o = a + i.length), a += i.length;
}
s ? n.push({
text: t.substring(o, a)
}) : n.push(i);
} else if ("🌝" === i.emoji.emojiId) {
const t = e("Kappa");
t && n.push({
emoji: t
});
} else n.push(i);
return t.runs = n, t
}(t, (t => this.gatewayService.getEmote(t)))
}
}
class x extends e.exports.EventEmitter {
constructor(t) {
super(), this.backgroundService = t, this.badgeList = [], this.emoteCache = new Map, t.getLiveStorageValue("auth.token", dt).then((t => this.myUserInfo = t)), this.fetchUserInfo(), this.fetchEmotes(), this.fetchBadges(), setInterval((() => {
this.fetchUserInfo(), this.fetchEmotes(), this.fetchBadges();
}), 3e5);
}
async fetchBadges() {
const t = await this.backgroundService.fetch("/gateway/badges");
if (!s(t)) return;
const e = t.body.sort(((t, e) => t.months - e.months));
this.badgeList = e.map((t => [t.months, {
thumbnails: [{
url: t.url,
width: 16,
height: 16
}]
}])), this.emit("badges", this.badgeList);
}
async fetchEmotes() {
const t = await this.backgroundService.fetch("/gateway/emotes");
if (s(t)) {
this.emoteCache.clear();
for (const e of t.body) {
const t = m(e);
t && this.emoteCache.set(e.name, t);
}
this.emit("emotes", this.emoteCache);
}
}
async fetchUserInfo() {
const t = await this.backgroundService.fetch("/gateway/users");
s(t) && (this.infoCache = new Map(t.body), this.emit("users", this.infoCache));
}
getSerializedMetadata(t) {
const e = t.links.find((e => e.prv === t.prv)),
n = {
a: null == e ? void 0 : e.name,
b: t.meta.sub,
c: t.meta.col
};
return Object.values(n).filter((t => void 0 !== t)).length > 0 ? n : void 0
}
getUserInfo(t) {
var e, n, i;
if ((null === (n = null === (e = this.myUserInfo) || void 0 === e ? void 0 : e.value) || void 0 === n ? void 0 : n.sub) === t) {
const t = this.getSerializedMetadata(this.myUserInfo.value);
if (t) return t
}
return null === (i = this.infoCache) || void 0 === i ? void 0 : i.get(t)
}
getEmote(t) {
var e;
return null === (e = this.emoteCache) || void 0 === e ? void 0 : e.get(t)
}
}
async function I(t, n) {
await customElements.whenDefined(t);
const i = customElements.get(t);
if (!i) return void console.warn(`Polymer: ${t} not found`);
const a = i.prototype[n.functionName];
i.prototype[n.functionName] = function(...t) {
if (n.ludwigOnly && !e$4()) return a.apply(this, t);
if (n.type === b.OverrideFunction) try {
return n.function.apply(this, t)
} catch (t) {
console.error(JSON.stringify(n)), console.error(t);
}
if (n.type === b.RunBefore) try {
n.function.apply(this, t);
} catch (t) {
console.error(t);
}
let i = a.apply(this, t);
if (n.type === b.RunAfter) try {
i = n.function.apply(this, [i]);
} catch (t) {
console.error(t);
}
return i
};
}! function(t) {
t[t.OverrideFunction = 0] = "OverrideFunction", t[t.RunBefore = 1] = "RunBefore", t[t.RunAfter = 2] = "RunAfter";
}(b || (b = {}));
const B = (t, e, {
LudwigOnly: n
}) => ({
functionName: t,
function: e,
ludwigOnly: n || !1,
type: b.OverrideFunction
}),
E = (t, e, {
LudwigOnly: n
}) => ({
functionName: t,
function: e,
ludwigOnly: n || !1,
type: b.RunBefore
});
function C(t, n, i) {
I("yt-live-chat-item-list-renderer", E("handleAddChatItemAction_", (function(e) {
e.item.liveChatTextMessageRenderer && (t.addTwitchEmotesToMessage(e.item.liveChatTextMessageRenderer.message), n.addAliasesToMessage(e.item.liveChatTextMessageRenderer));
}), {
LudwigOnly: true
})), I("yt-live-chat-text-input-field-renderer", ((t, e, {
LudwigOnly: n
}) => ({
functionName: t,
function: e,
ludwigOnly: n || !1,
type: b.RunAfter
}))("calculateLiveChatRichMessageInput_", (function(t) {
var e;
if (!(null == t ? void 0 : t.textSegments)) return t;
for (const n of t.textSegments)
if (null === (e = n.emojiId) || void 0 === e ? void 0 : e.startsWith("mogultv")) {
const [, t] = n.emojiId.split("-");
delete n.emojiId, n.text = t;
} return t
}), {
LudwigOnly: true
})), i.on("emotes", (t => {
if (!e$4()) return;
const n = document.querySelector("yt-live-chat-item-list-renderer");
if (null == n ? void 0 : n.emojiManager) {
if (n.emojiManager._mogulTvLoaded) return;
n.emojiManager._mogulTvLoaded = !0, n.emojiManager.load([...t.values()]);
} else console.warn("Cannot find chat list");
}));
}
function S(t) {
return t ? t._mtvType ? t._mtvType : t.icon ? t.icon.iconType.toLowerCase() : t.customThumbnail ? "member" : "" : ""
}
async function M(t) {
I("yt-live-chat-author-badge-renderer", B("computeType_", (t => S(t.liveChatAuthorBadgeRenderer)), {
LudwigOnly: true
})), I("yt-live-chat-author-chip", B("computeAuthorType_", (function(e) {
var n, i;
const a = this.$["author-name"];
if (a) {
let e;
const o = null === (n = this.parentElement) || void 0 === n ? void 0 : n.parentElement,
r = null === (i = null == o ? void 0 : o.data) || void 0 === i ? void 0 : i.authorExternalChannelId;
if (r) {
const n = t.getUserInfo(r);
(null == n ? void 0 : n.c) && (e = n.c);
}
e || (e = function(t) {
const e = function(t) {
let e = 0;
if (0 === t.length) return 0;
for (let n = 0; n < t.length; n++) e = (e << 5) - e + t.charCodeAt(n), e |= 0;
return e
}(t);
return g[(e % g.length + g.length) % g.length]
}(this.authorName.simpleText)), a.style.color = e;
}
return function(t) {
if (!t) return "";
for (const e of t) {
if (!e.liveChatAuthorBadgeRenderer) continue;
const t = S(e.liveChatAuthorBadgeRenderer);
if ("verified" !== t) return t
}
return ""
}(e)
}), {
LudwigOnly: true
}));
}
e$1("\n/* Make the masthead transparent unless we are hovering it */\nbody[data-mogul-theater-mode] #masthead-container {\n opacity: 0.1;\n transition: opacity .2s ease;\n}\n\nbody[data-mogul-theater-mode] #masthead-container:hover {\n opacity: 1;\n}\n\n\n@media screen and (min-width: 1014px) {\n /* Make chat fill screen vertically */\n body[data-mogul-theater-mode] #chat.ytd-watch-flexy {\n height: calc(100vh - 56px) !important;\n width: var(--ytd-watch-flexy-sidebar-width);\n position: absolute;\n top: 56px;\n right: 0;\n }\n\n body[data-mogul-theater-mode] #player-theater-container.ytd-watch-flexy {\n width: calc(100vw - var(--ytd-watch-flexy-sidebar-width) - var(--ytd-watch-flexy-scrollbar-width)) !important;\n max-width: calc(100vw - var(--ytd-watch-flexy-sidebar-width) - var(--ytd-watch-flexy-scrollbar-width)) !important;\n height: calc(100vh - 56px) !important;\n max-height: calc(100vh - 56px) !important;\n }\n}");
const R = null !== document.querySelector("yt-live-chat-app"),
j = new e$2;
if (R) {
const t = new x(j),
e = new T(t),
n = new A(t);
L = j, C(e, n, O = t), async function(t) {
const e = await t.getLiveStorageValue("settings.chatBatching");
I("yt-timed-continuation", E("dataChanged_", (function() {
e.value && (this.data.timeoutMs /= 3 * Math.random() + 2);
}), {
LudwigOnly: !0
})), I("yt-live-chat-renderer", E("preprocessActions_", (function(t) {
const n = Object.getPrototypeOf(this.smoothedQueue_);
for (const t in n)
if (t.includes("emitSmoothedMessages")) {
n[t] = function() {
if (this.nextUpdateId_ = null, this.messageQueue_.length) {
let i = 1e4;
this.estimatedUpdateInterval_ && (i = this.estimatedUpdateInterval_ - Date.now() + this.lastUpdateTime_);
const a = this.messageQueue_.length < i / 80 ? 1 : Math.ceil(this.messageQueue_.length / (i / 80)),
o = this.messageQueue_.splice(0, a);
if (this.callback && this.callback(o[0]), this.messageQueue_.length) {
let o = 1;
e.value || (1 === a ? (o = i / this.messageQueue_.length, o *= Math.random() + .5, o = Math.min(1e3, o), o = Math.max(80, o)) : o = 80), this.nextUpdateId_ = window.setTimeout(n[t].bind(this), o);
}
}
};
break
} return t
}), {
LudwigOnly: !0
}));
}(L), M(O);
} else !async function(t) {
async function n() {
const n = document.querySelector("ytd-watch-flexy"),
i = (null == n ? void 0 : n.hasAttribute("theater")) || !1,
a = (null == n ? void 0 : n.hasAttribute("fullscreen")) || !1,
o = await t.getStorage("settings.theaterMode"),
r = e$4(!1);
return i && !a && o && r
}
let i = await n();
setInterval((async () => {
await n() ? (document.body.setAttribute("data-mogul-theater-mode", ""), i || window.dispatchEvent(new Event("resize")), i = !0) : (i = !1, document.body.removeAttribute("data-mogul-theater-mode"));
}), 250);
}(j), async function(t) {
if (!window.ytcfg) return;
const e = {
key: window.ytcfg.get("INNERTUBE_API_KEY"),
authUser: window.ytcfg.get("SESSION_INDEX"),
pageId: window.ytcfg.get("DELEGATED_SESSION_ID"),
href: window.location.href,
context: window.ytcfg.get("INNERTUBE_CONTEXT")
};
n = await e$3(Object.assign(Object.assign({}, e), {
cookies: v(document.cookie)
})), i = await t.fetch("/auth/@me"), (!n.success || !i.meta.isSuccess || n.data.id !== (null === (a = i.body) || void 0 === a ? void 0 : a.sub)) && await t.fetch("/auth/login", e);
var n, i, a;
}(j), setInterval((() => {
const t = document.querySelector("ytd-live-chat-frame iframe");
if (t && !t.src.includes("QXZRwEzaeHNfDqtD")) {
const e = n();
t.src += `#QXZRwEzaeHNfDqtD=${encodeURIComponent(JSON.stringify(e))}`;
}
}), 1e3);
var L, O;