百度文库文字复制

添加一个按钮,复制百度文库中的文字

  1. // ==UserScript==
  2. // @name 百度文库文字复制
  3. // @namespace BlueFire
  4. // @version 0.72
  5. // @description 添加一个按钮,复制百度文库中的文字
  6. // @author BlueFire
  7. // @match *://wenku.baidu.com/view/*
  8. // @grant unsafeWindow
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. if(!$){
  16. var s = document.createElement ("script");
  17. s.src = "http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js";
  18. s.async = false;
  19. document.documentElement.appendChild (s);
  20. }
  21.  
  22. let timeoutId = -1;
  23. let downloadBtn = '<div id="reader-copy-div" style="float:left;padding:10px 20px;background:green;z-index:999;position:relative;top:60px;left:0px;"><a id="reader-copy-text" href="###" style="color:white;font-size:15px;"><b class="ui-btn-btc">复制此页</b></a></div>';
  24. let toastDiv = '<div id="page-toast-div" style="margin: 0px auto;background: black;opacity: 0.8;padding: 15px 30px;position: fixed;z-index: 10001;display: block;top: 85%;left: 44%;"><span id="page-toast-span" style="color:white;font-size:15px;"></span></div>';
  25. let opacity = '0.95';
  26. let ua = navigator.userAgent;
  27. if(ua.indexOf("Edge") >= 0){
  28. opacity = '0.6';
  29. }
  30. else if(ua.indexOf("Chrome")){
  31. opacity = '0.95';
  32. }
  33. let textBlockDiv = '<div id="page-textblock-div" style="width:100%;height:100%;position: fixed;z-index: 9999;display: block;top: 0px;left: 0px;background:rgba(255,255,255,' + opacity + ');-webkit-backdrop-filter: blur(20px);display: flex;justify-content:center;align-items:center;"><div id="page-textblock-cancel-layer" style="width:100%;height:100%;position:fixed;top:0px;left:0px;"></div><pre id="page-textblock" style="width:60%;font-size:16px;line-height:22px;z-index:10000;white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;word-break:break-all;max-height:70%;overflow:auto;"></pre></div>"';
  34. function Copy(str){
  35. let result = false;
  36. let save = function(e){
  37. e.clipboardData.setData('text/plain', str);
  38. e.preventDefault();
  39. }
  40. document.addEventListener('copy', save);
  41. result = document.execCommand('copy');
  42. document.removeEventListener('copy',save);
  43. return result;
  44. }
  45.  
  46. function ShowToast(str){
  47. if(timeoutId >= 0){
  48. clearTimeout(timeoutId);
  49. }
  50. $('#page-toast-div').remove();
  51. $('body').append(toastDiv);
  52. $('#page-toast-span').text(str);
  53. timeoutId=setTimeout("$('#page-toast-div').remove();",1500);
  54. }
  55.  
  56. function ShowTextBlock(str){
  57. $('#page-textblock-div').remove();
  58. $('body').append(textBlockDiv);
  59. $('#page-textblock').text(str);
  60. $('#page-textblock-cancel-layer').click(function(){
  61. $('#page-textblock-div').remove();
  62. });
  63. }
  64.  
  65. function PrependButtonTo(ele){
  66. ele.prepend(downloadBtn);
  67. ele.find('#reader-copy-text').click(function(){
  68. let str = "";
  69. let parent = $(this).parent().parent();
  70. parent.find('.reader-word-layer').each(function(){
  71. str += this.innerText.replace(/\u2002/g,' ');
  72. });
  73. str = str.replace(/。\s/g,'。\r\n');
  74. let result= (str.length > 0);
  75. if(result){
  76. ShowToast("解析成功");
  77. ShowTextBlock(str);
  78. }else{
  79. ShowToast("解析失败,请等待网页加载");
  80. }
  81. });
  82. }
  83.  
  84. $(document).ready(function(){
  85. $('.mod.reader-page.complex').each(function(){
  86. PrependButtonTo($(this));
  87. });
  88. });
  89. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址