From 518e66c6c2a8dd0968120595de1fbdc5f798ce10 Mon Sep 17 00:00:00 2001 From: nin0 Date: Sun, 30 Mar 2025 05:12:50 -0400 Subject: [PATCH] init --- index.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ native.ts | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 index.ts create mode 100644 native.ts diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..ddd2e67 --- /dev/null +++ b/index.ts @@ -0,0 +1,68 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2025 nin0dev + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { Devs } from "@utils/constants"; +import definePlugin from "@utils/types"; +import { findByPropsLazy } from "@webpack"; +import { Toasts } from "@webpack/common"; + +const SpotifySocket = findByPropsLazy("getActiveSocketAndDevice"); +const SpotifyAPI = findByPropsLazy("vcSpotifyMarker"); + +function queueSong(id: string) { + const { socket } = SpotifySocket.getActiveSocketAndDevice(); + if (!socket) return Toasts.show({ + type: Toasts.Type.FAILURE, + message: "Make sure that Spotify is running before queuing a song", + id: Toasts.genId() + }); + + try { + SpotifyAPI.post(socket.accountId, socket.accessToken, { + url: "https://api.spotify.com/v1/me/player/queue", + query: { + uri: `spotify:track:${id}` + } + }); + Toasts.show({ + type: Toasts.Type.SUCCESS, + message: "Song queued", + id: Toasts.genId() + }); + } + catch (e) { + console.error(e); + Toasts.show({ + type: Toasts.Type.FAILURE, + message: "An error occurred, check console", + id: Toasts.genId() + }); + } +} + +export default definePlugin({ + name: "SpotifyAddToQueue", + description: "Vencord Virus", + authors: [Devs.nin0dev], + iframeMessageListener: e => { + try { + const songIDMatch = (e.data as string).match(/vc-spotifyaddtoqueue__([a-zA-Z0-9]{0,200})/); + if (!songIDMatch) return; + + const songID = songIDMatch[1]; + queueSong(songID); + } + catch { + return; + } + }, + start() { + window.addEventListener("message", this.iframeMessageListener); + }, + stop() { + window.removeEventListener("message", this.iframeMessageListener); + } +}); diff --git a/native.ts b/native.ts new file mode 100644 index 0000000..0ad7334 --- /dev/null +++ b/native.ts @@ -0,0 +1,41 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2025 nin0dev + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { RendererSettings } from "@main/settings"; +import { app } from "electron"; + +app.on("browser-window-created", (_, win) => { + win.webContents.on("frame-created", (_, { frame }) => { + frame?.once("dom-ready", () => { + if (frame.url.startsWith("https://open.spotify.com/embed/")) { + const settings = RendererSettings.store.plugins?.SpotifyAddToQueue; + if (!settings?.enabled) return; + + frame.executeJavaScript(` + const interval = setInterval(() => { + const actions = document.querySelector("[class^='PlayerControlsShort_playerControlsWrapper__']"); + if(actions) { + const addToQueueButton = document.createElement("button"); + Object.assign(addToQueueButton.style, { + position: "relative", + top: "16px", + left: "0px" + }); + addToQueueButton.title = "Add to queue"; + addToQueueButton.addEventListener("click", () => { + window.top.postMessage("vc-spotifyaddtoqueue__" + location.href.match(/https:\\/\\/open\\.spotify.com\\/embed\\/track\\/([a-zA-Z0-9]{0,200})\\?/ )[1], "*"); + }); + addToQueueButton.innerHTML = ''; + actions.insertBefore(addToQueueButton, actions.firstChild); + clearInterval(interval); + } + }, 100); + `); + } + }); + }); +}); +