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?

4
  • 2
    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.
    – Snappawapa
    Jul 20, 2015 at 17:37
  • @Snappawapa It should be that. However, I can still skip the ad emulating a mouse press, but it's not a clean solution.
    – cdonts
    Jul 20, 2015 at 18:12
  • 1
    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)
    – Snappawapa
    Jul 20, 2015 at 18:54
  • @Snappawapa Thanks for the tip. I'll try that.
    – cdonts
    Jul 21, 2015 at 0:29

4 Answers 4

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.

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.

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

1

Use this simple javascript bookmarklet:

javascript:document.querySelector('video').currentTime=document.querySelector('video').duration
1
  • 1
    hey there and thanks for contributing to stackoverflow! could you explain why your answer works, and where to add this bookmarklet? Oct 10, 2022 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.