YouTube Shorts URL to Normal URL (Enhanced)

自动将 YouTube Shorts 链接替换为普通视频链接,包括动态导航

  1. // ==UserScript==
  2. // @name YouTube Shorts URL to Normal URL (Enhanced)
  3. // @namespace https://example.com/
  4. // @version 1.1
  5. // @description 自动将 YouTube Shorts 链接替换为普通视频链接,包括动态导航
  6. // @author yy
  7. // @match https://*.youtube.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 检查并转换 Shorts URL 的函数
  16. function convertShortsUrl() {
  17. const currentUrl = window.location.href;
  18. if (currentUrl.includes("/shorts/")) {
  19. const videoCode = currentUrl.split("/shorts/")[1];
  20. const normalUrl = `https://www.youtube.com/watch?v=${videoCode}`;
  21. window.location.replace(normalUrl);
  22. }
  23. }
  24.  
  25. // 初始调用,处理页面加载时的 Shorts URL
  26. convertShortsUrl();
  27.  
  28. // 监听 URL 变化(支持 YouTube 的动态导航)
  29. let lastUrl = window.location.href;
  30. const observer = new MutationObserver(() => {
  31. const newUrl = window.location.href;
  32. if (newUrl !== lastUrl) {
  33. lastUrl = newUrl;
  34. convertShortsUrl();
  35. }
  36. });
  37.  
  38. // 监听文档的变化
  39. observer.observe(document, { subtree: true, childList: true });
  40. })();