首页 > 解决方案 > 如何使用 chrome 扩展复制“粘贴图像”

问题描述

我正在开发一个 chrome 扩展,它可以保存和粘贴片段。

文本项目效果很好:

export function copyText(text: string): void {
  const textarea = window.document.createElement('textarea');
  textarea.style.position = 'fixed';
  textarea.style.opacity = '0';
  textarea.value = text;
  window.document.body.appendChild(textarea);
  textarea.select();
  window.document.execCommand('copy');
  window.document.body.removeChild(textarea);
}

export function pasteText(element: HTMLElement): void {
  element.focus();
  window.document.execCommand('paste');
}

现在,我正在尝试为图像实现相同的功能。当它工作时,这就是它在 discord.com 上的样子:

在此处输入图像描述

目前,我一直在尝试重现这种行为(例如在 Chrome 控制台中)。

您知道如何实施吗?

标签: javascriptgoogle-chrome-extension

解决方案


我猜它正在检查您粘贴的内容。如果它以 png、jpg、webp 或 avif 等图像格式结尾。然后它将 url 嵌入到图像元素中。

例如:_ https://random.image/cute-dog.png会变成

<img src="https://random.image/cute-dog.png" />

推荐阅读