YouTube 加速腳本

高效攔截廣告、優化加載速度、屏蔽推薦視頻,並提供自定義功能

  1. // ==UserScript==
  2. // @name YouTube 加速腳本
  3. // @namespace https://tampermonkey.net/
  4. // @version 2.0
  5. // @description 高效攔截廣告、優化加載速度、屏蔽推薦視頻,並提供自定義功能
  6. // @author Weiren
  7. // @match https://www.youtube.com/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. console.log("YouTube 廣告加速與加載優化腳本啟動!");
  16.  
  17. // 可配置選項
  18. const CONFIG = {
  19. blockAds: true,
  20. skipPreRollAds: false,
  21. accelerateAds: true,
  22. disableTransitions: true,
  23. prefetchResources: true,
  24. };
  25.  
  26. // 攔截廣告域名
  27. const adDomains = [
  28. "*://*.doubleclick.net/*",
  29. "*://*.googleadservices.com/*",
  30. "*://*.googlesyndication.com/*",
  31. "*://*.youtube.com/api/stats/*",
  32. "*://*.youtube.com/ptracking",
  33. ];
  34.  
  35. if (CONFIG.blockAds) {
  36. const originalOpen = XMLHttpRequest.prototype.open;
  37. XMLHttpRequest.prototype.open = function (method, url) {
  38. if (adDomains.some(blockedUrl => url.includes(blockedUrl))) {
  39. console.log(`攔截請求:${url}`);
  40. return; // 阻止請求
  41. }
  42. originalOpen.apply(this, arguments);
  43. };
  44. }
  45.  
  46. // 監聽 DOM 變化並移除多餘元素
  47. const observer = new MutationObserver(() => {
  48. if (CONFIG.removeRecommended) {
  49. const recommended = document.querySelector("#related");
  50. if (recommended) recommended.style.display = "none";
  51. }
  52.  
  53. if (CONFIG.skipPreRollAds) {
  54. const adElements = document.querySelectorAll(".ad-showing, .ytp-ad-module");
  55. adElements.forEach(el => el.remove());
  56. }
  57.  
  58. // 廣告加速
  59. if (CONFIG.accelerateAds) {
  60. const video = document.querySelector("video");
  61. const isAdPlaying = document.querySelector(".ad-showing");
  62. if (video && isAdPlaying) {
  63. video.playbackRate = 16; // 設置 16 倍速
  64. console.log("廣告播放速度加速至 16 倍速");
  65. } else if (video) {
  66. video.playbackRate = 1; // 恢復正常播放速度
  67. }
  68. }
  69.  
  70. const mainContent = document.querySelector("#primary");
  71. if (mainContent) mainContent.style.visibility = "visible";
  72. });
  73.  
  74. observer.observe(document.documentElement, { childList: true, subtree: true });
  75.  
  76. // 禁用動畫過渡效果
  77. if (CONFIG.disableTransitions) {
  78. const style = document.createElement("style");
  79. style.innerHTML = `
  80. * {
  81. transition: none !important;
  82. animation: none !important;
  83. }
  84. `;
  85. document.head.appendChild(style);
  86. }
  87.  
  88. // 預加載關鍵資源(僅在主頁時啟用)
  89. if (CONFIG.prefetchResources && location.pathname === "/") {
  90. const links = [
  91. "https://www.youtube.com/feed/trending",
  92. "https://www.youtube.com/feed/library",
  93. ];
  94.  
  95. links.forEach(link => {
  96. const prefetch = document.createElement("link");
  97. prefetch.rel = "prefetch";
  98. prefetch.href = link;
  99. document.head.appendChild(prefetch);
  100. console.log(`已預加載資源:${link}`);
  101. });
  102. }
  103. function skipAd() {
  104. var skipButton = document.querySelector('.ytp-ad-text.ytp-ad-skip-button-text');
  105. if (skipButton) {
  106. skipButton.click();
  107. console.log("Click button");
  108. }
  109. }
  110.  
  111. // 設定時間監測
  112. var timer = setInterval(skipAd, 1); // 1000毫秒 = 1秒
  113.  
  114. console.log("YouTube 廣告加速與加載優化腳本運行中!");
  115. })();

QingJ © 2025

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