clipboardData Emulator

emulate clipboardData in non-IE browser

当前为 2014-05-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name clipboardData Emulator
  3. // @namespace com.ayanamist.clipboardData
  4. // @version 0.4
  5. // @description emulate clipboardData in non-IE browser
  6. // @include http*
  7. // @run-at document-start
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13. if (unsafeWindow.window.clipboardData) {
  14. return;
  15. }
  16. var textareaId = 'clipboardEmu';
  17. unsafeWindow.window.clipboardData = {
  18. clearData: function () {
  19. return true;
  20. },
  21. getData: function () {
  22. return '';
  23. },
  24. setData: function (format, data) {
  25. // Prevent multiple textarea instance.
  26. var clipboard = document.getElementById(textareaId);
  27. if (!clipboard) {
  28. clipboard = document.createElement("textarea");
  29. clipboard.style.float = 'left';
  30. clipboard.style.position = 'fixed';
  31. clipboard.style.left = 0;
  32. clipboard.style.top = 0;
  33. clipboard.style.width = '400px';
  34. clipboard.style.height = '300px';
  35. clipboard.style.zIndex = 9999;
  36. clipboard.id = textareaId;
  37. clipboard.readOnly = true;
  38. document.getElementsByTagName('body')[0].appendChild(clipboard);
  39. }
  40. clipboard.textContent = data;
  41. clipboard.focus();
  42. clipboard.select();
  43. var closeClipboard = function () {
  44. clipboard.parentNode.removeChild(clipboard);
  45. clipboard = null;
  46. };
  47. clipboard.onkeydown = function (evt) {
  48. if(evt.which == 27){ // Esc
  49. closeClipboard();
  50. }
  51. };
  52. clipboard.oncut = clipboard.oncopy = function () {
  53. setTimeout(closeClipboard, 0);
  54. return true;
  55. };
  56. return true;
  57. },
  58. files: [],
  59. };
  60. })();

QingJ © 2025

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