为什么我无法访问 NYT 线上填字游戏页面上的 HTML 集合中的元素?

huangapple go评论64阅读模式
英文:

Why can't I access elements in an HTML collection on the NYT crossword page?

问题

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?

英文:

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

得分: 1

以下是翻译好的部分:

潜在原因:

您正在查看分配的返回值。

在导航到https://www.nytimes.com/crosswords/game/mini/并在我的浏览器开发者控制台中执行var requiredElement = document.getElementsByClassName('xwd__toolbar--tools')[0];后,确实会得到undefined

为什么我无法访问 NYT 线上填字游戏页面上的 HTML 集合中的元素?

尽管显示undefined,但requiredElement内确实存储有一个值,如果我告诉它评估其值,它将在开发者控制台中显示:

为什么我无法访问 NYT 线上填字游戏页面上的 HTML 集合中的元素?

这里有一个有更多详细信息的有趣答案:https://stackoverflow.com/a/22844864

英文:

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:

为什么我无法访问 NYT 线上填字游戏页面上的 HTML 集合中的元素?

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:

为什么我无法访问 NYT 线上填字游戏页面上的 HTML 集合中的元素?

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

huangapple
  • 本文由 发表于 2023年4月19日 14:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定