YouTube去广告

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

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

  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.44
  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. let lastTime = parseInt(getUrlParams(`t`))||0;//由于youtube出现广告前是先将进度条归零再进行广告node的更新,故此将上一次进度记录
  104. let currentTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  105. let videoLink = `${location.href.split(`&`)[0]}`;//当前视频链接
  106. let video;
  107.  
  108. /**
  109. * 将标准时间格式化
  110. * @param {Date} time 标准时间
  111. * @param {String} format 格式
  112. * @return {String}
  113. */
  114. function moment(time, format = 'YYYY-MM-DD HH:mm:ss') {
  115. // 获取年⽉⽇时分秒
  116. let y = time.getFullYear()
  117. let m = (time.getMonth() + 1).toString().padStart(2, '0')
  118. let d = time.getDate().toString().padStart(2, '0')
  119. let h = time.getHours().toString().padStart(2, '0')
  120. let min = time.getMinutes().toString().padStart(2, '0')
  121. let s = time.getSeconds().toString().padStart(2, '0')
  122. if (format === 'YYYY-MM-DD') {
  123. return `${y}-${m}-${d}`
  124. } else {
  125. return `${y}-${m}-${d} ${h}:${min}:${s}`
  126. }
  127. }
  128.  
  129. /**
  130. * 获取当前url的参数,如果要查询特定参数请传参
  131. * @param {String} 要查询的参数
  132. * @return {String || Object}
  133. */
  134. function getUrlParams(param) {
  135. // 通过 ? 分割获取后面的参数字符串
  136. let urlStr = location.href.split(`?`)[1]
  137. if(!urlStr){
  138. return ``;
  139. }
  140. // 创建空对象存储参数
  141. let obj = {};
  142. // 再通过 & 将每一个参数单独分割出来
  143. let paramsArr = urlStr.split(`&`)
  144. for(let i = 0,len = paramsArr.length;i < len;i++){
  145. // 再通过 = 将每一个参数分割为 key:value 的形式
  146. let arr = paramsArr[i].split(`=`)
  147. obj[arr[0]] = arr[1];
  148. }
  149.  
  150. if(!param){
  151. return obj;
  152. }
  153.  
  154. return obj[param]||``;
  155. }
  156.  
  157. /**
  158. * 得到跳过链接
  159. * @return {String}
  160. */
  161. function getSkipAdUrl(){
  162. let urlParams = getUrlParams();
  163. let url = `${videoLink}`;
  164. for(let key in urlParams){
  165. if(key !== `v` && key !== `t`){
  166. url = `${url}&${key}=${urlParams[key]}`
  167. }
  168. }
  169. return `${url}&t=${parseInt(lastTime)}s`;
  170. }
  171.  
  172. /**
  173. * 生成去除广告的css元素style并附加到HTML节点上
  174. * @param {String} styles 样式文本
  175. * @param {String} styleId 元素id
  176. * @return {undefined}
  177. */
  178. function generateRemoveADHTMLElement(styles,styleId) {
  179. //如果已经设置过,退出.
  180. if (document.getElementById(styleId)) {
  181. return false
  182. }
  183.  
  184. //设置移除广告样式.
  185. let style = document.createElement(`style`);//创建style元素.
  186. style.id = styleId;
  187. (document.querySelector(`head`) || document.querySelector(`body`)).appendChild(style);//将节点附加到HTML.
  188. style.appendChild(document.createTextNode(styles));//附加样式节点到元素节点.
  189. console.log(`屏蔽页面广告节点已生成`)
  190. }
  191.  
  192. /**
  193. * 生成去除广告的css文本
  194. * @param {Array} cssSeletorArr 待设置css选择器数组
  195. * @return {String}
  196. */
  197. function generateRemoveADCssText(cssSeletorArr){
  198. cssSeletorArr.forEach((seletor,index)=>{
  199. cssSeletorArr[index]=`${seletor}{display:none!important}`;//遍历并设置样式.
  200. });
  201. return cssSeletorArr.join(` `);//拼接成字符串.
  202. }
  203.  
  204.  
  205. /**
  206. * 检测用户切换了视频
  207. * @return {undefined}
  208. */
  209. function switchVideoHook(){
  210. if(videoLink !== `${location.href.split(`&`)[0]}`){
  211. videoLink = location.href.split(`&`)[0];//更新链接
  212. lastTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  213. currentTime = parseInt(getUrlParams(`t`))||0;//根据url初始化当前播放时间s
  214. console.log(`检测到用户切换了视频,已更新播放进度`)
  215. }
  216. }
  217.  
  218. /**
  219. * 去除播放中的广告
  220. * @return {undefined}
  221. */
  222. function removePlayerAD(){
  223.  
  224. //广告节点监听
  225. const targetNode = document.querySelector(`.video-ads.ytp-ad-module`);
  226. const config = {childList: true, subtree: true };// 监听目标节点本身与子树下节点的变动
  227. // 当观察到变动时执行的回调函数
  228. const callback = function (mutationsList, observer) {
  229. switchVideoHook();//检测用户是否切换了视频
  230.  
  231. //拥有跳过按钮的广告.
  232. let skipButton = document.querySelector(`.ytp-ad-skip-button`);
  233. if(skipButton)
  234. {
  235. skipButton.click();// 跳过广告.
  236. console.log(`使用按钮跳过了一条广告`)
  237. return false;//终止
  238. }
  239.  
  240. //没有跳过按钮的短广告.
  241. let shortAdMsg = document.querySelector(`.video-ads.ytp-ad-module .ytp-ad-player-overlay`);
  242. if(shortAdMsg){
  243. console.log(`查看上一次进度:`,lastTime);
  244. console.log(`查看当前进度:`,currentTime);
  245. location.href = getSkipAdUrl();//得到跳转的url,重新加载.
  246. clearInterval(timerId);//跳转界面,清理计时器
  247. observer.disconnect();//跳转界面,停止观察
  248. return false;//终止
  249. }
  250.  
  251. console.log(`刚刚监听到了节点变化但都没有处理`,skipButton,shortAdMsg)
  252.  
  253. }
  254. // 创建一个观察器实例并传入回调函数
  255. const observer = new MutationObserver(callback);
  256. // 以上述配置开始观察广告节点
  257. observer.observe(targetNode, config);
  258.  
  259. //定时更新信息
  260. let timerId =setInterval(function(){
  261. switchVideoHook();//检测用户是否切换了视频
  262.  
  263. //如果不是播放页就退出,因为youtube跳转页面时并不会reload页面,所以继续执行定时器,
  264. video = document.querySelector(`video`);
  265. if(!video){
  266. return false;
  267. }
  268. videoLink = location.href.split(`&`)[0];//更新链接
  269. lastTime = currentTime;
  270. currentTime = video.currentTime;//未检测到广告,记录播放进度
  271.  
  272. console.log(`记录当前进度`,currentTime)
  273. },1000);//太快广告节点没有出来会判断错误
  274.  
  275. console.log(`去除视频广告脚本运行中...`)
  276. }
  277.  
  278.  
  279. /**
  280. * main函数
  281. */
  282. function main(){
  283. console.log(`YouTube去广告脚本正在运行:${moment(new Date())}`)
  284. generateRemoveADHTMLElement(generateRemoveADCssText(cssSeletorArr),`removeAD`);//移除界面中的广告.
  285. removePlayerAD();//移除播放中的广告.
  286. }
  287.  
  288. main();
  289.  
  290. })();