0

I am working on creating a chrome extension for an ESPN fantasy league page.

Essentially, wherever there is a link of a players name, I want it to also link to a new tab of a different site. That is mostly irrelevant, as what I am having issue with is gathering the links into an array in the first place.

Using javascript:

window.addEventListener('load', function () {

    console.log("Test 1");

    var links = document.getElementsByClassName('link clr-link pointer');
    console.log(links.length);

    for (elt of links){
        console.log(links.text);
    }
    console.log("Test 2");

});

The page is a league page so its going to be private, but here is a screenshot of the DOM: enter image description here

So I am looking for all links with the class attribute of "link clr-link pointer"

In my console, the code is running after the DOM loads but is returning an array of size 0, despite every player link being of that same class: enter image description here

Any idea why it isn't grabbing any of the links? Thanks in advance for any help.

  • 3
    Assuming the element is not inside a ShadowDOM, that site is probably creating the contents dynamically **after** the page has loaded. See [this answer](/a/39508954) for common solutions. – wOxxOm Oct 25 '19 at 20:57
  • 1
    Just to be clear, `getElementsByClassName` returns an `HTMLCollection`. Also, is it possible these links are generated after page load? if you copy paste your code into the console, it might get your links. – Artur Oct 25 '19 at 20:57
  • So you both, @wOxxOm and Artur seem to correct about it loading in after the page has loaded. I tried pasting my code in the console and it comes up with the HTMLCollection size (27), but in my for loop to print out the anchors text contents its just says 27 again, not the text contained within. Could this be due to the dynamic loading? But the text is already there when I paste this code. – Ethan Guillotte Oct 25 '19 at 21:11
  • It wasn't displaying because I was using .text, not .textContent. Now I just need to figure out how to make it way until after the content is generated, loooking into @wOxxOm link now. – Ethan Guillotte Oct 25 '19 at 21:20

0 Answers0