• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Roséa
😘正是你花费在玫瑰上的时间才使得你的玫瑰花珍贵无比...
博客园    首页    新随笔    联系   管理    订阅  订阅

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

Javascript为复制的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 = '\n\n 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 = '\n\n 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 = '\n\n 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('\n'))
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 }

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

敬请留意~~

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

posted on 2017-12-11 11:12  Roséa  阅读(688)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3