OpenInApp: Add Epic Games; properly respect settings

This commit is contained in:
V 2023-06-30 15:16:42 +02:00
parent 73354973a3
commit b592defaaf
No known key found for this signature in database
GPG Key ID: A1DC0CFB5615D905
2 changed files with 24 additions and 6 deletions

@ -31,7 +31,8 @@ export const ALLOWED_PROTOCOLS = [
"https:", "https:",
"http:", "http:",
"steam:", "steam:",
"spotify:" "spotify:",
"com.epicgames.launcher:",
]; ];
export const IS_VANILLA = /* @__PURE__ */ process.argv.includes("--vanilla"); export const IS_VANILLA = /* @__PURE__ */ process.argv.includes("--vanilla");

@ -25,6 +25,7 @@ import { MouseEvent } from "react";
const ShortUrlMatcher = /^https:\/\/(spotify\.link|s\.team)\/.+$/; const ShortUrlMatcher = /^https:\/\/(spotify\.link|s\.team)\/.+$/;
const SpotifyMatcher = /^https:\/\/open\.spotify\.com\/(track|album|artist|playlist|user)\/(.+)(?:\?.+?)?$/; const SpotifyMatcher = /^https:\/\/open\.spotify\.com\/(track|album|artist|playlist|user)\/(.+)(?:\?.+?)?$/;
const SteamMatcher = /^https:\/\/(steamcommunity\.com|(?:help|store)\.steampowered\.com)\/.+$/; const SteamMatcher = /^https:\/\/(steamcommunity\.com|(?:help|store)\.steampowered\.com)\/.+$/;
const EpicMatcher = /^https:\/\/store\.epicgames\.com\/(.+)$/;
const settings = definePluginSettings({ const settings = definePluginSettings({
spotify: { spotify: {
@ -36,12 +37,17 @@ const settings = definePluginSettings({
type: OptionType.BOOLEAN, type: OptionType.BOOLEAN,
description: "Open Steam links in the Steam app", description: "Open Steam links in the Steam app",
default: true, default: true,
},
epic: {
type: OptionType.BOOLEAN,
description: "Open Epic Games links in the Epic Games Launcher",
default: true,
} }
}); });
export default definePlugin({ export default definePlugin({
name: "OpenInApp", name: "OpenInApp",
description: "Open Spotify and Steam URLs in the Spotify and Steam app instead of your Browser", description: "Open Spotify, Steam and Epic Games URLs in their respective apps instead of your browser",
authors: [Devs.Ven], authors: [Devs.Ven],
settings, settings,
@ -56,7 +62,7 @@ export default definePlugin({
// Make Spotify profile activity links open in app on web // Make Spotify profile activity links open in app on web
{ {
find: "WEB_OPEN(", find: "WEB_OPEN(",
predicate: () => !IS_DISCORD_DESKTOP, predicate: () => !IS_DISCORD_DESKTOP && settings.store.spotify,
replacement: { replacement: {
match: /\i\.\i\.isProtocolRegistered\(\)(.{0,100})window.open/g, match: /\i\.\i\.isProtocolRegistered\(\)(.{0,100})window.open/g,
replace: "true$1VencordNative.native.openExternal" replace: "true$1VencordNative.native.openExternal"
@ -100,7 +106,6 @@ export default definePlugin({
if (!SteamMatcher.test(url)) break steam; if (!SteamMatcher.test(url)) break steam;
VencordNative.native.openExternal(`steam://openurl/${url}`); VencordNative.native.openExternal(`steam://openurl/${url}`);
event.preventDefault(); event.preventDefault();
// Steam does not focus itself so show a toast so it's slightly less confusing // Steam does not focus itself so show a toast so it's slightly less confusing
@ -108,6 +113,18 @@ export default definePlugin({
return true; return true;
} }
epic: {
if (!settings.store.epic) break epic;
const match = EpicMatcher.exec(url);
if (!match) break epic;
VencordNative.native.openExternal(`com.epicgames.launcher://store/${match[1]}`);
event.preventDefault();
return true;
}
// in case short url didn't end up being something we can handle // in case short url didn't end up being something we can handle
if (event.defaultPrevented) { if (event.defaultPrevented) {
window.open(url, "_blank"); window.open(url, "_blank");
@ -118,10 +135,10 @@ export default definePlugin({
}, },
handleAccountView(event: { preventDefault(): void; }, platformType: string, userId: string) { handleAccountView(event: { preventDefault(): void; }, platformType: string, userId: string) {
if (platformType === "spotify") { if (platformType === "spotify" && settings.store.spotify) {
VencordNative.native.openExternal(`spotify:user:${userId}`); VencordNative.native.openExternal(`spotify:user:${userId}`);
event.preventDefault(); event.preventDefault();
} else if (platformType === "steam") { } else if (platformType === "steam" && settings.store.steam) {
VencordNative.native.openExternal(`steam://openurl/https://steamcommunity.com/profiles/${userId}`); VencordNative.native.openExternal(`steam://openurl/https://steamcommunity.com/profiles/${userId}`);
showToast("Opened link in Steam", Toasts.Type.SUCCESS); showToast("Opened link in Steam", Toasts.Type.SUCCESS);
event.preventDefault(); event.preventDefault();