Copy Real Link

Extract and copy real URLs from redirect URLs on Google, Fb, etc., with a copy button.

当前为 2024-10-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Copy Real Link
  3. // @version 1.3
  4. // @namespace https://greasyfork.org/en/scripts/482987-copy-real-link
  5. // @license CC BY
  6. // @description Extract and copy real URLs from redirect URLs on Google, Fb, etc., with a copy button.
  7. // @author almahmud & gpt
  8. // @match *://www.google.*/*search*
  9. // @match *://search.yahoo.com/*
  10. // @match *://*.facebook.com/*
  11. // @match *://hangouts.google.com/*
  12. // @grant GM_setClipboard
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const createCopyButton = (url) => {
  19. const button = document.createElement('button');
  20. button.style.position = 'absolute';
  21. button.style.zIndex = '1000';
  22. button.style.backgroundColor = '#007bff';
  23. button.style.color = '#fff';
  24. button.style.border = 'none';
  25. button.style.padding = '5px 10px';
  26. button.style.borderRadius = '4px';
  27. button.style.cursor = 'pointer';
  28.  
  29. // SVG icon
  30. const svgIcon = `<svg width="16px" height="16px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.975 14.51a1.05 1.05 0 0 0 0-1.485 2.95 2.95 0 0 1 0-4.172l3.536-3.535a2.95 2.95 0 1 1 4.172 4.172l-1.093 1.092a1.05 1.05 0 0 0 1.485 1.485l1.093-1.092a5.05 5.05 0 0 0-7.142-7.142L9.49 7.368a5.05 5.05 0 0 0 0 7.142c.41.41 1.075.41 1.485 0zm2.05-5.02a1.05 1.05 0 0 0 0 1.485 2.95 2.95 0 0 1 0 4.172l-3.5 3.5a2.95 2.95 0 1 1-4.171-4.172l1.025-1.025a1.05 1.05 0 0 0-1.485-1.485L3.87 12.99a5.05 5.05 0 0 0 7.142 7.142l3.5-3.5a5.05 5.05 0 0 0 0-7.142 1.05 1.05 0 0 0-1.485 0z" fill="#000000"/></svg>`;
  31. button.innerHTML = `${svgIcon} Copy Real Link`; // Add icon and text
  32.  
  33. button.onclick = () => {
  34. GM_setClipboard(url); // Copy to clipboard
  35. button.textContent = 'Copied!'; // Update button text to indicate success
  36. setTimeout(() => button.remove(), 2000); // Remove button after a short delay
  37. };
  38.  
  39. return button;
  40. };
  41.  
  42. document.addEventListener('contextmenu', function(event){
  43. let target = event.target;
  44.  
  45. // Check if the right-clicked element is a link
  46. if (target.tagName === 'A' && target.href) {
  47. let url = target.href;
  48. let testRE;
  49.  
  50. // Define the regular expressions for each website
  51. if (document.URL.match("http(s|)://www.google")) {
  52. testRE = url.match("url=([^&]*)&");
  53. } else if (document.URL.match("http(s|)://mail.google")) {
  54. testRE = url.match("url\\?q=([^&]*)&");
  55. } else if (document.URL.match("http(s|)://www.facebook")) {
  56. testRE = url.match("u=([^&]*)&");
  57. } else if (document.URL.match("http(s|)://web.facebook")) {
  58. testRE = url.match("u=([^&]*)&");
  59. } else if (document.URL.match("http(s|)://.*search.yahoo")) {
  60. testRE = url.match("RU=([^/]*)/");
  61. }
  62.  
  63. // Decode the URL if a match is found
  64. if (testRE) {
  65. let realURL = decodeURIComponent(testRE[1]);
  66.  
  67. // Create and position the copy button
  68. const button = createCopyButton(realURL);
  69. document.body.appendChild(button);
  70. const rect = target.getBoundingClientRect();
  71. button.style.top = `${rect.top + window.scrollY}px`;
  72. button.style.left = `${rect.right + window.scrollX + 5}px`;
  73. // Show the default context menu
  74. setTimeout(() => {
  75. button.remove();
  76. }, 2000); // Remove button after a short delay
  77. }
  78. }
  79. });
  80. })();