Greasy Fork is available in English.

Discussions » Creation Requests

Youtube Automatic Display Chapters

§
Posted: 20.04.2021.

Hey,
So I wanted to cleate a script to automatically always open youtube 'Chapters' if a video has them as youtube doesnt have it as an option.
So dont laugh too hard but I repurposed a tiny script(I imagine this is a terrible way to do this lmao):

var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
if(timesRun === 20){
clearInterval(interval);
}
document.getElementsByClassName("ytp-chapter-title ytp-button")[0].click();
}, 100);

but it was almost enough but it needs to be able to check for hash changes so it reruns when not fully refreshing a page.
If anyone can help me to knowing what Im doing I would appreciate it.

window.addEventListener("hashchange", checkForValidUrl, true);

Thanks guys

woxxomMod
§
Posted: 20.04.2021.

Use MutationObserver, here's the entire script:

let lastBtn; 
new MutationObserver(() => {
  const btn = document.querySelector('ytp-chapter-title ytp-button');
  if (btn && lastBtn !== btn) {
    lastBtn = btn;
    btn.click();
  }
}).observe(document, {subtree: true, childList: true});
woxxomMod
§
Posted: 20.04.2021.

Also make sure your @match covers the entire site e.g. @match https://www.youtube.com/*

§
Posted: 20.04.2021.
Edited: 20.04.2021.

Hey,
Seems just a little better then my solution, thank you.
Unfortunately doesn't seem to work though, any ideas?
I tried changing the Selected Div because I just picked one that worked in the first place and didn't know which level would be best.
Yeah I blacklisted youtube, thank you.

§
Posted: 20.04.2021.
Edited: 20.04.2021.

*Doesnt seem to work in any case that is sorry

woxxomMod
§
Posted: 20.04.2021.

Indeed, I made a mistake.

Anyway here's another approach, the entire script:

document.addEventListener('yt-navigate-finish', () => {
  const container = document.querySelector('.ytp-chapter-container');
  const mo = new MutationObserver(() => {
    container.querySelector('button')?.click();
    mo.disconnect();
  });
  mo.observe(container, {attributes: true, attributeFilter: ['style']});
});
§
Posted: 20.04.2021.

Oh wow well done. Looks like it'll work flawlessly.

There's an easy Script you can add to your collection.

I really appreciate it. Thanks a bunch.
Take care,
Hayden.

Post reply

Sign in to post a reply.