-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.tsx
207 lines (177 loc) · 5.79 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { addAccessory, removeAccessory } from "@api/MessageAccessories";
import { definePluginSettings } from "@api/Settings";
import { getUserSettingLazy } from "@api/UserSettings";
import ErrorBoundary from "@components/ErrorBoundary";
import definePlugin, { OptionType } from "@utils/types";
import { React, Text } from "@webpack/common";
import type { Message } from "discord-types/general";
import { knownHosts } from "./knownHosts";
import { knownVideoIds } from "./knownVideoIDs";
const MessageDisplayCompact = getUserSettingLazy(
"textAndImages",
"messageDisplayCompact"
)!;
const settings = definePluginSettings({
customLinks: {
description: "Custom links to check for (separated by commas)",
type: OptionType.STRING,
default: "",
restartNeeded: true,
},
customVideoIds: {
description:
"Custom YouTube video IDs to check for (separated by commas)",
type: OptionType.STRING,
default: "",
restartNeeded: true,
},
});
function isPotentialRickroll(url: string): boolean {
try {
const parsedUrl = new URL(url);
const hostname = parsedUrl.hostname.replace("www.", "");
const customLinks = settings.store.customLinks
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const customVideoIDs = settings.store.customVideoIds
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (customLinks.some((link) => parsedUrl.href.includes(link))) {
return true;
}
if (hostname === "youtube.com" || hostname === "youtu.be") {
let videoID = "";
if (hostname === "youtube.com") {
videoID =
parsedUrl.searchParams.get("v") ||
parsedUrl.searchParams.get("V") ||
"";
} else if (hostname === "youtu.be") {
videoID = parsedUrl.pathname.slice(1);
}
const knownVideoIDs = [...knownVideoIds, ...customVideoIDs];
if (knownVideoIDs.includes(videoID)) {
return true;
}
}
if (knownHosts.includes(hostname)) {
return true;
}
} catch (e) {
// Invalid URL, ignore :trolley:
}
return false;
}
function extractUrls(content: string): string[] {
const urls: string[] = [];
const markdownLinkRegex = /\[.*?\]\((<)?(https?:\/\/[^\s>]+)(>)?\)/g;
const urlRegex = /https?:\/\/[^\s<]+/g;
const maskedUrlRegex = /<(https?:\/\/[^\s>]+)>/g;
let match: RegExpExecArray | null;
while ((match = markdownLinkRegex.exec(content)) !== null) {
urls.push(match[2]);
}
while ((match = urlRegex.exec(content)) !== null) {
urls.push(match[0]);
}
while ((match = maskedUrlRegex.exec(content)) !== null) {
urls.push(match[1]);
}
return urls;
}
function RickrollWarningAccessory({ message }: { message: Message }) {
const urls = extractUrls(message.content);
if (urls.length === 0) return null;
for (const url of urls) {
const isCustom = isCustomRickroll(url);
if (isPotentialRickroll(url) || isCustom) {
return (
<ErrorBoundary>
<RickrollWarning message={message} isCustom={isCustom} />
</ErrorBoundary>
);
}
}
return null;
}
function isCustomRickroll(url: string): boolean {
try {
const parsedUrl = new URL(url);
const customLinks = settings.store.customLinks
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const customVideoIDs = settings.store.customVideoIds
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (customLinks.some((link) => parsedUrl.href.includes(link))) {
return true;
}
const hostname = parsedUrl.hostname.replace("www.", "");
if (hostname === "youtube.com" || hostname === "youtu.be") {
let videoID = "";
if (hostname === "youtube.com") {
videoID = parsedUrl.searchParams.get("v") || "";
} else if (hostname === "youtu.be") {
videoID = parsedUrl.pathname.slice(1);
}
if (customVideoIDs.includes(videoID)) {
return true;
}
}
} catch (e) {
// Invalid URL, ignore :trolley: (could probably merge this)
}
return false;
}
function RickrollWarning({
message,
isCustom,
}: {
message: Message;
isCustom: boolean;
}) {
const compact = MessageDisplayCompact.useSetting();
return (
<div>
<Text color="text-danger" variant="text-xs/semibold">
⚠️ This link is{" "}
{isCustom
? "matching one of your filters for rickrolls."
: "a known rickroll."}
</Text>
</div>
);
}
export default definePlugin({
name: "AntiRickroll",
description:
"Warns you of potential Rickrolls in messages, including masked links (supports custom rules)",
authors: [{ name: "ryanamay", id: 1262793452236570667n }],
dependencies: ["MessageAccessoriesAPI", "UserSettingsAPI"],
settings,
start() {
addAccessory(
"rickrollWarning",
(props: Record<string, any>) => {
return (
<RickrollWarningAccessory
message={props.message as Message}
/>
);
},
4
);
},
stop() {
removeAccessory("rickrollWarning");
},
});