Easy Copy URL without Trackers

Removes annoying url trackers parameters and copies the cleaned URL to the clipboard when using Alt+C (or Option+C on Mac).

安装此脚本
作者推荐脚本

您可能也喜欢Remove URL trackers

安装此脚本
  1. // ==UserScript==
  2. // @name Easy Copy URL without Trackers
  3. // @namespace https://github.com/insign/userscripts
  4. // @version 202409181420
  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. // Lista de prefixos de parâmetros a serem removidos da URL.
  15. const paramsToStrip = ['utm_', 'ref', 'gclid', 'gclsrc', 'gs_', 'ga_', '_ga', '_gaq', '__utm', 'fbclid', 'mc_', '_cid', 'epik', 'context']
  16.  
  17. /**
  18. * Verifica se um parâmetro (formato 'chave=valor') deve ser mantido na URL.
  19. * @param {string} param - O parâmetro a ser verificado.
  20. * @returns {boolean} - Retorna true se o parâmetro NÃO começa com nenhum dos prefixos em `paramsToStrip`.
  21. */
  22. function shouldPreserveParam(param) {
  23. return !paramsToStrip.some(prefix => param.startsWith(prefix))
  24. }
  25.  
  26. /**
  27. * Remove os parâmetros de rastreamento de uma URL.
  28. * @param {string} url - A URL original.
  29. * @returns {string} - A URL limpa, sem os parâmetros de rastreamento.
  30. */
  31. function cleanUrl(url) {
  32. // Procura pela query string (parte após '?') e a processa.
  33. return url.replace(/\?([^#]*)/, (match, searchParams) => {
  34. // Divide os parâmetros, filtra mantendo apenas os que devem ser preservados, e junta novamente.
  35. const updatedParams = searchParams
  36. .split('&')
  37. .filter(shouldPreserveParam)
  38. .join('&')
  39. // Retorna a query string limpa ou uma string vazia se não houver parâmetros restantes.
  40. return updatedParams ? '?' + updatedParams : ''
  41. })
  42. }
  43.  
  44. /**
  45. * Copia o texto fornecido para a área de transferência.
  46. * Cria um input temporário, define seu valor, seleciona e executa o comando de cópia.
  47. * @param {string} text - O texto a ser copiado.
  48. */
  49. function copyToClipboard(text) {
  50. // Cria um elemento input temporário
  51. const tempInput = document.createElement('input')
  52. tempInput.value = text // Define o valor como a URL limpa
  53. document.body.appendChild(tempInput) // Adiciona ao corpo do documento
  54. tempInput.select() // Seleciona o conteúdo do input
  55. document.execCommand('copy') // Executa o comando de cópia do navegador
  56. document.body.removeChild(tempInput) // Remove o input temporário
  57. }
  58.  
  59. /**
  60. * Exibe uma notificação deslizante no topo da página.
  61. * Útil para dar feedback visual ao usuário após a cópia.
  62. * @param {string} message - A mensagem a ser exibida na notificação.
  63. */
  64. function showNotification(message) {
  65. // Cria o elemento da notificação
  66. const notification = document.createElement('div')
  67. notification.textContent = message // Define o texto da mensagem
  68.  
  69. // Estilização da notificação (posição fixa no topo, aparência, etc.)
  70. notification.style.position = 'fixed'
  71. notification.style.top = '0'
  72. notification.style.right = '10px'
  73. notification.style.backgroundColor = 'black'
  74. notification.style.color = 'white'
  75. notification.style.padding = '10px'
  76. notification.style.border = '3px solid white'
  77. notification.style.borderTopWidth = '0'
  78. notification.style.borderRadius = '0 0 5px 5px'
  79. notification.style.zIndex = '2147483647' // Garante que fique sobre a maioria dos elementos
  80. notification.style.transform = 'translateY(-100%)' // Começa escondida acima da tela
  81. notification.style.transition = 'transform 0.5s ease' // Efeito de transição suave
  82.  
  83. // Adiciona a notificação ao corpo do documento
  84. document.body.appendChild(notification)
  85.  
  86. // Animação: deslizar para baixo (tornar visível)
  87. setTimeout(() => {
  88. notification.style.transform = 'translateY(0)'
  89. }, 100) // Pequeno atraso para garantir que a transição funcione
  90.  
  91. // Animação: deslizar para cima e remover após um tempo
  92. setTimeout(() => {
  93. notification.style.transform = 'translateY(-100%)' // Desliza de volta para cima
  94. // Remove o elemento do DOM após a animação de subida terminar
  95. setTimeout(() => {
  96. if (document.body.contains(notification)) { // Verifica se ainda existe antes de remover
  97. document.body.removeChild(notification)
  98. }
  99. }, 500) // Tempo da transição de subida
  100. }, 1500) // Tempo que a notificação permanece visível
  101. }
  102.  
  103. // Adiciona um listener para o evento de pressionar tecla.
  104. window.addEventListener('keydown', function(event) {
  105. // Verifica se a tecla Alt (ou Option no Mac) e a tecla 'C' foram pressionadas juntas.
  106. if (event.altKey && event.code === 'KeyC') {
  107. event.preventDefault() // Impede qualquer ação padrão do navegador para Alt+C
  108.  
  109. const currentUrl = location.href // Pega a URL atual
  110. const cleanedUrl = cleanUrl(currentUrl) // Limpa a URL
  111.  
  112. copyToClipboard(cleanedUrl) // Copia a URL limpa
  113.  
  114. // Exibe uma notificação diferente dependendo se a URL foi realmente modificada.
  115. if (currentUrl !== cleanedUrl) {
  116. showNotification('Copied without trackers!')
  117. } else {
  118. showNotification('Copied!')
  119. }
  120. }
  121. })
  122. })()