mirror of
https://github.com/Vendicated/Vencord.git
synced 2024-09-20 06:30:35 +00:00
Compare commits
12 commits
56a21d1d8d
...
55bbccb0b4
Author | SHA1 | Date | |
---|---|---|---|
|
55bbccb0b4 | ||
|
c7e5295da0 | ||
|
b542ee48a7 | ||
|
e7a1be6512 | ||
|
ad03ee3fb5 | ||
|
5f5e9c9558 | ||
|
a24967ed2f | ||
|
7c62039a09 | ||
|
d96ff463f5 | ||
|
99cf950b95 | ||
|
332e434a74 | ||
|
4dfaa87961 |
6 changed files with 295 additions and 81 deletions
5
src/plugins/fullSearchContext/README.md
Normal file
5
src/plugins/fullSearchContext/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# FullSearchContext
|
||||
|
||||
Makes the message context menu in message search results have all options you'd expect.
|
||||
|
||||
![](https://github.com/user-attachments/assets/472d1327-3935-44c7-b7c4-0978b5348550)
|
82
src/plugins/fullSearchContext/index.tsx
Normal file
82
src/plugins/fullSearchContext/index.tsx
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* 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 { migratePluginSettings } from "@api/Settings";
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin from "@utils/types";
|
||||
import { findByPropsLazy } from "@webpack";
|
||||
import { ChannelStore, ContextMenuApi, i18n, UserStore } from "@webpack/common";
|
||||
import { Message } from "discord-types/general";
|
||||
import type { MouseEvent } from "react";
|
||||
|
||||
const { useMessageMenu } = findByPropsLazy("useMessageMenu");
|
||||
|
||||
function MessageMenu({ message, channel, onHeightUpdate }) {
|
||||
const canReport = message.author &&
|
||||
!(message.author.id === UserStore.getCurrentUser().id || message.author.system);
|
||||
|
||||
return useMessageMenu({
|
||||
navId: "message-actions",
|
||||
ariaLabel: i18n.Messages.MESSAGE_UTILITIES_A11Y_LABEL,
|
||||
|
||||
message,
|
||||
channel,
|
||||
canReport,
|
||||
onHeightUpdate,
|
||||
onClose: () => ContextMenuApi.closeContextMenu(),
|
||||
|
||||
textSelection: "",
|
||||
favoriteableType: null,
|
||||
favoriteableId: null,
|
||||
favoriteableName: null,
|
||||
itemHref: void 0,
|
||||
itemSrc: void 0,
|
||||
itemSafeSrc: void 0,
|
||||
itemTextContent: void 0,
|
||||
});
|
||||
}
|
||||
|
||||
migratePluginSettings("FullSearchContext", "SearchReply");
|
||||
export default definePlugin({
|
||||
name: "FullSearchContext",
|
||||
description: "Makes the message context menu in message search results have all options you'd expect",
|
||||
authors: [Devs.Ven, Devs.Aria],
|
||||
|
||||
patches: [{
|
||||
find: "onClick:this.handleMessageClick,",
|
||||
replacement: {
|
||||
match: /this(?=\.handleContextMenu\(\i,\i\))/,
|
||||
replace: "$self"
|
||||
}
|
||||
}],
|
||||
|
||||
handleContextMenu(event: MouseEvent, message: Message) {
|
||||
const channel = ChannelStore.getChannel(message.channel_id);
|
||||
if (!channel) return;
|
||||
|
||||
event.stopPropagation();
|
||||
|
||||
ContextMenuApi.openContextMenu(event, contextMenuProps =>
|
||||
<MessageMenu
|
||||
message={message}
|
||||
channel={channel}
|
||||
onHeightUpdate={contextMenuProps.onHeightUpdate}
|
||||
/>
|
||||
);
|
||||
}
|
||||
});
|
|
@ -1,6 +0,0 @@
|
|||
# SearchReply
|
||||
|
||||
Adds a reply button to search results.
|
||||
|
||||
![the plugin in action](https://github.com/Vendicated/Vencord/assets/45497981/07e741d3-0f97-4e5c-82b0-80712ecf2cbb)
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
* 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 { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
|
||||
import { ReplyIcon } from "@components/Icons";
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin from "@utils/types";
|
||||
import { findByCodeLazy } from "@webpack";
|
||||
import { ChannelStore, i18n, Menu, PermissionsBits, PermissionStore, SelectedChannelStore } from "@webpack/common";
|
||||
import { Message } from "discord-types/general";
|
||||
|
||||
|
||||
const replyToMessage = findByCodeLazy(".TEXTAREA_FOCUS)", "showMentionToggle:");
|
||||
|
||||
const messageContextMenuPatch: NavContextMenuPatchCallback = (children, { message }: { message: Message; }) => {
|
||||
// make sure the message is in the selected channel
|
||||
if (SelectedChannelStore.getChannelId() !== message.channel_id) return;
|
||||
const channel = ChannelStore.getChannel(message?.channel_id);
|
||||
if (!channel) return;
|
||||
if (channel.guild_id && !PermissionStore.can(PermissionsBits.SEND_MESSAGES, channel)) return;
|
||||
|
||||
// dms and group chats
|
||||
const dmGroup = findGroupChildrenByChildId("pin", children);
|
||||
if (dmGroup && !dmGroup.some(child => child?.props?.id === "reply")) {
|
||||
const pinIndex = dmGroup.findIndex(c => c?.props.id === "pin");
|
||||
dmGroup.splice(pinIndex + 1, 0, (
|
||||
<Menu.MenuItem
|
||||
id="reply"
|
||||
label={i18n.Messages.MESSAGE_ACTION_REPLY}
|
||||
icon={ReplyIcon}
|
||||
action={(e: React.MouseEvent) => replyToMessage(channel, message, e)}
|
||||
/>
|
||||
));
|
||||
return;
|
||||
}
|
||||
|
||||
// servers
|
||||
const serverGroup = findGroupChildrenByChildId("mark-unread", children);
|
||||
if (serverGroup && !serverGroup.some(child => child?.props?.id === "reply")) {
|
||||
serverGroup.unshift((
|
||||
<Menu.MenuItem
|
||||
id="reply"
|
||||
label={i18n.Messages.MESSAGE_ACTION_REPLY}
|
||||
icon={ReplyIcon}
|
||||
action={(e: React.MouseEvent) => replyToMessage(channel, message, e)}
|
||||
/>
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export default definePlugin({
|
||||
name: "SearchReply",
|
||||
description: "Adds a reply button to search results",
|
||||
authors: [Devs.Aria],
|
||||
contextMenus: {
|
||||
"message": messageContextMenuPatch
|
||||
}
|
||||
});
|
3
src/plugins/svgEmbed/README.md
Normal file
3
src/plugins/svgEmbed/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# SVGEmbed
|
||||
|
||||
Makes SVG files which are uploaded directly to Discord or linked via `discord.com`/`cdn.discordapp.com` embed like normal images.
|
205
src/plugins/svgEmbed/index.ts
Normal file
205
src/plugins/svgEmbed/index.ts
Normal file
|
@ -0,0 +1,205 @@
|
|||
/*
|
||||
* 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 { definePluginSettings } from "@api/Settings";
|
||||
import { Devs } from "@utils/constants";
|
||||
import definePlugin, { OptionType } from "@utils/types";
|
||||
import { FluxDispatcher } from "@webpack/common";
|
||||
import Message from "discord-types/general/Message";
|
||||
|
||||
const EMBED_SUPPRESSED = 1 << 2;
|
||||
|
||||
const MAX_EMBEDS_PER_MESSAGE = 5;
|
||||
const MIN_SVG_WIDTH = 400;
|
||||
const MIN_SVG_HEIGHT = 350;
|
||||
// Limit the size to prevent lag when parsing big files
|
||||
const MAX_SVG_SIZE_MB = 10;
|
||||
|
||||
const URL_REGEX = new RegExp(
|
||||
/(?<!<)https?:\/\/(?:(?:canary|ptb)?\.?discord\.com|(?:cdn)\.?discordapp\.(?:com))\/[-a-zA-Z0-9:%_+.~/=]+\.svg[-a-zA-Z0-9:%_+.&?#=]*(?!>)/g,
|
||||
);
|
||||
|
||||
// Cache to avoid excessive requests in component updates
|
||||
const FileSizeCache: Map<string, boolean> = new Map();
|
||||
async function checkFileSize(url: string) {
|
||||
if (FileSizeCache.has(url)) {
|
||||
return FileSizeCache.get(url);
|
||||
}
|
||||
|
||||
let belowMaxSize = false;
|
||||
try {
|
||||
// We cannot check Content-Length due to Access-Control-Expose-Headers.
|
||||
// Instead, we request 1 byte past the size limit, and see if it succeeds.
|
||||
const res = await fetch(url, {
|
||||
method: "HEAD",
|
||||
headers: {
|
||||
Range: `bytes=${MAX_SVG_SIZE_MB * 1024 * 1024}-${MAX_SVG_SIZE_MB * 1024 * 1024 + 1}`
|
||||
}
|
||||
});
|
||||
|
||||
if (res.status === 416 /* Range Not Satisfiable */) {
|
||||
belowMaxSize = true;
|
||||
}
|
||||
} catch (ex) { }
|
||||
|
||||
FileSizeCache.set(url, belowMaxSize);
|
||||
return belowMaxSize;
|
||||
}
|
||||
|
||||
async function getSVGDimensions(svgUrl: string) {
|
||||
let width = 0, height = 0;
|
||||
let svgData: string;
|
||||
|
||||
try {
|
||||
const res = await fetch(svgUrl);
|
||||
svgData = await res.text();
|
||||
} catch (ex) {
|
||||
return { width, height };
|
||||
}
|
||||
|
||||
const svgElement = new DOMParser().parseFromString(svgData, "image/svg+xml").documentElement as unknown as SVGSVGElement;
|
||||
|
||||
// Return 0,0 on error, so that the renderer falls back to displaying the raw content
|
||||
const errorNode = svgElement.querySelector("parsererror");
|
||||
if (errorNode) {
|
||||
return { width, height };
|
||||
}
|
||||
|
||||
if (svgElement.width && svgElement.height && svgElement.width.baseVal.unitType === 1 && svgElement.height.baseVal.unitType === 1) {
|
||||
width = svgElement.width.baseVal.value;
|
||||
height = svgElement.height.baseVal.value;
|
||||
} else {
|
||||
width = svgElement.viewBox.baseVal.width;
|
||||
height = svgElement.viewBox.baseVal.height;
|
||||
}
|
||||
|
||||
// If the dimensions are below the minimum values,
|
||||
// scale them up by the smallest integer which makes at least 1 of them exceed it
|
||||
if (width < MIN_SVG_WIDTH && height < MIN_SVG_HEIGHT) {
|
||||
const multiplier = Math.ceil(Math.min(MIN_SVG_WIDTH / width, MIN_SVG_HEIGHT / height));
|
||||
width *= multiplier;
|
||||
height *= multiplier;
|
||||
}
|
||||
|
||||
return { width, height };
|
||||
}
|
||||
|
||||
const settings = definePluginSettings({
|
||||
embedLinks: {
|
||||
type: OptionType.BOOLEAN,
|
||||
description: "Embed SVG links hosted under discord.com and cdn.discordapp.com",
|
||||
default: true,
|
||||
restartNeeded: true
|
||||
},
|
||||
});
|
||||
|
||||
export default definePlugin({
|
||||
name: "SVGEmbed",
|
||||
description: "Makes SVG files embed as images.",
|
||||
authors: [Devs.amia, Devs.nakoyasha],
|
||||
settings: settings,
|
||||
|
||||
patches: [
|
||||
{
|
||||
find: "png|jpe?g|webp|gif|",
|
||||
replacement: {
|
||||
match: /(?<=png\|jpe\?g\|webp\|gif\|)/,
|
||||
replace: "svg|"
|
||||
}
|
||||
},
|
||||
{
|
||||
find: ".Messages.REMOVE_ATTACHMENT_BODY",
|
||||
replacement: [
|
||||
{
|
||||
match: /(?<=renderAttachments\(\i\){)/,
|
||||
replace: "$self.processAttachments(arguments[0]);"
|
||||
},
|
||||
{
|
||||
match: /(?<=renderEmbeds\(\i\){)/,
|
||||
predicate: () => settings.store.embedLinks,
|
||||
replace: "$self.processEmbeds(arguments[0]);"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
async processAttachments(message: Message) {
|
||||
let shouldUpdateMessage = false;
|
||||
|
||||
const toProcess = message.attachments.filter(x => x.content_type?.startsWith("image/svg+xml") && x.width == null && x.height == null);
|
||||
for (const attachment of toProcess) {
|
||||
if (attachment.size / 1024 / 1024 > MAX_SVG_SIZE_MB) continue;
|
||||
|
||||
const { width, height } = await getSVGDimensions(attachment.url);
|
||||
attachment.width = width;
|
||||
attachment.height = height;
|
||||
|
||||
// Change the media.discordapp.net url to use cdn.discordapp.com
|
||||
// since the media one will return http 415 for svgs
|
||||
attachment.proxy_url = attachment.url;
|
||||
|
||||
shouldUpdateMessage = true;
|
||||
}
|
||||
|
||||
if (shouldUpdateMessage) {
|
||||
FluxDispatcher.dispatch({ type: "MESSAGE_UPDATE", message });
|
||||
}
|
||||
},
|
||||
|
||||
currentlyProcessing: new Set<string>(),
|
||||
async processEmbeds(message: Message) {
|
||||
if (message.state !== "SENT") return;
|
||||
if (message.hasFlag(EMBED_SUPPRESSED)) return;
|
||||
|
||||
if (this.currentlyProcessing.has(message.id)) return;
|
||||
this.currentlyProcessing.add(message.id);
|
||||
|
||||
let shouldUpdateMessage = false;
|
||||
|
||||
const svgUrls = new Set(message.content.match(URL_REGEX));
|
||||
const existingUrls = new Set(message.embeds.filter(e => e.type === "image").map(e => e.image?.url));
|
||||
|
||||
let imageEmbedsCount = existingUrls.size;
|
||||
for (const url of [...svgUrls.values()]) {
|
||||
if (imageEmbedsCount >= MAX_EMBEDS_PER_MESSAGE) break;
|
||||
if (existingUrls.has(url)) continue;
|
||||
|
||||
const belowMaxSize = await checkFileSize(url);
|
||||
if (!belowMaxSize) continue;
|
||||
|
||||
const { width, height } = await getSVGDimensions(url);
|
||||
// @ts-ignore ~ bad types
|
||||
message.embeds.push({
|
||||
id: "embed_1", // The id can be anything as it seems to be changed by the client anyways
|
||||
url,
|
||||
type: "image",
|
||||
image: { url, proxyURL: url, width, height },
|
||||
fields: []
|
||||
});
|
||||
|
||||
imageEmbedsCount++;
|
||||
shouldUpdateMessage = true;
|
||||
}
|
||||
|
||||
if (shouldUpdateMessage) {
|
||||
FluxDispatcher.dispatch({ type: "MESSAGE_UPDATE", message });
|
||||
}
|
||||
|
||||
this.currentlyProcessing.delete(message.id);
|
||||
}
|
||||
});
|
Loading…
Reference in a new issue