Skip to content

Commit c1b54dc

Browse files
committed
fix: add a way to detect invalid URL
1 parent 980c770 commit c1b54dc

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vscord",
33
"displayName": "Discord Rich Presence",
4-
"version": "5.3.4",
4+
"version": "5.3.5",
55
"description": "Highly customizable Discord Rich Presence extension for Visual Studio Code",
66
"main": "dist/extension.js",
77
"repository": "github:LeonardSSH/vscord",

src/controller.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { type Disposable, type WindowState, debug, languages, window, workspace, commands } from "vscode";
22
import { type SetActivity, type SetActivityResponse, Client } from "@xhayper/discord-rpc";
3+
import type { GatewayActivityButton } from "discord-api-types/v10";
34
import { getApplicationId } from "./helpers/getApplicationId";
45
import { activity, onDiagnosticsChange } from "./activity";
56
import { StatusBarMode, editor } from "./editor";
7+
import { validURL } from "./helpers/validURL";
68
import { throttle } from "./helpers/throttle";
79
import { logError, logInfo } from "./logger";
810
import { CONFIG_KEYS } from "./constants";
@@ -175,6 +177,20 @@ export class RPCController {
175177
this.state.instance = true;
176178
if (!this.state || Object.keys(this.state).length === 0 || !this.canSendActivity)
177179
return void this.client.user?.clearActivity(process.pid);
180+
const filteredButton: GatewayActivityButton[] = [];
181+
this.state.buttons = (this.state.buttons ?? []).filter((button) => {
182+
const isValid = validURL(button.url);
183+
if (!isValid) filteredButton.push(button);
184+
return isValid;
185+
});
186+
187+
if (filteredButton.length > 0)
188+
logInfo(
189+
"[005]",
190+
"Invalid buttons!\n",
191+
filteredButton.map((button) => JSON.stringify(button, null, 2)).join("\n")
192+
);
193+
178194
return this.client.user?.setActivity(this.state, process.pid);
179195
}
180196

src/helpers/validURL.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://stackoverflow.com/a/5717133
2+
export function validURL(str: string): boolean {
3+
var pattern = new RegExp(
4+
"^(https?:\\/\\/)?" + // protocol
5+
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
6+
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
7+
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
8+
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
9+
"(\\#[-a-z\\d_]*)?$",
10+
"i"
11+
); // fragment locator
12+
return !!pattern.test(str);
13+
}

0 commit comments

Comments
 (0)