0

I am looking to get youtube video page's related video's title element.

This is the javascript I am using, for some reason I could not get it to work. Here's the code,

var span = document.getElementById('title').style;
if(span.font-size=='13')    {
document.cookie="VISITOR_INFO1_LIVE=-fntp2HbKFI;%20path=/;%20domain=.youtube.com"; 
window.location.reload();
}
else{
document.cookie="VISITOR_INFO1_LIVE=;%20path=/;%20domain=.youtube.com";
window.location.reload();
}

The code above turns on the Youtube experiment on the video pages and off when executed again.

The experiment is on the video page, it increases the related video's title's font size. It gets turned on with a cookie value (VISITOR_INFO1_LIVE) set to some value, and reloads the page. Now the related video's title's font size increases from 13 px to 19 px.

I am unable to get that title element with getElementById. When logged to console, the error is get is TypeError: document.getElementById(...) is null. I get this even for element id, 'list-video-item'

Then with an another execution of the code, it checks for the title's font size which is 19 px. The code now removes the cookie value and reloads the page again, to set the font size back to 13 px.

The code goes on a bookmarklet, to toggle turn on and off the Youtube's new experiment.

You can read about this small experiment here.

Eshwar
  • 104
  • 10

1 Answers1

1

title is a class not an id, use getElementsByClassName to get an array of all elements with the class

var spans = document.getElementsByClassName('title');
if(spans.length){
    var spanStyle = spans[0].style;
    if(spanStyle.fontSize=='13')
    ...
    else
    ...
}
omma2289
  • 54,161
  • 8
  • 64
  • 68