Remove YouTube Tracking Parameters and Convert Share Links

Removes tracking parameters from all YouTube links and converts them to shortened youtu.be links within the share box.

目前为 2024-05-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Remove YouTube Tracking Parameters and Convert Share Links
  3. // @version 1.5
  4. // @description Removes tracking parameters from all YouTube links and converts them to shortened youtu.be links within the share box.
  5. // @author kpganon
  6. // @license MIT
  7. // @namespace https://github.com/kpg-anon/scripts
  8. // @match *://*.youtube.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function removeTrackingParameters(input) {
  16. if (input && input.value) {
  17. let newValue = input.value
  18. .replace(/(?:&|\?)si=[^&]+/, '')
  19. .replace(/(?:&|\?)pp=[^&]+/, '')
  20. .replace(/^([^?]+)&/, '$1?')
  21. .replace(/(?:youtube\.com\/shorts\/|youtube\.com\/live\/|www\.youtube\.com\/watch\?v=|music\.youtube\.com\/watch\?v=)([a-zA-Z0-9_-]+)/, 'youtu.be/$1')
  22. .replace(/www\.youtu\.be/, 'youtu.be');
  23.  
  24. if (input.value !== newValue) {
  25. input.value = newValue;
  26. }
  27. }
  28. }
  29.  
  30. function handleInputChange() {
  31. const shareInput = document.querySelector('yt-share-target-renderer input');
  32. removeTrackingParameters(shareInput);
  33. }
  34.  
  35. const observer = new MutationObserver((mutations) => {
  36. for (const mutation of mutations) {
  37. if (mutation.addedNodes.length) {
  38. for (const node of mutation.addedNodes) {
  39. if (node.nodeType === Node.ELEMENT_NODE && node.querySelector('yt-share-target-renderer')) {
  40. const intervalId = setInterval(handleInputChange, 50);
  41.  
  42. const closeButton = node.querySelector('[aria-label="Close"]');
  43. if (closeButton) {
  44. closeButton.addEventListener('click', () => {
  45. clearInterval(intervalId);
  46. });
  47. }
  48. }
  49. }
  50. }
  51. }
  52. });
  53.  
  54. observer.observe(document.body, { childList: true, subtree: true });
  55.  
  56. setInterval(() => {
  57. document.querySelectorAll('input, a').forEach(element => {
  58. if (element.tagName.toLowerCase() === 'input') {
  59. removeTrackingParameters(element);
  60. } else if (element.tagName.toLowerCase() === 'a' && /\/watch\?v=/.test(element.href)) {
  61. element.href = element.href.replace(/(?:&|\?)si=[^&]+/, '').replace(/(?:&|\?)pp=[^&]+/, '')
  62. .replace(/(?:youtube\.com\/shorts\/|youtube\.com\/live\/|www\.youtube\.com\/watch\?v=|music\.youtube\.com\/watch\?v=)([a-zA-Z0-9_-]+)/, 'youtu.be/$1')
  63. .replace(/www\.youtu\.be/, 'youtu.be');
  64. }
  65. });
  66. }, 50);
  67. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址