Get all href links in the DOM

I need to write code that puts all href links from a web page into an array. Here is what I still have:

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, such as a Gmail inbox, because some of the links are inside an iframe. How can I get ALL links, including inside iframes?

It is also an extension for Chrome. In the manifest, I have all_frames set to true - does it matter?

thanks

+6
source share
4 answers

I have a method that I use to access data in an IFrame. How fun it is that the answer is never recorded or used: 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; } 

Then I use it to search for any Im element after:

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

You can always change the method to iterate over all iFrames, etc. blah blah, but that should be enough to make you move. Rate me! Im new

+5
source

Surely you're going to get "arr not defined" with code to start 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); } 
+7
source

One thing to remember is that

  • document.links
  • document.images
  • document.forms
  • document.forms [0] .elements
  • document.getElementsByName ()
  • document.getElementsByClassName ()
  • document.getElementsByTagName ()

are live queries for DOM objects, so in forLoops this can slow things down significantly (since I <links.length are queries for each loop) if you check the length of the array as follows:

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

instead, you'd 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); } 
+7
source

From my web adapter, the 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 to all_frames_docs , and it will be called alternately for each frame and iframe in the current window, if your script has access to such (i.e. an extension or bookmarklet). So, all you have to do is code to process each document, which can go through document.getElementsByTagName("a") or something else, and make this function the parameter of your call to all_frames_docs .

0
source

Source: https://habr.com/ru/post/906374/


All Articles