I have a simple content script for my Chrome Extension that is supposed to grab the titles of YouTube videos. My problem is that it has only worked once. I tried accessing the child nodes of the HTMLCollection which should only be one but I get null of undefined. Doing something like:
element[0].innerText;
doesn't give me anything useful but from my understanding if I use getElementsByClassName and apply innerText on the first element with [0] it shoud work. It might be an issue with the html not being fully loaded as sometimes I get null but HTMLCollection always has the property that I want to access just sitting there.
Also:
element.length
returns 0.
This is what I usually get with my script.
Inside is the "innerText" property that I want to grab.
And this is what I got the one time it worked.
{
"manifest_version": 2,
"name": "test",
"author": "Muhammad Amer",
"description": "test",
"version": "1.0",
"content_scripts": [
{
"matches": [
"https://www.youtube.com/*"
],
"js": ["jquery-3.3.1.js", "content.js"]
}
],
"permissions": [
"https://www.youtube.com/*",
"tabs",
"activeTab",
"webNavigation"
]
}
var element = document.getElementsByClassName("title style-scope ytd-video-
primary-info-renderer");
console.log(element);
for (var i = 0; i < element.length; i++) {
var songTitle = element[i].innerText;
console.log(songTitle);
}