Copy Magnet Links

Copy all magnet links on the page to the clipboard

  1. // ==UserScript==
  2. // @name Copy Magnet Links
  3. // @namespace https://lynelluo.github.io/
  4. // @version 0.1
  5. // @description Copy all magnet links on the page to the clipboard
  6. // @author lynel0625
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. // Function to copy magnet links to clipboard
  15. function copyMagnetLinks() {
  16. var magnets = [];
  17. var links = document.querySelectorAll('a[href^="magnet:"]');
  18.  
  19. links.forEach(function(link) {
  20. magnets.push(link.href);
  21. });
  22.  
  23. if (magnets.length > 0) {
  24. var magnetLinks = magnets.join("\n");
  25.  
  26. var textarea = document.createElement("textarea");
  27. textarea.value = magnetLinks;
  28. document.body.appendChild(textarea);
  29. textarea.select();
  30. document.execCommand("copy");
  31. document.body.removeChild(textarea);
  32.  
  33. showNotification("Copied magnet links:\n" + magnetLinks);
  34. } else {
  35. showNotification("No magnet links found.");
  36. }
  37. }
  38.  
  39. // Function to show a notification
  40. function showNotification(message) {
  41. var notification = document.createElement("div");
  42. notification.innerText = message;
  43. notification.style.position = "fixed";
  44. notification.style.bottom = "50px";
  45. notification.style.left = "10px";
  46. notification.style.backgroundColor = "#333";
  47. notification.style.color = "#fff";
  48. notification.style.padding = "10px";
  49. notification.style.borderRadius = "5px";
  50. notification.style.zIndex = 1001;
  51. notification.style.fontSize = "14px";
  52. notification.style.opacity = "0.9";
  53. document.body.appendChild(notification);
  54.  
  55. // Automatically remove the notification after 2 seconds
  56. setTimeout(function() {
  57. document.body.removeChild(notification);
  58. }, 2000);
  59. }
  60.  
  61.  
  62. // Create a button to trigger the copy action
  63. var button = document.createElement("button");
  64. button.innerText = "Copy Magnet Links";
  65. button.style.position = "fixed";
  66. button.style.bottom = "10px";
  67. button.style.left = "10px"; // Place the button in the bottom-left corner
  68. button.style.zIndex = 1000; // Make sure the button is on top of other elements
  69. document.body.appendChild(button);
  70.  
  71. // Add event listener for the button
  72. button.addEventListener("click", function() {
  73. copyMagnetLinks();
  74. });
  75. })();