diff options
author | Vendicated <vendicated@riseup.net> | 2023-04-08 23:05:38 +0200 |
---|---|---|
committer | Vendicated <vendicated@riseup.net> | 2023-04-08 23:05:38 +0200 |
commit | acc874c34fac42422fcaedb2345f56d1e2d5bd74 (patch) | |
tree | 2f2ab15f2c2ad8ef7a1920980b3ec975d0cdf6e8 | |
parent | 0dee968e980f2626715df5db7a9269c3c5098f72 (diff) | |
download | Vencord-acc874c34fac42422fcaedb2345f56d1e2d5bd74.tar.gz Vencord-acc874c34fac42422fcaedb2345f56d1e2d5bd74.tar.bz2 Vencord-acc874c34fac42422fcaedb2345f56d1e2d5bd74.zip |
WebContextMenus: Fix jpegs being uncopyable
-rw-r--r-- | src/plugins/webContextMenus.web.ts | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/src/plugins/webContextMenus.web.ts b/src/plugins/webContextMenus.web.ts index 6b193e5..2e9d581 100644 --- a/src/plugins/webContextMenus.web.ts +++ b/src/plugins/webContextMenus.web.ts @@ -179,14 +179,25 @@ export default definePlugin({ ], async copyImage(url: string) { - const data = await fetchImage(url); - if (!data) return; - - await navigator.clipboard.write([ - new ClipboardItem({ - [data.type]: data - }) - ]); + // Clipboard only supports image/png, jpeg and similar won't work. Thus, we need to convert it to png + // via canvas first + const img = new Image(); + img.onload = () => { + const canvas = document.createElement("canvas"); + canvas.width = img.naturalWidth; + canvas.height = img.naturalHeight; + canvas.getContext("2d")!.drawImage(img, 0, 0); + + canvas.toBlob(data => { + navigator.clipboard.write([ + new ClipboardItem({ + "image/png": data! + }) + ]); + }, "image/png"); + }; + img.crossOrigin = "anonymous"; + img.src = url; }, async saveImage(url: string) { |