Markdown Link

One click to copy a markdown link of current page.

  1. // ==UserScript==
  2. // @name Markdown Link
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description One click to copy a markdown link of current page.
  6. // @author YourName
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to create and show a hovering, styled div with the page title
  16. function displayHoveringTitle() {
  17. const pageTitle = document.title; // Get the current page title
  18. const pageUrl = new URL(window.location.href);
  19.  
  20. // Optionally, clean the URL by removing search parameters and hash
  21. pageUrl.search = ""; // Remove query strings
  22. pageUrl.hash = ""; // Remove fragment identifier
  23.  
  24. // Create the markdown link for copy purpose only
  25. const markdownLink = `[${pageTitle}](${pageUrl.href})`;
  26.  
  27. // Create a div element for displaying the title
  28. const floatingDiv = document.createElement('div');
  29. floatingDiv.textContent = pageTitle; // Set the content to page title only
  30. floatingDiv.style.position = 'fixed';
  31. floatingDiv.style.bottom = '10px';
  32. floatingDiv.style.right = '10px';
  33. floatingDiv.style.backgroundColor = 'rgba(0,0,0,0.5)'; // Semi-transparent black background
  34. floatingDiv.style.color = 'white';
  35. floatingDiv.style.padding = '10px';
  36. floatingDiv.style.borderRadius = '5px';
  37. floatingDiv.style.zIndex = '1000';
  38. floatingDiv.style.cursor = 'pointer'; // Change cursor to pointer to indicate clickable
  39.  
  40. // Append the div to the body of the page
  41. document.body.appendChild(floatingDiv);
  42.  
  43. // Add click event listener to copy markdown link to clipboard
  44. floatingDiv.addEventListener('click', function() {
  45. navigator.clipboard.writeText(markdownLink).then(() => {
  46. console.log('Markdown link copied to clipboard!');
  47. }).catch(err => {
  48. console.error('Failed to copy markdown link: ', err);
  49. });
  50. });
  51. }
  52.  
  53. // Add an event listener to call the function when the userscript is loaded
  54. window.addEventListener('load', displayHoveringTitle);
  55. })();