Copy URL

Copy URL of current browser tab as different markup such as orgmode, markdown, typst and even RTF (rich text format or WYSIWYG) ...

  1. // ==UserScript==
  2. // @name Copy URL
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.3
  5. // @description Copy URL of current browser tab as different markup such as orgmode, markdown, typst and even RTF (rich text format or WYSIWYG) ...
  6. // @author Ice Zero
  7. // @license MIT
  8. // @match http://*/*
  9. // @match https://*/*
  10. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_setClipboard
  13. // @run-at document-end
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. 'use strict';
  18.  
  19. provideSchemas().forEach(behavior);
  20.  
  21. function behavior({ name, type = 'text', getLinkMarkup, shortcut }) {
  22. GM_registerMenuCommand(`Copy URL as ${name} link`, () => {
  23. getPageMeta().then(({ title, url }) => {
  24. GM_setClipboard(getLinkMarkup({ title, url }), type);
  25. });
  26. }, shortcut);
  27. }
  28.  
  29. async function getPageMeta() {
  30. const title = document.title;
  31. const url = window.location.href;
  32. return { title, url };
  33. }
  34.  
  35. function provideSchemas() {
  36. return [
  37. {
  38. name: 'title',
  39. getLinkMarkup: ({title}) => `${title}`,
  40. },
  41. {
  42. name: 'url',
  43. getLinkMarkup: ({url}) => `${url}`,
  44. },
  45. {
  46. // @see https://www.tampermonkey.net/documentation.php#api:GM_setClipboard
  47. name: 'richtext',
  48. type: 'html',
  49. getLinkMarkup: ({title, url}) => `<a href="${url}">${title}</a>`,
  50. // @see https://www.tampermonkey.net/documentation.php?locale=en#api:GM_registerMenuCommand
  51. // @desc should config shortcut for call Tampermonkey in `chrome://extensions/shortcuts` first
  52. shortcut: 'r',
  53. },
  54. {
  55. name: 'markdown',
  56. getLinkMarkup: ({ title, url }) => `[${title}](${url})`,
  57. shortcut: 'm',
  58. },
  59. {
  60. name: 'html',
  61. getLinkMarkup: ({ title, url }) => `<a href="${url}">${title}</a>`,
  62. shortcut: 'h',
  63. },
  64. {
  65. name: 'orgmode',
  66. getLinkMarkup: ({ title, url }) => `[[${url}][${title}]]`,
  67. shortcut: 'o',
  68. },
  69. {
  70. name: 'typst',
  71. // @see https://typst.app/docs/reference/model/link/
  72. getLinkMarkup: ({ title, url }) => `#link("${url}")[${title}]`,
  73. shortut: 't',
  74. },
  75. {
  76. name: 'tsdoc',
  77. // @see https://tsdoc.org/pages/tags/link/
  78. getLinkMarkup: ({ title, url }) => `{@link ${url} | ${title}}`,
  79. },
  80. ];
  81. }
  82. })();