diff --git a/src/plugins/translate/TranslationAccessory.tsx b/src/plugins/translate/TranslationAccessory.tsx index 4f46e4b3..72b35940 100644 --- a/src/plugins/translate/TranslationAccessory.tsx +++ b/src/plugins/translate/TranslationAccessory.tsx @@ -44,6 +44,9 @@ export function TranslationAccessory({ message }: { message: Message; }) { const [translation, setTranslation] = useState(); useEffect(() => { + // Ignore MessageLinkEmbeds messages + if ((message as any).vencordEmbeddedBy) return; + TranslationSetters.set(message.id, setTranslation); return () => void TranslationSetters.delete(message.id); diff --git a/src/plugins/translate/index.tsx b/src/plugins/translate/index.tsx index d71b2da4..3007032b 100644 --- a/src/plugins/translate/index.tsx +++ b/src/plugins/translate/index.tsx @@ -18,19 +18,39 @@ import "./styles.css"; +import { addContextMenuPatch, findGroupChildrenByChildId, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu"; import { addAccessory, removeAccessory } from "@api/MessageAccessories"; import { addPreSendListener, removePreSendListener } from "@api/MessageEvents"; import { addButton, removeButton } from "@api/MessagePopover"; import ErrorBoundary from "@components/ErrorBoundary"; import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; -import { ChannelStore } from "@webpack/common"; +import { ChannelStore, Menu } from "@webpack/common"; import { settings } from "./settings"; import { TranslateChatBarIcon, TranslateIcon } from "./TranslateIcon"; import { handleTranslate, TranslationAccessory } from "./TranslationAccessory"; import { translate } from "./utils"; +const messageCtxPatch: NavContextMenuPatchCallback = (children, { message }) => () => { + if (!message.content) return; + + const group = findGroupChildrenByChildId("copy-text", children); + if (!group) return; + + group.splice(group.findIndex(c => c?.props?.id === "copy-text") + 1, 0, ( + { + const trans = await translate("received", message.content); + handleTranslate(message.id, trans); + }} + /> + )); +}; + export default definePlugin({ name: "Translate", description: "Translate messages with Google Translate", @@ -53,6 +73,8 @@ export default definePlugin({ start() { addAccessory("vc-translation", props => ); + addContextMenuPatch("message", messageCtxPatch); + addButton("vc-translate", message => { if (!message.content) return null; @@ -78,6 +100,7 @@ export default definePlugin({ stop() { removePreSendListener(this.preSend); + removeContextMenuPatch("message", messageCtxPatch); removeButton("vc-translate"); removeAccessory("vc-translation"); },