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.0
  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 }) {
  22. GM_registerMenuCommand(`Copy URL as ${name} link`, () => {
  23. getPageMeta().then(({ title, url }) => {
  24. GM_setClipboard(getLinkMarkup({ title, url }), type);
  25. });
  26. });
  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. // @see https://www.tampermonkey.net/documentation.php#api:GM_setClipboard
  39. name: 'richtext',
  40. type: 'html',
  41. getLinkMarkup: ({title, url}) => `<a href="${url}">${title}</a>`
  42. },
  43. {
  44. name: 'markdown',
  45. getLinkMarkup: ({ title, url }) => `[${title}](${url})`
  46. },
  47. {
  48. name: 'html',
  49. getLinkMarkup: ({ title, url }) => `<a href="${url}">${title}</a>`
  50. },
  51. {
  52. name: 'orgmode',
  53. getLinkMarkup: ({ title, url }) => `[[${url}][${title}]]`
  54. },
  55. {
  56. name: 'typst',
  57. // @see https://typst.app/docs/reference/model/link/
  58. getLinkMarkup: ({ title, url }) => `#link("${url}")[${title}]`
  59. },
  60. ];
  61. }
  62. })();