• 【分享】JS如何为复制的Web文本添加其他信息


    看到了两篇关于这题的讨论,简单的记录一下!o(* ̄▽ ̄*)ブ

     1.  stackoverflow , How to add extra info to copied web text

     2.  黑客派,https://hacpai.com/article/1510544423932

    第一条使用两种方法对文章进行粘贴追加信息~~~

    方法一:

    ①监听copy事件,然后将隐藏盒子中的信息添加到其中;

    ②结合window.selection()方法;

    ③浏览器兼容情况是主流浏览器IE8以上;

    ④线上demo http://jsfiddle.net/jp6nhmxf/ ;

    ⑤使用:复制一段文本再粘贴就会出现 pagelink中的内容 。

    主要JS code

     1   function addLink() {
     2         //Get the selected text and append the extra info
     3         var selection = window.getSelection(),
     4             pagelink = '
    
     Read more at: ' + document.location.href,
     5             copytext = selection + pagelink,
     6             newdiv = document.createElement('div');
     7 
     8         //hide the newly created container
     9         newdiv.style.position = 'absolute';
    10         newdiv.style.left = '-99999px';
    11 
    12         //insert the container, fill it with the extended text, and define the new selection
    13         document.body.appendChild(newdiv);
    14         newdiv.innerHTML = copytext;
    15         selection.selectAllChildren(newdiv);
    16 
    17         window.setTimeout(function () {
    18             document.body.removeChild(newdiv);
    19         }, 100);
    20     }
    21 
    22     document.addEventListener('copy', addLink);

    方法二:

    ①监听copy事件,然后修改剪贴板中的内容,也就是clipboard使用;

    ②结合window.clipboardData.setData()方法;

    ③浏览器兼容情况是IE4以上(换言之只针对于IE);

    ④线上demo http://jsfiddle.net/m56af0je/ (IE模式下起效);

    主要JS code

        function addLink(event) {
            event.preventDefault();
    
            var pagelink = '
    
     Read more at: ' + document.location.href,
                copytext =  window.getSelection() + pagelink;
    
            if (window.clipboardData) {
                window.clipboardData.setData('Text', copytext);
            }
        }
    
        document.addEventListener('copy', addLink);

    ⑤另外疑问点来了,使用clipboard能在其他浏览器(比如谷歌/火狐/safari)中工作吗?

    主要JS code

     1 function addLink(event) {
     2     event.preventDefault();
     3 
     4     var pagelink = '
    
     Read more at: ' + document.location.href,
     5         copytext =  window.getSelection() + pagelink;
     6 
     7     (event.clipboardData || window.clipboardData).setData('Text', copytext);
     8 }
     9 
    10 document.addEventListener('copy', addLink);

    区别在 window.clipboarddata  <-->  event.clipboardData

    亲测在兼容模式、极速模式、谷歌、火狐、IE等浏览器中均测有效!

    第二条使用的方法跟第一条类似~~~

    主要JS code

     1 /**
     2  * @description 添加版权
     3  */
     4  const addCopyright = () => {
     5   const genCopy = () => {
     6     return [
     7       '',
     8       '',
     9       '作者:Vanessa',
    10       '链接 [文章复制添加版权](https://hacpai.com/article/1510544423932) ',
    11       '来源:黑客派',
    12       '著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。',
    13     ]
    14   }
    15 
    16   $('.content-reset').on('copy', function (event) {
    17     if (!window.getSelection) {
    18       return
    19     }
    20 
    21     let copyString = window.getSelection().toString()
    22 
    23     if (copyString.length < 128) {
    24       return
    25     }
    26 
    27     if ('object' === typeof event.originalEvent.clipboardData) {
    28       event.originalEvent.clipboardData.setData('text/html', copyString + genCopy().join(''))
    29       event.originalEvent.clipboardData.setData('text/plain', copyString + genCopy().join('
    '))
    30       event.preventDefault()
    31       return
    32     }
    33 
    34     $('body').append(`${copyString}${genCopy().join('')}`)
    35     window.getSelection().selectAllChildren($('#pipeFixCopy')[0])
    36     setTimeout(function() {
    37       $('#pipeFixCopy').remove()
    38     }, 200)
    39   })
    40 }

    找一个空白地方复制粘贴测试,~~本人只在极速模式下测通过,其他未测~~

    敬请留意~~

    ~~~抱拳撒花*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。~~~

  • 相关阅读:
    AM3715/DM3730 更改内存大小后kernel boot不起来的解决办法
    xslt转换xml文档&&xslt call java方法
    VSCode 运行go test显示打印日志
    tomcat作为windows服务的参数配置,特别是PermSize的设置
    高亮显示web页表格行
    深入分析 Java 中的中文编码问题
    webwork和spring多配置文件的方法
    Bug笔记:诡异的$.ajax
    C#多态
    委托的本质
  • 原文地址:https://www.cnblogs.com/anniey/p/8021727.html
Copyright © 2020-2023  润新知