OpenInApp: Support steam; integrate with SpotifyControls & ShowConnections
This commit is contained in:
parent
e12c0e546c
commit
73354973a3
|
@ -58,4 +58,10 @@ export default {
|
||||||
getVersions: () => process.versions as Partial<NodeJS.ProcessVersions>,
|
getVersions: () => process.versions as Partial<NodeJS.ProcessVersions>,
|
||||||
openExternal: (url: string) => invoke<void>(IpcEvents.OPEN_EXTERNAL, url)
|
openExternal: (url: string) => invoke<void>(IpcEvents.OPEN_EXTERNAL, url)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
pluginHelpers: {
|
||||||
|
OpenInApp: {
|
||||||
|
resolveRedirect: (url: string) => invoke<string>(IpcEvents.OPEN_IN_APP__RESOLVE_REDIRECT, url),
|
||||||
|
},
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import "./updater";
|
import "./updater";
|
||||||
|
import "./ipcPlugins";
|
||||||
|
|
||||||
import { debounce } from "@utils/debounce";
|
import { debounce } from "@utils/debounce";
|
||||||
import { IpcEvents } from "@utils/IpcEvents";
|
import { IpcEvents } from "@utils/IpcEvents";
|
||||||
|
|
46
src/main/ipcPlugins.ts
Normal file
46
src/main/ipcPlugins.ts
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Vencord, a modification for Discord's desktop app
|
||||||
|
* Copyright (c) 2023 Vendicated and contributors
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { IpcEvents } from "@utils/IpcEvents";
|
||||||
|
import { ipcMain } from "electron";
|
||||||
|
import { request } from "https";
|
||||||
|
|
||||||
|
// #region OpenInApp
|
||||||
|
// These links don't support CORS, so this has to be native
|
||||||
|
const validRedirectUrls = /^https:\/\/(spotify\.link|s\.team)\/.+$/;
|
||||||
|
|
||||||
|
function getRedirect(url: string) {
|
||||||
|
return new Promise<string>((resolve, reject) => {
|
||||||
|
const req = request(new URL(url), { method: "HEAD" }, res => {
|
||||||
|
resolve(
|
||||||
|
res.headers.location
|
||||||
|
? getRedirect(res.headers.location)
|
||||||
|
: url
|
||||||
|
);
|
||||||
|
});
|
||||||
|
req.on("error", reject);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ipcMain.handle(IpcEvents.OPEN_IN_APP__RESOLVE_REDIRECT, async (_, url: string) => {
|
||||||
|
if (!validRedirectUrls.test(url)) return url;
|
||||||
|
|
||||||
|
return getRedirect(url);
|
||||||
|
});
|
||||||
|
// #endregion
|
|
@ -16,22 +16,41 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { definePluginSettings } from "@api/Settings";
|
||||||
import { Devs } from "@utils/constants";
|
import { Devs } from "@utils/constants";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
|
import { showToast, Toasts } from "@webpack/common";
|
||||||
|
import { MouseEvent } from "react";
|
||||||
|
|
||||||
const SpotifyMatcher = /https:\/\/open\.spotify\.com\/(track|album|artist|playlist|user)\/([^S]+)/;
|
const ShortUrlMatcher = /^https:\/\/(spotify\.link|s\.team)\/.+$/;
|
||||||
|
const SpotifyMatcher = /^https:\/\/open\.spotify\.com\/(track|album|artist|playlist|user)\/(.+)(?:\?.+?)?$/;
|
||||||
|
const SteamMatcher = /^https:\/\/(steamcommunity\.com|(?:help|store)\.steampowered\.com)\/.+$/;
|
||||||
|
|
||||||
|
const settings = definePluginSettings({
|
||||||
|
spotify: {
|
||||||
|
type: OptionType.BOOLEAN,
|
||||||
|
description: "Open Spotify links in the Spotify app",
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
|
steam: {
|
||||||
|
type: OptionType.BOOLEAN,
|
||||||
|
description: "Open Steam links in the Steam app",
|
||||||
|
default: true,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default definePlugin({
|
export default definePlugin({
|
||||||
name: "OpenInApp",
|
name: "OpenInApp",
|
||||||
description: "Open spotify URLs in app",
|
description: "Open Spotify and Steam URLs in the Spotify and Steam app instead of your Browser",
|
||||||
authors: [Devs.Ven],
|
authors: [Devs.Ven],
|
||||||
|
settings,
|
||||||
|
|
||||||
patches: [
|
patches: [
|
||||||
{
|
{
|
||||||
find: '"MaskedLinkStore"',
|
find: '"MaskedLinkStore"',
|
||||||
replacement: {
|
replacement: {
|
||||||
match: /return ((\i)\.apply\(this,arguments\))(?=\}function \i.{0,200}\.trusted)/,
|
match: /return ((\i)\.apply\(this,arguments\))(?=\}function \i.{0,200}\.trusted)/,
|
||||||
replace: "return $self.handleLink(...arguments)||$1"
|
replace: "return $self.handleLink(...arguments).then(handled => handled || $1)"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// Make Spotify profile activity links open in app on web
|
// Make Spotify profile activity links open in app on web
|
||||||
|
@ -52,23 +71,60 @@ export default definePlugin({
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
handleLink(data: { href: string; }, event: MouseEvent) {
|
async handleLink(data: { href: string; }, event: MouseEvent) {
|
||||||
if (!data) return;
|
if (!data) return false;
|
||||||
|
|
||||||
const match = SpotifyMatcher.exec(data.href);
|
let url = data.href;
|
||||||
if (!match) return;
|
if (!IS_WEB && ShortUrlMatcher.test(url)) {
|
||||||
|
event.preventDefault();
|
||||||
|
// CORS jumpscare
|
||||||
|
url = await VencordNative.pluginHelpers.OpenInApp.resolveRedirect(url);
|
||||||
|
}
|
||||||
|
|
||||||
const [, type, id] = match;
|
spotify: {
|
||||||
VencordNative.native.openExternal(`spotify:${type}:${id}`);
|
if (!settings.store.spotify) break spotify;
|
||||||
event.preventDefault();
|
|
||||||
|
|
||||||
return Promise.resolve();
|
const match = SpotifyMatcher.exec(url);
|
||||||
|
if (!match) break spotify;
|
||||||
|
|
||||||
|
const [, type, id] = match;
|
||||||
|
VencordNative.native.openExternal(`spotify:${type}:${id}`);
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
steam: {
|
||||||
|
if (!settings.store.steam) break steam;
|
||||||
|
|
||||||
|
if (!SteamMatcher.test(url)) break steam;
|
||||||
|
|
||||||
|
VencordNative.native.openExternal(`steam://openurl/${url}`);
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
// Steam does not focus itself so show a toast so it's slightly less confusing
|
||||||
|
showToast("Opened link in Steam", Toasts.Type.SUCCESS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// in case short url didn't end up being something we can handle
|
||||||
|
if (event.defaultPrevented) {
|
||||||
|
window.open(url, "_blank");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
handleAccountView(event: MouseEvent, platformType: string, otherUserId: string) {
|
handleAccountView(event: { preventDefault(): void; }, platformType: string, userId: string) {
|
||||||
if (platformType !== "spotify") return;
|
if (platformType === "spotify") {
|
||||||
|
VencordNative.native.openExternal(`spotify:user:${userId}`);
|
||||||
VencordNative.native.openExternal(`spotify:user:${otherUserId}`);
|
event.preventDefault();
|
||||||
event.preventDefault();
|
} else if (platformType === "steam") {
|
||||||
|
VencordNative.native.openExternal(`steam://openurl/https://steamcommunity.com/profiles/${userId}`);
|
||||||
|
showToast("Opened link in Steam", Toasts.Type.SUCCESS);
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -147,6 +147,13 @@ function CompactConnectionComponent({ connection, theme }: { connection: Connect
|
||||||
className="vc-user-connection"
|
className="vc-user-connection"
|
||||||
href={url}
|
href={url}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
|
onClick={e => {
|
||||||
|
if (Vencord.Plugins.isPluginEnabled("OpenInApp")) {
|
||||||
|
const OpenInApp = Vencord.Plugins.plugins.OpenInApp as any as typeof import("../openInApp").default;
|
||||||
|
// handleLink will .preventDefault() if applicable
|
||||||
|
OpenInApp.handleLink(e.currentTarget, e);
|
||||||
|
}
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{img}
|
{img}
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -23,6 +23,7 @@ import { Flex } from "@components/Flex";
|
||||||
import { ImageIcon, LinkIcon, OpenExternalIcon } from "@components/Icons";
|
import { ImageIcon, LinkIcon, OpenExternalIcon } from "@components/Icons";
|
||||||
import { Link } from "@components/Link";
|
import { Link } from "@components/Link";
|
||||||
import { debounce } from "@utils/debounce";
|
import { debounce } from "@utils/debounce";
|
||||||
|
import { openImageModal } from "@utils/discord";
|
||||||
import { classes, copyWithToast } from "@utils/misc";
|
import { classes, copyWithToast } from "@utils/misc";
|
||||||
import { ContextMenu, FluxDispatcher, Forms, Menu, React, useEffect, useState, useStateFromStores } from "@webpack/common";
|
import { ContextMenu, FluxDispatcher, Forms, Menu, React, useEffect, useState, useStateFromStores } from "@webpack/common";
|
||||||
|
|
||||||
|
@ -231,7 +232,7 @@ function AlbumContextMenu({ track }: { track: Track; }) {
|
||||||
id="view-cover"
|
id="view-cover"
|
||||||
label="View Album Cover"
|
label="View Album Cover"
|
||||||
// trolley
|
// trolley
|
||||||
action={() => (Vencord.Plugins.plugins.ViewIcons as any).openImage(track.album.image.url)}
|
action={() => openImageModal(track.album.image.url)}
|
||||||
icon={ImageIcon}
|
icon={ImageIcon}
|
||||||
/>
|
/>
|
||||||
<Menu.MenuControlItem
|
<Menu.MenuControlItem
|
||||||
|
|
|
@ -89,7 +89,7 @@ export const SpotifyStore = proxyLazy(() => {
|
||||||
public isSettingPosition = false;
|
public isSettingPosition = false;
|
||||||
|
|
||||||
public openExternal(path: string) {
|
public openExternal(path: string) {
|
||||||
const url = Settings.plugins.SpotifyControls.useSpotifyUris
|
const url = Settings.plugins.SpotifyControls.useSpotifyUris || Vencord.Plugins.isPluginEnabled("OpenInApp")
|
||||||
? "spotify:" + path.replaceAll("/", (_, idx) => idx === 0 ? "" : ":")
|
? "spotify:" + path.replaceAll("/", (_, idx) => idx === 0 ? "" : ":")
|
||||||
: "https://open.spotify.com" + path;
|
: "https://open.spotify.com" + path;
|
||||||
|
|
||||||
|
|
|
@ -30,4 +30,6 @@ export const enum IpcEvents {
|
||||||
UPDATE = "VencordUpdate",
|
UPDATE = "VencordUpdate",
|
||||||
BUILD = "VencordBuild",
|
BUILD = "VencordBuild",
|
||||||
OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor",
|
OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor",
|
||||||
|
|
||||||
|
OPEN_IN_APP__RESOLVE_REDIRECT = "VencordOIAResolveRedirect",
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,17 @@ export const Toasts = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show a simple toast. If you need more options, use Toasts.show manually
|
||||||
|
*/
|
||||||
|
export function showToast(message: string, type = ToastType.MESSAGE) {
|
||||||
|
Toasts.show({
|
||||||
|
id: Toasts.genId(),
|
||||||
|
message,
|
||||||
|
type
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export const UserUtils = {
|
export const UserUtils = {
|
||||||
fetchUser: findByCodeLazy(".USER(", "getUser") as (id: string) => Promise<User>,
|
fetchUser: findByCodeLazy(".USER(", "getUser") as (id: string) => Promise<User>,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue