3

I'm using the following code to get all links in a webpage, even in its iframes:

function GetTheLinks()
{
    var linksArray = [];
    var links = document.getElementsByTagName("a");
    var iLinksCount = links.length;
    for (var i = 0; i < iLinksCount; i++)
    {
        var link = links[i];
        linksArray.push(link.innerHTML);
        linksArray.push(link.innerText);
        linksArray.push(link.href);
    }
    var frames = document.getElementsByTagName("iframe");
    var iFramesCount = frames.length;
    for (var f = 0; f < iFramesCount; f++)
    {
        var doc = frames[f].contentDocument;
        if (doc)
        {
             links = doc.getElementsByTagName("a");
             alert(links.length + " links in frame " + f);
             var iLinksCount = links.length;
             for (var i = 0; i < iLinksCount; i++)
             {
                 var link = links[i];
                 linksArray.push(link.innerHTML);
                 linksArray.push(link.innerText);
                 linksArray.push(link.href);
             }
         }
         else
         {
             alert("Error!");
         }
    }
    return linksArray;
}

I am confronted with an URL where my code does not catch a certain link. The URL is this one.

The link that I want to catch is this one:

enter image description here

Using Chrome's dev tools, I can see that it is contained in a frame.

What am I doing wrong in my code so that it doesn't catch this link?

Thank you!

There is its code:

<a href="https://premium.zeit.de/bestellung/1932788?angebot=https%3A%2F%2Fpremium.zeit.de%2Fbestellung%2F1932788&amp;entry_service=pur&amp;url=https%3A%2F%2Fmeine.zeit.de%2Fbestellung_abschliessen%3Furl%3Dhttps%253A%252F%252Fwww.zeit.de%252Findex" class="option__button option__button--pur js-forward-link-purabo" data-encode-level="1" data-ct-label="purabo_button">
                                    <span class="visually-hidden">zeit.de Pur: </span>Jetzt abonnieren
                                </a>
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 1
    *`frames[f].getElementsByTagName("a")`* gets the children elements of that frame element, but it won't have any. If you want the links inside the document that the iframe embeds into the page, you'll need to use [`frames[f].contentDocument.getElementsByTagName("a")`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement/contentDocument), but notice that it might not be accessible – Bergi Jun 25 '22 at 04:13
  • @Bergi Sorry, I missed that, but in my code, it was present. I have edited my post. It still doesn't work. – tmighty Jun 25 '22 at 04:17
  • @Bergi What do you mean by "not accessible"? – tmighty Jun 25 '22 at 04:18
  • You can’t read the content of frames that aren’t on the same origin (i.e. that don’t belong to you). – Ry- Jun 25 '22 at 05:43
  • @Ry- Can you tell me what you mean exactely, please? – tmighty Jun 25 '22 at 06:23
  • https://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame – RoToRa Jun 25 '22 at 08:05
  • @RoToRa But as far as I can see, the iframe is coming from the same domain (zeit.de), isn't it? – tmighty Jun 25 '22 at 17:36
  • @Ry Also, I am not getting any except. It's just that frames[f].getElementsByTagName("a").length = 0. – tmighty Jun 25 '22 at 17:39

0 Answers0