YouTube去广告

简单高效的YouTube去广告脚本,拒绝花里胡哨.这个脚本可以移除所有广告,甚至是视频广告.如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素.如果你有好的建议可以联系我(zgh0118c@gmail.com).

当前为 2023-02-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube去广告
  3. // @name:zh-CN YouTube去广告
  4. // @name:zh-TW YouTube去廣告
  5. // @name:ar YouTube AD Blocker
  6. // @name:bg YouTube AD Blocker
  7. // @name:cs YouTube AD Blocker
  8. // @name:da YouTube AD Blocker
  9. // @name:de YouTube AD Blocker
  10. // @name:el YouTube AD Blocker
  11. // @name:eo YouTube AD Blocker
  12. // @name:es YouTube AD Blocker
  13. // @name:fi YouTube AD Blocker
  14. // @name:fr YouTube AD Blocker
  15. // @name:fr-CA YouTube AD Blocker
  16. // @name:he YouTube AD Blocker
  17. // @name:hu YouTube AD Blocker
  18. // @name:id YouTube AD Blocker
  19. // @name:ko YouTube AD Blocker
  20. // @name:nb YouTube AD Blocker
  21. // @name:nl YouTube AD Blocker
  22. // @name:pl YouTube AD Blocker
  23. // @name:pt-BR YouTube AD Blocker
  24. // @name:ro YouTube AD Blocker
  25. // @name:ru YouTube AD Blocker
  26. // @name:sk YouTube AD Blocker
  27. // @name:sr YouTube AD Blocker
  28. // @name:sv YouTube AD Blocker
  29. // @name:th YouTube AD Blocker
  30. // @name:tr YouTube AD Blocker
  31. // @name:uk YouTube AD Blocker
  32. // @name:ug YouTube AD Blocker
  33. // @name:vi YouTube AD Blocker
  34. // @name:it YouTube AD Blocker
  35. // @name:ja YouTubeの広告を削除します
  36. // @name:kr 포함한 YouTube 광고 차단
  37.  
  38. // @namespace http://tampermonkey.net/
  39. // @version 1.35
  40.  
  41. // @description 简单高效的YouTube去广告脚本,拒绝花里胡哨.这个脚本可以移除所有广告,甚至是视频广告.如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素.如果你有好的建议可以联系我(zgh0118c@gmail.com).
  42. // @description:zh-CN 简单高效的YouTube去广告脚本,拒绝花里胡哨.这个脚本可以移除所有广告,甚至是视频广告.如果你有一丢丢编程知识,可以尝试为常量cssSeletorArr定义元素.如果你有好的建议可以联系我(zgh0118c@gmail.com).
  43. // @description:zh-TW 簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.這個腳本可以移除所有廣告,甚至是視頻廣告.如果你有一丟丟編程知識,可以嘗試為常量cssSeletorArr定義元素.如果你有好的建議可以聯系我(zgh0118c@gmail.com).
  44. // @description:ar Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  45. // @description:bg Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  46. // @description:cs Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  47. // @description:da Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  48. // @description:de Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  49. // @description:el Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  50. // @description:eo Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  51. // @description:es Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  52. // @description:fi Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  53. // @description:fr Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  54. // @description:fr-CA Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  55. // @description:he Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  56. // @description:hu Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  57. // @description:id Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  58. // @description:ko Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  59. // @description:nb Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  60. // @description:nl Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  61. // @description:pl Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  62. // @description:pt-BR Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  63. // @description:ro Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  64. // @description:ru Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  65. // @description:sk Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  66. // @description:sr Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  67. // @description:sv Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  68. // @description:th Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  69. // @description:tr Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  70. // @description:uk Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  71. // @description:ug Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  72. // @description:vi Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  73. // @description:it Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  74. // @description:ja Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  75. // @description:kr Simple and efficient YouTube AD Blocker, no bells and whistles. The script removes all ads, even video ads. If you have a little programming knowledge, you can try editing the constant cssSeletorArr. If you have any good suggestions, you can contact me (zgh0118c@gmail.com).
  76.  
  77. // @author iamfugui
  78. // @match *://*.youtube.com/*
  79. // @icon https://www.google.com/s2/favicons?sz=64&domain=YouTube.com
  80. // @grant none
  81. // @license MIT
  82. // ==/UserScript==
  83. (function() {
  84. 'use strict';
  85.  
  86. //界面广告选择器
  87. const cssSeletorArr = [
  88. `#masthead-ad`,//首页顶部横幅广告. Homepage banner AD1.
  89. `ytd-rich-item-renderer.style-scope.ytd-rich-grid-row:has(.ytd-display-ad-renderer)`,//首页视频排版广告. Homepage video layout AD.
  90. `ytd-rich-section-renderer #dismissible`,//首页中部横幅广告. Homepage banner AD2.
  91. `.video-ads.ytp-ad-module`,//播放器底部广告. Player bottom AD.
  92. `tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)`,//播放页会员促销广告. Play page member promotion AD.
  93. `#related #player-ads`,//播放页评论区右侧推广广告. Play page, right side, promotion AD.
  94. `#related ytd-ad-slot-renderer`,//播放页评论区右侧视频排版广告. Play page, right side, video layout AD.
  95. `ytd-search-pyv-renderer:has(ytd-ad-slot-renderer)`,//搜索页广告. Search page AD.
  96. `ytd-item-section-renderer:has(ytd-ad-slot-renderer)`//搜索页广告2. Search page AD2.
  97. ];
  98.  
  99. /**
  100. * 生成去除广告的css元素style并附加到HTML节点上
  101. * @param {String} styles 样式文本
  102. * @param {String} styleId 元素id
  103. * @return {undefined}
  104. */
  105. function generateRemoveADHTMLElement(styles,styleId) {
  106. //如果已经设置过,退出. If already set.
  107. if (document.getElementById(styleId)) {
  108. return false
  109. }
  110.  
  111. //设置移除广告样式. Remove AD CSS.
  112. let style = document.createElement(`style`);//创建style元素. Create element style.
  113. style.id = styleId;
  114. (document.querySelector(`head`) || document.querySelector(`body`)).appendChild(style);//将节点附加到HTML. Append html node.
  115. style.appendChild(document.createTextNode(styles));//附加样式节点到元素节点. Append element node.
  116. }
  117.  
  118. /**
  119. * 生成去除广告的css文本
  120. * @param {Array} cssSeletorArr 待设置css选择器数组
  121. * @return {String}
  122. */
  123. function generateRemoveADCssText(cssSeletorArr){
  124. cssSeletorArr.forEach((seletor,index)=>{
  125. cssSeletorArr[index]=`${seletor}{display:none!important}`;//遍历并设置样式. Setting css.
  126. });
  127. return cssSeletorArr.join(" ");//拼接成字符串. Return string.
  128. }
  129.  
  130. /**
  131. * 去除播放中的广告
  132. * @return {undefined}
  133. */
  134. function removePlayerAD(){
  135. let timerId =setInterval(function(){
  136. //拥有跳过按钮的广告. Use the Skip button.
  137. let skipButton = document.querySelector(`.ytp-ad-skip-button`);
  138. if(skipButton)
  139. {
  140. skipButton.click();// 跳过广告. Skip AD.
  141. return false;//防止后面错判. Return.
  142. }
  143.  
  144. //片头短广告. Short AD.
  145. let ADShortMsg = document.querySelector(`.video-ads.ytp-ad-module .ytp-ad-player-overlay`);
  146. if(ADShortMsg){
  147. location.href = location.href;//重新加载. Reload.
  148. clearInterval(timerId);
  149. }
  150.  
  151. }, 16);//主流屏幕刷新率为60hz,此设置与16.666666毫秒每帧对应. The mainstream screen refresh rate is 60hz, which corresponds to 16.666666 milliseconds per frame.
  152. }
  153.  
  154. /**
  155. * main函数
  156. */
  157. function main(){
  158. generateRemoveADHTMLElement(generateRemoveADCssText(cssSeletorArr),`removeAD`);//移除界面中的广告. Remove AD from page.
  159. removePlayerAD();//移除播放中的广告. Remove AD from player.
  160. }
  161. main();
  162.  
  163. })();