Remove YouTube Tracking Parameters and Convert Shorts Links

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

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

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