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.1
  5. // @description Copy current tab title and url, and convert to markdown syntax
  6. // @author https://github.com/Dream4ever
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // Thanks to https://stackoverflow.com/questions/400212/
  12. // solved the problem of unable select text
  13. // of none-displayed textarea
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. window.onload = function () {
  19. var rdnId = Math.random().toString(36).substring(5);
  20. var nodeButton = document.createElement('button');
  21. nodeButton.setAttribute('id', rdnId);
  22. nodeButton.innerHTML = 'Copy';
  23. nodeButton.style.fontSize = '14px';
  24. nodeButton.style.color = '#000';
  25. nodeButton.style.width = '80px';
  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 = '70px';
  33. nodeButton.style.zIndex = 2;
  34. document.body.appendChild(nodeButton);
  35.  
  36. nodeButton.addEventListener('click', function (event) {
  37.  
  38. // get title and url
  39. var title = document.title;
  40. var url = document.URL;
  41. var address = '[' + title + '](' + url + ')';
  42.  
  43. if (copyTextToClipboard(address)) {
  44. nodeButton.innerHTML = 'Done';
  45. setTimeout(function() { nodeButton.innerHTML = 'Copy'; }, 2000);
  46. } else {
  47. nodeButton.innerHTML = 'Failed';
  48. setTimeout(function() { nodeButton.innerHTML = 'Copy'; }, 2000);
  49. }
  50. });
  51. };
  52.  
  53. })();
  54.  
  55. function copyTextToClipboard(text) {
  56. var textArea = document.createElement("textarea");
  57.  
  58. textArea.style.position = 'fixed';
  59. textArea.style.top = 0;
  60. textArea.style.left = 0;
  61.  
  62. textArea.style.width = '2em';
  63. textArea.style.height = '2em';
  64.  
  65. textArea.style.padding = 0;
  66.  
  67. textArea.style.border = 'none';
  68. textArea.style.outline = 'none';
  69. textArea.style.boxShadow = 'none';
  70.  
  71. textArea.style.background = 'transparent';
  72.  
  73. textArea.value = text;
  74.  
  75. document.body.appendChild(textArea);
  76.  
  77. textArea.select();
  78.  
  79. var isOK = false;
  80.  
  81. try {
  82. var successful = document.execCommand('copy');
  83. isOK = !!successful;
  84. } catch (err) {}
  85.  
  86. document.body.removeChild(textArea);
  87.  
  88. return isOK;
  89. }