zoukankan      html  css  js  c++  java
  • 【分享】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 }

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

    敬请留意~~

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

  • 相关阅读:
    遗传算法(Genetic Algorithm, GA)及MATLAB实现
    CCF CSP 201809-2 买菜
    PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题 (20 分)
    PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
    PAT (Basic Level) Practice (中文)1004 成绩排名 (20 分)
    PAT (Basic Level) Practice (中文)1002 写出这个数 (20 分)
    PAT (Advanced Level) Practice 1001 A+B Format (20 分)
    BP神经网络(原理及MATLAB实现)
    问题 1676: 算法2-8~2-11:链表的基本操作
    问题 1744: 畅通工程 (并查集)
  • 原文地址:https://www.cnblogs.com/anniey/p/8021727.html
Copyright © 2011-2022 走看看