YouTube去广告

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

目前为 2023-02-16 提交的版本,查看 最新版本

  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.41
  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 #content: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-ad-slot-renderer`,//搜索页广告. Search AD.
  101. ];
  102.  
  103. /**
  104. * 将标准时间格式化
  105. * @param {Date} time 标准时间
  106. * @param {String} format 格式
  107. * @return {String}
  108. */
  109. function moment(time, format = 'YYYY-MM-DD HH:mm:ss') {
  110. // 获取年⽉⽇时分秒
  111. let y = time.getFullYear()
  112. let m = (time.getMonth() + 1).toString().padStart(2, '0')
  113. let d = time.getDate().toString().padStart(2, '0')
  114. let h = time.getHours().toString().padStart(2, '0')
  115. let min = time.getMinutes().toString().padStart(2, '0')
  116. let s = time.getSeconds().toString().padStart(2, '0')
  117. if (format === 'YYYY-MM-DD') {
  118. return `${y}-${m}-${d}`
  119. } else {
  120. return `${y}-${m}-${d} ${h}:${min}:${s}`
  121. }
  122. }
  123.  
  124. /**
  125. * 日志
  126. * @param {String} msg 写入日志信息
  127. * @return {undefined}
  128. */
  129. function setLog(msg) {
  130. }
  131.  
  132. /**
  133. * 获取当前url的参数
  134. * @return {String}
  135. */
  136. function getUrlParams(param) {
  137. // 通过 ? 分割获取后面的参数字符串
  138. let urlStr = location.href.split(`?`)[1]
  139. if(!urlStr){
  140. return ``;
  141. }
  142. // 创建空对象存储参数
  143. let obj = {};
  144. // 再通过 & 将每一个参数单独分割出来
  145. let paramsArr = urlStr.split(`&`)
  146. for(let i = 0,len = paramsArr.length;i < len;i++){
  147. // 再通过 = 将每一个参数分割为 key:value 的形式
  148. let arr = paramsArr[i].split(`=`)
  149. obj[arr[0]] = arr[1];
  150. }
  151. return obj[param]||``;
  152. }
  153.  
  154. /**
  155. * 生成去除广告的css元素style并附加到HTML节点上
  156. * @param {String} styles 样式文本
  157. * @param {String} styleId 元素id
  158. * @return {undefined}
  159. */
  160. function generateRemoveADHTMLElement(styles,styleId) {
  161. //如果已经设置过,退出.
  162. if (document.getElementById(styleId)) {
  163. return false
  164. }
  165.  
  166. //设置移除广告样式.
  167. let style = document.createElement(`style`);//创建style元素.
  168. style.id = styleId;
  169. (document.querySelector(`head`) || document.querySelector(`body`)).appendChild(style);//将节点附加到HTML.
  170. style.appendChild(document.createTextNode(styles));//附加样式节点到元素节点.
  171. console.log(`屏蔽页面广告节点已生成`)
  172. }
  173.  
  174. /**
  175. * 生成去除广告的css文本
  176. * @param {Array} cssSeletorArr 待设置css选择器数组
  177. * @return {String}
  178. */
  179. function generateRemoveADCssText(cssSeletorArr){
  180. cssSeletorArr.forEach((seletor,index)=>{
  181. cssSeletorArr[index]=`${seletor}{display:none!important}`;//遍历并设置样式.
  182. });
  183. return cssSeletorArr.join(` `);//拼接成字符串.
  184. }
  185.  
  186. /**
  187. * 去除播放中的广告
  188. * @return {undefined}
  189. */
  190. function removePlayerAD(){
  191. console.log(`去除视频广告脚本运行中...`)
  192. let currentTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  193. let href = `${location.href.split(`&`)[0]}`;//当前视频链接
  194.  
  195. let timerId =setInterval(function(){
  196. //如果不是播放页就退出,但继续执行定时器,因为YouTube跳转页面时并不会reload页面
  197. let video = document.querySelector(`video`);//这里是故意写在定时器内,load后查询dom太慢
  198. if(!video){
  199. return false;
  200. }
  201.  
  202. //拥有跳过按钮的广告.
  203. let skipButton = document.querySelector(`.ytp-ad-skip-button`);
  204. if(skipButton)
  205. {
  206. skipButton.click();// 跳过广告.
  207. console.log(`使用按钮跳过了一条广告`)
  208. return false;//终止
  209. }
  210.  
  211. //判断用户刚刚是否切换了视频
  212. if(href !== `${location.href.split(`&`)[0]}`){
  213. href = location.href.split(`&`)[0];//当前视频链接
  214. currentTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  215. console.log(`检测到用户切换了视频,已更新播放进度`)
  216. }else{
  217. currentTime = video.currentTime;//未检测到广告,记录播放进度
  218. }
  219.  
  220. //没有跳过按钮的短广告.
  221. let shortAdMsg = document.querySelector(`.video-ads.ytp-ad-module .ytp-ad-player-overlay`);
  222. if(shortAdMsg){
  223. clearInterval(timerId);
  224. location.href = `${href}&t=${currentTime}s`;//重新加载.
  225. return false;//终止
  226. }
  227.  
  228. }, 16);//主流屏幕刷新率为60hz,此设置与16.666666毫秒每帧对应.
  229. }
  230.  
  231. /**
  232. * main函数
  233. */
  234. function main(){
  235. console.log(`YouTube去广告脚本正在运行:${moment(new Date())}`)
  236. generateRemoveADHTMLElement(generateRemoveADCssText(cssSeletorArr),`removeAD`);//移除界面中的广告.
  237. removePlayerAD();//移除播放中的广告.
  238. }
  239.  
  240. main();
  241.  
  242. })();