YouTube Auto Skip Ads

autoclicks on youtube's skip ad buttons once they appear, also auto-closes banner ads

  1. // ==UserScript==
  2. // @name YouTube Auto Skip Ads
  3. // @version 0.9
  4. // @description autoclicks on youtube's skip ad buttons once they appear, also auto-closes banner ads
  5. // @author RustyPrimeLUX
  6. // @match https://www.youtube.com/*
  7. // @license MIT
  8. // @namespace none
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. var currentVideoTime = 0;
  15.  
  16. var videoPlayer = document.querySelector(".video-stream.html5-main-video");
  17. var skipAdBtn = document.querySelector("div.ytp-ad-text.ytp-ad-skip-button-text") || document.querySelector(".ytp-skip-ad-button") || document.querySelector(".ytp-skip-ad-button__text") || document.querySelector(".ytp-ad-skip-button-text-centered");
  18. var closeBannerBtn = document.querySelector("button.ytp-ad-overlay-close-button");
  19. var errorScreen = document.querySelector("#error-screen");
  20.  
  21. setInterval(function(){
  22. if (getVideoPlayer() && isVisible(videoPlayer)){
  23. var currentTime = Math.floor(videoPlayer.currentTime);
  24. if (currentTime > 30)
  25. {
  26. currentVideoTime = currentTime;
  27. }
  28. }
  29.  
  30. if(getSkipAdsButton() && isVisible(skipAdBtn)){
  31. skipAdBtn.click();
  32. }
  33.  
  34. if(getCloseBannerButton() && isVisible(closeBannerBtn)){
  35. closeBannerBtn.click();
  36. }
  37.  
  38. if (getErrorScreen() && isVisible(errorScreen)){
  39. if (currentVideoTime == 0)
  40. {
  41. window.location.reload();
  42. }
  43. else{
  44. var currentLink = window.location.href;
  45. var playAtTimestampQuery = "&t=";
  46.  
  47. if (currentLink.includes(playAtTimestampQuery)){
  48. currentLink = currentLink.split(playAtTimestampQuery)[0];
  49. }
  50.  
  51. var linkWithTimestamp = currentLink + playAtTimestampQuery + currentVideoTime;
  52. window.location.href = linkWithTimestamp;
  53. }
  54. }
  55. }, 1000);
  56.  
  57. function getVideoPlayer(){
  58. if (videoPlayer == null || (videoPlayer != null && !document.body.contains(videoPlayer))){
  59. videoPlayer = document.querySelector(".video-stream.html5-main-video");
  60. }
  61. return videoPlayer != null;
  62. }
  63.  
  64. function getSkipAdsButton() {
  65. if (skipAdBtn == null || (skipAdBtn != null && !document.body.contains(skipAdBtn))){
  66. skipAdBtn = document.querySelector("div.ytp-ad-text.ytp-ad-skip-button-text") || document.querySelector(".ytp-skip-ad-button") || document.querySelector(".ytp-skip-ad-button__text") || document.querySelector(".ytp-ad-skip-button-text-centered");
  67. }
  68. return skipAdBtn != null;
  69. }
  70.  
  71. function getCloseBannerButton() {
  72. if (closeBannerBtn == null || (closeBannerBtn != null && !document.body.contains(closeBannerBtn))){
  73. closeBannerBtn = document.querySelector("button.ytp-ad-overlay-close-button");
  74. }
  75. return closeBannerBtn != null;
  76. }
  77.  
  78. function getErrorScreen(){
  79. if (errorScreen == null || (errorScreen != null && !document.body.contains(errorScreen))){
  80. errorScreen = document.querySelector("#error-screen");
  81. }
  82. return errorScreen != null;
  83. }
  84.  
  85. function isVisible(el) {
  86. return (el.offsetParent !== null)
  87. }
  88. })();