在JavaScript中撰写HTML糊

发布于 2025-01-24 10:03:55 字数 121 浏览 2 评论 0原文

我在内容可编辑模式下使用HTML DIV。从JavaScript中,给定有效的图像URL,我如何将其转换为可以将其推入剪贴板上的东西,以便当我用ctrl+v键粘贴到内容可编辑的内容中时,它会粘贴图像本身,而不是粘贴纯文本字符串?

I'm using an HTML DIV in Content Editable mode. From Javascript, given a valid image URL in a string, how can I convert that into something that I can push onto the clipboard so that when I paste into the Content Editable DIV with CTRL+V keystroke, it pastes the image itself and not the plain text string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2025-01-31 10:03:55
async function writeClipImg(url) {
  try {
    const data = await fetch(url);
    const blob = await data.blob();
    await navigator.clipboard.write([new ClipboardItem({
      [blob.type]: blob
    })]);
    console.log('Fetched image copied.');
  } catch (err) {
    console.error(err.name, err.message);
  }
}
//function from https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem
let text; //get somehow the text of the div
if (text.indexOf('://') != -1) { //check 'https://' (typic url substring) is there at the text
  //only the https care - you can write this for http://, file:///, data etc.
  let link = text.slice(text.indexOf('https://'), text.indexOf(' ', text.indexOf('https://')))
  //its only get the first link
  writeClipImg(link)
}
async function writeClipImg(url) {
  try {
    const data = await fetch(url);
    const blob = await data.blob();
    await navigator.clipboard.write([new ClipboardItem({
      [blob.type]: blob
    })]);
    console.log('Fetched image copied.');
  } catch (err) {
    console.error(err.name, err.message);
  }
}
//function from https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem
let text; //get somehow the text of the div
if (text.indexOf('://') != -1) { //check 'https://' (typic url substring) is there at the text
  //only the https care - you can write this for http://, file:///, data etc.
  let link = text.slice(text.indexOf('https://'), text.indexOf(' ', text.indexOf('https://')))
  //its only get the first link
  writeClipImg(link)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文