diff --git a/src/content/js/rpc.js b/src/content/js/rpc.js index 4cde140..20b9195 100644 --- a/src/content/js/rpc.js +++ b/src/content/js/rpc.js @@ -1,79 +1,87 @@ -(() => { - let Dispatcher, - lookupAsset, - lookupApp, - apps = {}; +window.addEventListener( + "load", + (() => { + let Dispatcher, + lookupAsset, + lookupApp, + apps = {}; - ArmCordRPC.listen(async (data) => { - msg = data; //already parsed - //console.log(msg); + ArmCordRPC.listen(async (data) => { + msg = data; //already parsed + //console.log(msg); - if (!Dispatcher) { - const wpRequire = window.webpackChunkdiscord_app.push([[Symbol()], {}, (x) => x]); - const cache = wpRequire.c; - window.webpackChunkdiscord_app.pop(); + if (!Dispatcher) { + const wpRequire = window.webpackChunkdiscord_app.push([[Symbol()], {}, (x) => x]); + const cache = wpRequire.c; + window.webpackChunkdiscord_app.pop(); - for (const id in cache) { - let mod = cache[id].exports; - mod = mod && (mod.Z ?? mod.ZP); + for (const id in cache) { + let mod = cache[id].exports; + for (const prop in mod) { + const candidate = mod[prop]; + if (candidate && candidate.register && candidate.wait) { + Dispatcher = candidate; + break; + } + } + if (Dispatcher) break; // make sure to exit outer loop as well + } - if (mod && mod.register && mod.wait) { - Dispatcher = mod; - break; + const factories = wpRequire.m; + for (const id in factories) { + if (factories[id].toString().includes("getAssetImage: size must === [number, number] for Twitch")) { + const mod = wpRequire(id); + + // fetchAssetIds + const _lookupAsset = Object.values(mod).find( + (e) => typeof e === "function" && e.toString().includes("APPLICATION_ASSETS_FETCH_SUCCESS") + ); + if (_lookupAsset) + lookupAsset = async (appId, name) => (await _lookupAsset(appId, [name, undefined]))[0]; + } + if (lookupAsset) break; + } + + for (const id in factories) { + if (factories[id].toString().includes("APPLICATION_RPC")) { + const mod = wpRequire(id); + + // fetchApplicationsRPC + const _lookupApp = Object.values(mod).find( + (e) => typeof e === "function" && e.toString().includes(",coverImage:") + ); + if (_lookupApp) + lookupApp = async (appId) => { + let socket = {}; + await _lookupApp(socket, appId); + return socket.application; + }; + } + if (lookupApp) break; } } - const factories = wpRequire.m; - for (const id in factories) { - if (factories[id].toString().includes("getAssetImage: size must === [number, number] for Twitch")) { - const mod = wpRequire(id); + if (msg.activity?.assets?.large_image) + msg.activity.assets.large_image = await lookupAsset( + msg.activity.application_id, + msg.activity.assets.large_image + ); + if (msg.activity?.assets?.small_image) + msg.activity.assets.small_image = await lookupAsset( + msg.activity.application_id, + msg.activity.assets.small_image + ); - const _lookupAsset = Object.values(mod).find( - (e) => typeof e === "function" && e.toString().includes("apply(") - ); - lookupAsset = async (appId, name) => (await _lookupAsset(appId, [name, undefined]))[0]; + // prevent errors when activity is null and let activity stop + if (msg.activity) { + const appId = msg.activity.application_id; + if (!apps[appId]) apps[appId] = await lookupApp(appId); - break; - } + const app = apps[appId]; + if (!msg.activity.name) msg.activity.name = app.name; } - for (const id in factories) { - if (factories[id].toString().includes(`e.application={`)) { - const mod = wpRequire(id); - - const _lookupApp = Object.values(mod).find( - (e) => typeof e === "function" && e.toString().includes(`e.application={`) - ); - lookupApp = async (appId) => { - let socket = {}; - await _lookupApp(socket, appId); - return socket.application; - }; - - break; - } - } - } - - if (msg.activity?.assets?.large_image) - msg.activity.assets.large_image = await lookupAsset( - msg.activity.application_id, - msg.activity.assets.large_image - ); - if (msg.activity?.assets?.small_image) - msg.activity.assets.small_image = await lookupAsset( - msg.activity.application_id, - msg.activity.assets.small_image - ); - - if (msg.activity) { - const appId = msg.activity.application_id; - if (!apps[appId]) apps[appId] = await lookupApp(appId); - - const app = apps[appId]; - if (!msg.activity.name) msg.activity.name = app.name; - } - - Dispatcher.dispatch({type: "LOCAL_ACTIVITY_UPDATE", ...msg}); // set RPC status - }); -})(); + Dispatcher.dispatch({type: "LOCAL_ACTIVITY_UPDATE", ...msg}); // set RPC status + }); + })() +); diff --git a/src/preload/bridge.ts b/src/preload/bridge.ts index 3a5c298..ef365b8 100644 --- a/src/preload/bridge.ts +++ b/src/preload/bridge.ts @@ -67,5 +67,6 @@ contextBridge.exposeInMainWorld("ArmCordRPC", { } }); ipcRenderer.on("rpc", (_event, data: object) => { + console.log(data); windowCallback(data); }); diff --git a/src/preload/preload.ts b/src/preload/preload.ts index b237eae..c46d440 100644 --- a/src/preload/preload.ts +++ b/src/preload/preload.ts @@ -73,7 +73,10 @@ sleep(5000).then(async () => { // Settings info version injection setInterval(() => { - const host = document.querySelector("nav > [class|=side] [class|=info]"); + // @ts-expect-error + const host = [...document.querySelectorAll('[class*="sidebar"] [class*="info"] [class*="line"]')].find((x) => + x.textContent.startsWith("Host ") + ); if (!host || host.querySelector("#ac-ver")) { return; } @@ -84,8 +87,8 @@ setInterval(() => { el.onclick = () => ipcRenderer.send("openSettingsWindow"); host.append(el); let advanced = document - .querySelector('[class*="socialLinks-"]')! - .parentElement!.querySelector( + .querySelector('[class*="socialLinks"]') + ?.parentElement?.querySelector( '[class*="header"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"] + [class*="item"]' ); if (!advanced) return;