YouTube去广告

这个脚本可以移除所有广告,包括所有视频广告.简单高效的YouTube去广告脚本,拒绝花里胡哨.你可以尝试为常量cssSeletorArr定义元素.

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

  1. // ==UserScript==
  2. // @name YouTube去广告
  3. // @name:zh-CN YouTube去广告
  4. // @name:zh-TW YouTube去廣告
  5. // @name:zh-HK YouTube去廣告
  6. // @name:zh-MO YouTube去廣告
  7. // @name:ar YouTube AD Blocker
  8. // @name:bg YouTube AD Blocker
  9. // @name:cs YouTube AD Blocker
  10. // @name:da YouTube AD Blocker
  11. // @name:de YouTube AD Blocker
  12. // @name:el YouTube AD Blocker
  13. // @name:eo YouTube AD Blocker
  14. // @name:es YouTube AD Blocker
  15. // @name:fi YouTube AD Blocker
  16. // @name:fr YouTube AD Blocker
  17. // @name:fr-CA YouTube AD Blocker
  18. // @name:he YouTube AD Blocker
  19. // @name:hu YouTube AD Blocker
  20. // @name:id YouTube AD Blocker
  21. // @name:nb YouTube AD Blocker
  22. // @name:nl YouTube AD Blocker
  23. // @name:pl YouTube AD Blocker
  24. // @name:pt-BR YouTube AD Blocker
  25. // @name:ro YouTube AD Blocker
  26. // @name:ru YouTube AD Blocker
  27. // @name:sk YouTube AD Blocker
  28. // @name:sr YouTube AD Blocker
  29. // @name:sv YouTube AD Blocker
  30. // @name:th YouTube AD Blocker
  31. // @name:tr YouTube AD Blocker
  32. // @name:uk YouTube AD Blocker
  33. // @name:ug YouTube AD Blocker
  34. // @name:vi YouTube AD Blocker
  35. // @name:it YouTube AD Blocker
  36. // @name:ja YouTubeの広告を削除します
  37. // @name:kr 포함한 YouTube 광고 차단
  38. // @name:ko 포함한 YouTube 광고 차단
  39.  
  40. // @namespace http://tampermonkey.net/
  41. // @version 1.38
  42.  
  43. // @description 这个脚本可以移除所有广告,包括所有视频广告.简单高效的YouTube去广告脚本,拒绝花里胡哨.你可以尝试为常量cssSeletorArr定义元素.
  44. // @description:zh-CN 这个脚本可以移除所有广告,包括所有视频广告.简单高效的YouTube去广告脚本,拒绝花里胡哨.你可以尝试为常量cssSeletorArr定义元素.
  45. // @description:zh-TW 這個腳本可以移除所有廣告,包括所有視頻廣告.簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.你可以嘗試為常量cssSeletorArr定義元素.
  46. // @description:zh-HK 這個腳本可以移除所有廣告,包括所有視頻廣告.簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.你可以嘗試為常量cssSeletorArr定義元素.
  47. // @description:zh-MO 這個腳本可以移除所有廣告,包括所有視頻廣告.簡單高效的YouTube去廣告腳本,拒絕花裏胡哨.你可以嘗試為常量cssSeletorArr定義元素.
  48. // @description:ar The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  49. // @description:bg The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  50. // @description:cs The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  51. // @description:da The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  52. // @description:de The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  53. // @description:el The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  54. // @description:eo The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  55. // @description:es The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  56. // @description:fi The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  57. // @description:fr The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  58. // @description:fr-CA The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  59. // @description:he The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  60. // @description:hu The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  61. // @description:id The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  62. // @description:nb The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  63. // @description:nl The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  64. // @description:pl The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  65. // @description:pt-BR The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  66. // @description:ro The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  67. // @description:ru The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  68. // @description:sk The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  69. // @description:sr The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  70. // @description:sv The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  71. // @description:th The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  72. // @description:tr The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  73. // @description:uk The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  74. // @description:ug The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  75. // @description:vi The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  76. // @description:it The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  77. // @description:ja The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  78. // @description:kr The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  79. // @description:ko The script removes all ads, includes all video ads. Simple and efficient YouTube AD Blocker, no bells and whistles. You can try editing the constant cssSeletorArr.
  80.  
  81.  
  82. // @author iamfugui
  83. // @match *://*.youtube.com/*
  84. // @icon https://www.google.com/s2/favicons?sz=64&domain=YouTube.com
  85. // @grant none
  86. // @license MIT
  87. // ==/UserScript==
  88. (function() {
  89. `use strict`;
  90.  
  91. //界面广告选择器
  92. const cssSeletorArr = [
  93. `#masthead-ad`,//首页顶部横幅广告. Banner AD at the top of the homepage.
  94. `ytd-rich-item-renderer.style-scope.ytd-rich-grid-row:has(.ytd-display-ad-renderer)`,//首页视频排版广告. Video layout AD in the homepage.
  95. `ytd-rich-section-renderer #dismissible`,//首页中部横幅广告. Banner AD in the middle of the homepage.
  96. `.video-ads.ytp-ad-module`,//播放器底部广告. AD at the bottom of the player.
  97. `tp-yt-paper-dialog:has(yt-mealbar-promo-renderer)`,//播放页会员促销广告. Member promotion AD on the play page.
  98. `#related #player-ads`,//播放页评论区右侧推广广告. Promotion AD on the right side of the play page.
  99. `#related ytd-ad-slot-renderer`,//播放页评论区右侧视频排版广告. Video layout AD on the right side of the play page.
  100. `ytd-search-pyv-renderer:has(ytd-ad-slot-renderer)`,//搜索页广告. Search AD.
  101. `ytd-item-section-renderer:has(ytd-ad-slot-renderer)`//搜索页广告2. Search AD2.
  102. ];
  103.  
  104. /**
  105. * 获取当前url的参数
  106. * @return {String}
  107. */
  108. function getUrlParams(param) {
  109. // 通过 ? 分割获取后面的参数字符串
  110. let urlStr = location.href.split(`?`)[1]
  111. // 创建空对象存储参数
  112. let obj = {};
  113. // 再通过 & 将每一个参数单独分割出来
  114. let paramsArr = urlStr.split(`&`)
  115. for(let i = 0,len = paramsArr.length;i < len;i++){
  116. // 再通过 = 将每一个参数分割为 key:value 的形式
  117. let arr = paramsArr[i].split(`=`)
  118. obj[arr[0]] = arr[1];
  119. }
  120. return obj[param]||``;
  121. }
  122.  
  123. /**
  124. * 生成去除广告的css元素style并附加到HTML节点上
  125. * @param {String} styles 样式文本
  126. * @param {String} styleId 元素id
  127. * @return {undefined}
  128. */
  129. function generateRemoveADHTMLElement(styles,styleId) {
  130. //如果已经设置过,退出.
  131. if (document.getElementById(styleId)) {
  132. return false
  133. }
  134.  
  135. //设置移除广告样式.
  136. let style = document.createElement(`style`);//创建style元素.
  137. style.id = styleId;
  138. (document.querySelector(`head`) || document.querySelector(`body`)).appendChild(style);//将节点附加到HTML.
  139. style.appendChild(document.createTextNode(styles));//附加样式节点到元素节点.
  140. }
  141.  
  142. /**
  143. * 生成去除广告的css文本
  144. * @param {Array} cssSeletorArr 待设置css选择器数组
  145. * @return {String}
  146. */
  147. function generateRemoveADCssText(cssSeletorArr){
  148. cssSeletorArr.forEach((seletor,index)=>{
  149. cssSeletorArr[index]=`${seletor}{display:none!important}`;//遍历并设置样式.
  150. });
  151. return cssSeletorArr.join(` `);//拼接成字符串.
  152. }
  153.  
  154. /**
  155. * 去除播放中的广告
  156. * @return {undefined}
  157. */
  158. function removePlayerAD(){
  159. let currentTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  160.  
  161. let timerId =setInterval(function(){
  162.  
  163. let video = document.querySelector(`video`);
  164.  
  165. //不是播放页退出
  166. if(!video){
  167. clearInterval(timerId);
  168. return false;
  169. }
  170.  
  171. //拥有跳过按钮的广告.
  172. let skipButton = document.querySelector(`.ytp-ad-skip-button`);
  173. if(skipButton)
  174. {
  175. skipButton.click();// 跳过广告.
  176. return false;//终止
  177. }
  178.  
  179. //没有跳过按钮的短广告.
  180. let shortAdMsg = document.querySelector(`.video-ads.ytp-ad-module .ytp-ad-player-overlay`);
  181. if(shortAdMsg){
  182. clearInterval(timerId);
  183. let href = `${location.href.split("&")[0]}&t=${currentTime}s`;
  184. location.href = href;//重新加载.
  185. return false;//终止
  186. }
  187.  
  188. currentTime = video.currentTime;//未检测到广告,记录播放进度
  189.  
  190. }, 16);//主流屏幕刷新率为60hz,此设置与16.666666毫秒每帧对应.
  191. }
  192.  
  193. /**
  194. * main函数
  195. */
  196. function main(){
  197. generateRemoveADHTMLElement(generateRemoveADCssText(cssSeletorArr),`removeAD`);//移除界面中的广告.
  198. removePlayerAD();//移除播放中的广告.
  199. }
  200. main();
  201.  
  202. })();