Copy Markdown-Format Address

Copy current tab title and url, and convert to markdown syntax

目前为 2018-02-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Copy Markdown-Format Address
  3. // @namespace undefined
  4. // @version 0.2
  5. // @description Copy current tab title and url, and convert to markdown syntax
  6. // @author https://github.com/Dream4ever
  7. // @require https://code.jquery.com/jquery-latest.js
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. // Thanks to https://stackoverflow.com/questions/400212/
  13. // solved the problem of unable select text
  14. // of none-displayed textarea
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. window.onload = function () {
  20. var rdnId = Math.random().toString(36).substring(5);
  21. var nodeButton = document.createElement('button');
  22. nodeButton.setAttribute('id', rdnId);
  23. nodeButton.style.width = '22px';
  24. nodeButton.style.height = '22px';
  25. nodeButton.style.opacity = '.2';
  26. nodeButton.style.padding = '5px 10px';
  27. nodeButton.style.borderRadius = '5px';
  28. nodeButton.style.background = 'rgb(240, 240, 240)';
  29. nodeButton.style.boxShadow = '3px 3px 3px rgba(0, 0, 0, .1)';
  30. nodeButton.style.position = 'fixed';
  31. nodeButton.style.bottom = '20px';
  32. nodeButton.style.right = '100px';
  33. nodeButton.style.zIndex = 2;
  34. document.body.appendChild(nodeButton);
  35.  
  36. var id = '#' + rdnId;
  37. var $btn = $(id);
  38.  
  39. $btn.hover(function()
  40. {
  41. $btn.text('复制');
  42. $btn.css('width', '80px');
  43. $btn.css('height', '28px');
  44. $btn.css('opacity', '1');
  45. $btn.css('fontSize', '14px');
  46. $btn.css('fontWeight', '400');
  47. $btn.css('lineHeight', '1.15');
  48. $btn.css('color', '#000');
  49. },
  50. function()
  51. {
  52. $btn.text('');
  53. $btn.css('width', '22px');
  54. $btn.css('height', '22px');
  55. $btn.css('opacity', '.2');
  56. });
  57.  
  58. $btn.on('click', function (event) {
  59.  
  60. // get title and url
  61. var title = document.title;
  62. var url = document.URL;
  63. var address = '[' + title + '](' + url + ')';
  64.  
  65. if (copyTextToClipboard(address)) {
  66. $btn.text('复制成功');
  67. $btn.css('color', 'green');
  68. setTimeout(function() {
  69. $btn.text('复制');
  70. $btn.css('color', '#000');
  71. }, 2000);
  72. } else {
  73. $btn.text('复制失败');
  74. $btn.css('color', 'red');
  75. setTimeout(function() {
  76. $btn.text('复制');
  77. $btn.css('color', '#000');
  78. }, 2000);
  79. }
  80. });
  81. };
  82.  
  83. })();
  84.  
  85. function copyTextToClipboard(text) {
  86. var textArea = document.createElement("textarea");
  87.  
  88. //
  89. // *** This styling is an extra step which is likely not required. ***
  90. //
  91. // Why is it here? To ensure:
  92. // 1. the element is able to have focus and selection.
  93. // 2. if element was to flash render it has minimal visual impact.
  94. // 3. less flakyness with selection and copying which **might** occur if
  95. // the textarea element is not visible.
  96. //
  97. // The likelihood is the element won't even render, not even a flash,
  98. // so some of these are just precautions. However in IE the element
  99. // is visible whilst the popup box asking the user for permission for
  100. // the web page to copy to the clipboard.
  101. //
  102.  
  103. // Place in top-left corner of screen regardless of scroll position.
  104. textArea.style.position = 'fixed';
  105. textArea.style.top = 0;
  106. textArea.style.left = 0;
  107.  
  108. // Ensure it has a small width and height. Setting to 1px / 1em
  109. // doesn't work as this gives a negative w/h on some browsers.
  110. textArea.style.width = '2em';
  111. textArea.style.height = '2em';
  112.  
  113. // We don't need padding, reducing the size if it does flash render.
  114. textArea.style.padding = 0;
  115.  
  116. // Clean up any borders.
  117. textArea.style.border = 'none';
  118. textArea.style.outline = 'none';
  119. textArea.style.boxShadow = 'none';
  120.  
  121. // Avoid flash of white box if rendered for any reason.
  122. textArea.style.background = 'transparent';
  123.  
  124. textArea.value = text;
  125.  
  126. document.body.appendChild(textArea);
  127.  
  128. textArea.select();
  129.  
  130. var isOK = false;
  131.  
  132. try {
  133. var successful = document.execCommand('copy');
  134. isOK = !!successful;
  135. } catch (err) {}
  136.  
  137. document.body.removeChild(textArea);
  138.  
  139. return isOK;
  140. }