Skip to content

Instantly share code, notes, and snippets.

@RicoP
Last active May 31, 2019 09:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RicoP/fac032d7f0b94508ca725d77ffb03d62 to your computer and use it in GitHub Desktop.
Save RicoP/fac032d7f0b94508ca725d77ffb03d62 to your computer and use it in GitHub Desktop.
youtube mini ad block (When you don't want to use ad block, but still don't want to manually click the close button for the ad)
// ==UserScript==
// @name youtube mini ad block
// @namespace Violentmonkey Scripts
// @grant none
// @include https://www.youtube.com/*
// ==/UserScript==
var started = false;
var adWholeTime = 0;
function timeStringToSeconds(time) {
var t = time.split(":");
var wholeSeconds = t[0] * 60 + t[1] * 1;
return wholeSeconds;
}
try {
var style = document.createElement("style");
// WebKit hack :(
style.appendChild(document.createTextNode(""));
// Add the <style> element to the page
document.head.appendChild(style);
var sheet = style.sheet;
sheet.insertRule(".video-ads { visibility: hidden; }", 0);
sheet.insertRule(".ad-div { visibility: hidden; position:absolute; }", 0);
}
catch(e) {
console.error(e);
}
window.setInterval(function() {
if(!started) {
console.log("yt mini ad block");
started = true;
}
try {
var buttons = document.getElementsByClassName("videoAdUiSkipButton");
if(buttons.length > 0) {
var adRemainingTimeString = document.getElementsByClassName("videoAdUiAttribution")[0].innerText
var adRemainingTime = timeStringToSeconds(adRemainingTimeString.match(/\d.*/g)[0]);
var wholeSeconds = timeStringToSeconds(document.getElementsByClassName("ytp-time-current")[0].innerText);
if(!adWholeTime) {
adWholeTime = adRemainingTime;
}
console.log(wholeSeconds, adWholeTime, adRemainingTime);
if(wholeSeconds > 30 || adWholeTime - adRemainingTime > 30) {
console.log("skiped. " + wholeSeconds)
document.getElementsByClassName("videoAdUiSkipButton")[0].click();
}
}
else {
adWholeTime = 0;
}
}
catch(e) {
console.error("yt mini ad block", e)
}
}, 16);
window.setInterval(function() {
//autoclick show more
//
if(document.getElementsByClassName("yt-uix-expander yt-uix-expander-collapsed").length == 2) {
document.getElementsByClassName("multirow-shelf-expander")[0].click();
//clicked = true;
}
}, 200);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment