替换Discuz的复制flash

替换Discuz论坛的"点此复制到剪贴板"flash

当前为 2017-12-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name replace copy flash on Discuz
  3. // @name:zh-CN 替换Discuz的复制flash
  4. // @description Replace the "click here to copy" flash on Discuz
  5. // @description:zh-CN 替换Discuz论坛的"点此复制到剪贴板"flash
  6. // @namespace https://github.com/Testla
  7. // @version 0.9.1
  8. // @include http*://www.tsdm.me/*
  9. // @include http*://www.lightnovel.cn/*
  10. // @author Testla
  11. // @license MIT License
  12. // @compatible firefox 57 + Greasemonkey4/Tampermonkey tested
  13. // @compatible chrome + Tampermonkey
  14. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  15. // @grant GM.info
  16. // @grant GM.setClipboard
  17. // @grant GM_info
  18. // @grant GM_setClipboard
  19. // @grant unsafeWindow
  20. // ==/UserScript==
  21.  
  22. (function() {
  23. 'use strict';
  24.  
  25. // There are two versions available,
  26. // the non-privileged version doesn't use privileged API
  27. // but doesn't support Greasemonkey 4+
  28. // and may be incompatible with some old browsers
  29. // (check https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand#Browser_compatibility).
  30. // To switch to the non-privileged version:
  31. // 1. remove all @require and @grant in the header
  32. // 2. add @grant none to the same place
  33. // 3. comment out the privileged version
  34. // 4. uncomment the non-privileged version
  35.  
  36. // ---------------- BEGIN PRIVILEGED VERSION ----------------
  37. // If you only run on Greasemonkey 4+, you can remove the @require.
  38. // If you need not to run on Greasemonkey 4+,
  39. // you can remove the @require line together with the @grant GM.*s
  40. // and replace all "GM." with "GM_".
  41. // Note that the "@grant GM_*"s are required for Tampermonkey in Chrome
  42. // even if the corresponding "@grant GM.*"s and gm4-polyfill already exists,
  43. // please let me know if you can figure out why.
  44.  
  45. function copyAndHint(text) {
  46. GM.setClipboard(text);
  47. // showPrompt comes with Discuz
  48. unsafeWindow.showPrompt(null, null, 'Copied', 3000);
  49. }
  50.  
  51. function setCopy(text, hint) {
  52. copyAndHint(text);
  53. }
  54.  
  55. function copycode(code_div) {
  56. copyAndHint(code_div.textContent);
  57. }
  58.  
  59. var greasemonkey4OrGreater = GM.info.scriptHandler == 'Greasemonkey' &&
  60. parseFloat(GM.info.version) >= 4.0;
  61. if (greasemonkey4OrGreater) {
  62. // uses Firefox-specific hack
  63. // https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts
  64. exportFunction(copyAndHint, window, {defineAs:'copyAndHint'});
  65. window.eval(
  66. 'window.setCopy = function(text, hint) { copyAndHint(text); };' +
  67. 'window.copycode = function(code_div) { copyAndHint(code_div.textContent); };');
  68. } else {
  69. unsafeWindow.setCopy = setCopy;
  70. unsafeWindow.copycode = copycode;
  71. }
  72. // ---------------- END PRIVILEGED VERSION ----------------
  73.  
  74. // ---------------- BEGIN NON-PRIVILEGED VERSION ----------------
  75. // var copyTextarea = document.createElement("textarea");
  76. // copyTextarea.style.width = "0px";
  77. // copyTextarea.style.height = "0px";
  78. // copyTextarea.style.position = "fixed";
  79.  
  80. // // https://stackoverflow.com/questions/400212
  81. // function copyAndHint(text) {
  82. // document.body.appendChild(copyTextarea);
  83. // copyTextarea.textContent = text;
  84. // copyTextarea.select();
  85.  
  86. // try {
  87. // var successful = document.execCommand('copy');
  88. // var msg = successful ? 'succeeded' : 'failed';
  89. // showPrompt(null, null, 'Copy ' + msg, 3000);
  90. // } catch (err) {
  91. // showPrompt(null, null, 'Oops, unable to copy', 3000);
  92. // console.log(err);
  93. // }
  94. // document.body.removeChild(copyTextarea);
  95. // }
  96.  
  97. // window.setCopy = function(text, hint) {
  98. // copyAndHint(text);
  99. // };
  100.  
  101. // window.copycode = function(code_div) {
  102. // copyAndHint(code_div.textContent);
  103. // };
  104. // ---------------- END NON-PRIVILEGED VERSION ----------------
  105.  
  106. console.log('finished replacing Discuz\'s copy flash');
  107. })();