How to skip ad after 5 seconds using the YouTube JS API?
Asked Answered
N

4

5

I can do almost everything using the JavaScript API to take control of the YouTube Player.

player = document.getElementById('movie_player');

But I can't find any function that let me skip the ad video (if any) after 5 seconds.

How can I achieve this?

Narrative answered 20/7, 2015 at 17:34 Comment(4)
This is probably to ensure the user wants to skip the ad. It might even be against some terms and conditions to do so, as you would automatically be skipping over content that someone paid to be shown. So it would not surprise me hat there wouldn't be.Hennie
@Hennie It should be that. However, I can still skip the ad emulating a mouse press, but it's not a clean solution.Narrative
I agree, and I can see why you would want to, just putting that point out there. I know that if the ads can't load, it skips over them. This might mean that if you can incorporate an adblock of some sort (I don't know if that is possible in your context or not) that would keep ads from loading in the first place (again, probably against some terms and conditions)Hennie
@Hennie Thanks for the tip. I'll try that.Narrative
P
4

I could not find a clean solution for this. But below is what I did,

Option 1:

add the below code in a bookmark,

javascript:(function(){setInterval(function() { var $cross = document.getElementsByClassName("ytp-ad-overlay-close-container")[0]; var $skip = document.getElementsByClassName("ytp-ad-skip-button")[0]; if ($cross != undefined) $cross.click(); if ($skip != undefined) $skip.click() }, 2000)})();

Adding a JS code in bookmark is easy,

  • just click on the bookmark icon
  • click on more button

add above code in the URL and give it a name, it will now appear in the bookmark tab.

Whenever you open any youtube video just click on the bookmark you created to skip

Option 2(better option):

  1. Install chrome extension Requestly
  2. Click on New Rule
  3. Select Insert Scripts
  4. Give youtube in request url contains.
  5. Click on Add Custom Script and select Custom Code
  6. Insert Below code and click on save
setInterval(function() { 
            var $cross = document.getElementsByClassName("ytp-ad-overlay-close-container")[0]; 
            var $skip = document.getElementsByClassName("ytp-ad-skip-button")[0]; 
            if ($cross != undefined) $cross.click(); 
            if ($skip != undefined) $skip.click() 
            }, 2000);

Requestly will run this code whenever youtube opens, and it will skip the ads and ad videos.

Note: this is a dirty solution and I use it and it works for me. There might be better solutions present.

Prot answered 23/6, 2020 at 8:32 Comment(0)
A
1

You just need to detect id of skip button (if available). Then call event click of it. Something looks like this:

setTimeout(function(){
  var _skip = $('#skip_button_id');
  if(_skip!=null)
    _skip.click();
}, 1000);

This is not real code. Just let you know how to do.

Ailyn answered 19/11, 2017 at 2:16 Comment(0)
C
1
const skip_add = (clazz) => 
 {
  const buttons = document.getElementsByClassName(clazz);
  for (const button of buttons) 
    {
     button.click();
     console.log("No More Ad");
    }
 }

 setInterval(() => 
 {
  skip_add("ytp-ad-skip-button-text");
  skip_add("ytp-ad-overlay-close-button");
 }, 1000);

this is not very elegant code , but try to check if ad is there every 1000ms. play with the time to optimize or add an if loop to counter check

Coit answered 15/11, 2020 at 7:37 Comment(0)
S
1

Use this simple javascript bookmarklet:

javascript:document.querySelector('video').currentTime=document.querySelector('video').duration
Stargell answered 6/10, 2022 at 2:6 Comment(1)
hey there and thanks for contributing to stackoverflow! could you explain why your answer works, and where to add this bookmarklet?Amadaamadas

© 2022 - 2024 — McMap. All rights reserved.