Remove YouTube Tracking Parameters and Convert Share Links

Removes tracking parameters from all YouTube links and converts both /shorts/ and music.youtube.com links to normal (shortened) video links within the share box.

目前为 2023-12-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Remove YouTube Tracking Parameters and Convert Share Links
  3. // @version 1.3
  4. // @description Removes tracking parameters from all YouTube links and converts both /shorts/ and music.youtube.com links to normal (shortened) video 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\/|www\.youtube\.com\/watch\?v=|music\.youtube\.com\/watch\?v=)([a-zA-Z0-9_-]+)/, 'youtu.be/$2');
  22.  
  23. if (input.value !== newValue) {
  24. input.value = newValue;
  25. }
  26. }
  27. }
  28.  
  29. function handleInputChange() {
  30. const shareInput = document.querySelector('yt-share-target-renderer input');
  31. removeTrackingParameters(shareInput);
  32. }
  33.  
  34. const observer = new MutationObserver((mutations) => {
  35. for (const mutation of mutations) {
  36. if (mutation.addedNodes.length) {
  37. for (const node of mutation.addedNodes) {
  38. if (node.nodeType === Node.ELEMENT_NODE && node.querySelector('yt-share-target-renderer')) {
  39. const intervalId = setInterval(handleInputChange, 50);
  40.  
  41. const closeButton = node.querySelector('[aria-label="Close"]');
  42. if (closeButton) {
  43. closeButton.addEventListener('click', () => {
  44. clearInterval(intervalId);
  45. });
  46. }
  47. }
  48. }
  49. }
  50. }
  51. });
  52.  
  53. observer.observe(document.body, { childList: true, subtree: true });
  54.  
  55. setInterval(() => {
  56. document.querySelectorAll('input, a').forEach(element => {
  57. if (element.tagName.toLowerCase() === 'input') {
  58. removeTrackingParameters(element);
  59. } else if (element.tagName.toLowerCase() === 'a' && /\/watch\?v=/.test(element.href)) {
  60. element.href = element.href.replace(/(\&|\?)si=[^&]+/, '').replace(/(\&|\?)pp=[^&]+/, '');
  61. }
  62. });
  63. }, 50);
  64. })();

QingJ © 2025

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