Remove URL trackers and Copy

Removes annoying url trackers parameters and copies the cleaned URL to the clipboard when using alt+c (or option+c on mac)

当前为 2024-09-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Remove URL trackers and Copy
  3. // @namespace https://github.com/insign
  4. // @version 202409172110
  5. // @description Removes annoying url trackers parameters and copies the cleaned URL to the clipboard when using alt+c (or option+c on mac)
  6. // @match *://*/*
  7. // @author Hélio <open@helio.me>
  8. // @license WTFPL
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. const paramsToStrip = ['utm_', 'ref', 'gclid', 'gclsrc', 'gs_', 'ga_', '_ga', '_gaq', '__utm', 'fbclid', 'mc_', '_cid', 'epik', 'context'];
  15.  
  16. function shouldPreserveParam(param) {
  17. return !paramsToStrip.some(prefix => param.startsWith(prefix));
  18. }
  19.  
  20. function cleanUrl(url) {
  21. return url.replace(/\?([^#]*)/, (match, searchParams) => {
  22. const updatedParams = searchParams
  23. .split('&')
  24. .filter(shouldPreserveParam)
  25. .join('&');
  26. return updatedParams ? '?' + updatedParams : '';
  27. });
  28. }
  29.  
  30. // Copy the cleaned URL to clipboard
  31. function copyToClipboard(url) {
  32. const tempInput = document.createElement('input');
  33. tempInput.value = url;
  34. document.body.appendChild(tempInput);
  35. tempInput.select();
  36. document.execCommand('copy');
  37. document.body.removeChild(tempInput);
  38. }
  39.  
  40. // Display a sliding notification after copying
  41. function showNotification(message) {
  42. const notification = document.createElement('div');
  43. notification.textContent = message;
  44. notification.style.position = 'fixed';
  45. notification.style.top = '0';
  46. notification.style.right = '10px';
  47. notification.style.backgroundColor = 'black';
  48. notification.style.color = 'white';
  49. notification.style.padding = '10px';
  50. notification.style.border = '3px solid white';
  51. notification.style.borderTopWidth = '0';
  52. notification.style.borderRadius = '0 0 5px 5px';
  53. notification.style.zIndex = '1000';
  54. notification.style.transform = 'translateY(-100%)'; // Start hidden above the viewport
  55. notification.style.transition = 'transform 0.5s ease'; // Smooth sliding effect
  56.  
  57. document.body.appendChild(notification);
  58.  
  59. // Slide down into view
  60. setTimeout(() => {
  61. notification.style.transform = 'translateY(0)';
  62. }, 100); // Slight delay to allow CSS to apply
  63.  
  64. // Slide up and remove after 1.5 seconds
  65. setTimeout(() => {
  66. notification.style.transform = 'translateY(-100%)'; // Slide back up
  67. setTimeout(() => {
  68. document.body.removeChild(notification); // Remove after sliding up
  69. }, 500); // Time for the slide-up transition to finish
  70. }, 1500);
  71. }
  72.  
  73. // Event listener for the shortcut
  74. window.addEventListener('keydown', function(event) {
  75. if (event.altKey && event.code === 'KeyC') {
  76. event.preventDefault();
  77.  
  78. const currentUrl = location.href;
  79. const cleanedUrl = cleanUrl(currentUrl);
  80.  
  81. copyToClipboard(cleanedUrl);
  82.  
  83. // Check if the original URL and cleaned URL are different
  84. if (currentUrl !== cleanedUrl) {
  85. showNotification('Copied without trackers');
  86. } else {
  87. showNotification('Copied!');
  88. }
  89. }
  90. });
  91. })();