0

I'm trying to write a Chrome extension that adds "previous puzzle" and "next puzzle" buttons to the toolbar on a New York Times Crossword page. I hate having to navigate back to the crossword archive to find another puzzle and can't believe they don't include this in the toolbar.

My plan is to inject two additional li tags with buttons on the toolbar, to sit next to the existing Rebus and Reset buttons. I have all the HTML figured out that I want to inject, but I can't seem to get the ul bound to a javascript variable for me to edit the innerHTML.

I thought this would be a simple 10-15 line javascript, 30 minute project. Whoops.

For example, if you navigate to:

https://www.nytimes.com/crosswords/game/daily/

or if you don't have a Times account:

https://www.nytimes.com/crosswords/game/mini/

I can retrieve an HTMLCollection of the toolbar by writing:

var toolbar = document.getElementsByClassName('xwd__toolbar--tools'); //returns an HTMLCollection with the ul as the 0th element.

But if I try to actually access the ul in the 0th element, it's returning undefined and I've been beating my head against a wall trying to figure out why.

var requiredElement = document.getElementsByClassName('xwd__toolbar--tools')[0]; //returns undefined

What undoubtedly obvious javascript thing am I missing here?

  • 1
    Is there any other site you can reproduce this with? nytimes unfortunately requires me to login... – tobifasc Apr 19 '23 at 06:48
  • https://www.nytimes.com/crosswords/game/mini you can choose to proceed without an account and this page has the same toolbar on it – GoldyCantCode Apr 19 '23 at 22:29

1 Answers1

1

Probable cause:

You are looking at the return value of the assignment.

After navigating to https://www.nytimes.com/crosswords/game/mini/ and executing var requiredElement = document.getElementsByClassName('xwd__toolbar--tools')[0]; in the developer console of my browser sure enough i get undefined:

Screenshot of developer console showing undefined return of variable assignment

Even though this says undefined there is a value stored inside requiredElement which will be displayed in the developer console if i tell it to evaluate its value:

Screenshot of developer console showing evaluation of variable

Here is an interesting answer with some more details: https://stackoverflow.com/a/22844864

tobifasc
  • 681
  • 5
  • 17