有条件地避免在文本区域内复制文本不起作用

问题描述

我正在使用将一些信息添加到剪贴板中复制的项目的代码。此外,我还努力避免当所选文本位于文本区域内时此功能不起作用,但直到现在仍没有成功。

看看源代码。当我运行它时,即使选择并复制了文本区域内的文本,也会添加额外的信息。 目的是当所选文本位于textarea内时,不要在剪贴板中复制的文本中添加额外的信息。

我的代码有什么问题吗?

重要提示:它不适用于Mozilla Firefox。

function addLink() {

  /* The intention is that extra info don't be added to the copied text when the selected text be inside a textarea */
if (window.getSelection().baseNode.parentNode.tagName === 'TEXTAREA') return; 
  
  var selection = window.getSelection();
    pagelink = "<br/></br/>Full text: " + document.title + "<br/>" + document.location.href;
    copytext = ("“"+selection+"”") + pagelink;
    newdiv = document.createElement('div');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';
    document.body.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);
    window.setTimeout(function (){document.body.removeChild(newdiv);},100);
}
    
    document.addEventListener('copy',addLink); 
<b>It'll be added some extra info to whatever text copied inside this MRE,but the intention is that they don't be added when the selected text be inside a textarea.</b>

<p>When this text is copied,some informations are added to the clipboard.</p>

<p>Here too,but... </p>

<p><textarea id="urlCopied" rows="3" cols="30">how to avoid that extra informations be added when the copied text come from a textarea?</textarea></p>

<textarea placeholder="Paste for testing..." cols="40" rows="7"></textarea>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)