Get all href links in DOM

2020-02-23 08:45发布

I need to write code that puts all of the href links from a webpage into an array. Here's what I have so far:

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
  array.push(links[i].href);
}

However, this does not work on a page like Gmail's inbox, because the some of the links are within an iframe. How can I get ALL of the links, including the ones inside the iframe?

Also, this is for a google chrome extension. In the manifest, I have all_frames set to true - does this make a difference?

Thanks

4条回答
▲ chillily
2楼-- · 2020-02-23 08:49

From my Web Adjuster's bookmarklet code,

function all_frames_docs(c) {
    var f=function(w) {
        if(w.frames && w.frames.length) {
            var i; for(i=0; i<w.frames.length; i++) f(w.frames[i])
        } c(w.document) };
    f(window) }

You can pass any function into all_frames_docs and it will be called in turn on every frame and iframe in the current window, provided your script has access to such (i.e. it's an extension or a bookmarklet). So all you have to do now is code the function to handle each document, which can go through document.getElementsByTagName("a") or whatever, and make this function the parameter of your call to all_frames_docs.

查看更多
Viruses.
3楼-- · 2020-02-23 09:02

I have a method I use to access data in an IFrame. How fun that the answer is never just written down to read and use :P. Feel free to modify and abuse:

public HtmlElementCollection GetIFrameElements(String tmpTag, int Frame)
    {
        HtmlElementCollection tmpCollection = mWebBrowser.Document.Window.Frames[Frame].Document.Body.GetElementsByTagName(tmpTag);
        return tmpCollection;
    }

I then use it to look for whatever element Im after:

foreach (HtmlElement el in GetElements("input"))
        {
            if (el.GetAttribute("id").Equals("hasNoGoogleAccount"))
            {
                el.InvokeMember("click");
            }
        }

You could always change the method to loop through and get all iFrames etc blah blah but that should be enough to get you moving. Rate me! Im new

查看更多
再贱就再见
4楼-- · 2020-02-23 09:05

Surely you're going to get 'arr is not defined' with your code to begin with?

var array = [];
var links = document.links;
for(var i=0; i<links.length; i++) {
    arr.push(links[i].href);
}

Try:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}
查看更多
Anthone
5楼-- · 2020-02-23 09:09

One thing to remember that

  1. document.links
  2. document.images
  3. document.forms
  4. document.forms[0].elements
  5. document.getElementsByName()
  6. document.getElementsByClassName()
  7. document.getElementsByTagName()

are live queries to DOM objects, therefore in forLoops it may significantly slow down your execution (as i < links.length is queries on each for cycle), if you check the array length like this:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
    array.push(links[i].href);
}

instead you better do this:

var array = [];
var links = document.getElementsByTagName("a");
for(var i=0, max=links.length; i<max; i++) {
    array.push(links[i].href);
}
查看更多
登录 后发表回答