admin管理员组文章数量:1332889
I need a list/collection/array/whatever of all links on a website. Currently I'm using window.content.document.links, but that doesn't work an all websites. (Those websites produce an empty array.) (Example: dctp.ws) I'm guessing that's because those sites contain frames. Is there any way to access the links inside the frames?
Also, this is a FireGestures script, so it'll run "inside the browser". I don't want to download the website or something like that, since the browser already downloaded and parsed it.
I need a list/collection/array/whatever of all links on a website. Currently I'm using window.content.document.links, but that doesn't work an all websites. (Those websites produce an empty array.) (Example: dctp.ws) I'm guessing that's because those sites contain frames. Is there any way to access the links inside the frames?
Also, this is a FireGestures script, so it'll run "inside the browser". I don't want to download the website or something like that, since the browser already downloaded and parsed it.
Share Improve this question asked Feb 21, 2012 at 9:22 Aran-FeyAran-Fey 43.4k13 gold badges110 silver badges158 bronze badges 1- do you want links from a third party website or your own site?? – Sunil Kumar B M Commented Feb 21, 2012 at 9:25
2 Answers
Reset to default 5You can get a NodeList
of all a
elements from a document using getElementsByTagName
, like this:
var list = document.getElementsByTagName("a");
So you'd do that for the main document, and for all frames in the document. To access the frames, you can use the window.frames
pseudo-array. Each entry is the window
object of that frame, so:
var listInFrame = window.frames[n].document.getElementsByTagName("a");
So create a blank array, add in the elements from the document itself, then loop through the windows adding the links from their documents.
I'm not familiar with FireGestures, so I don't know if the Same Origin Policy applies to the scripts it runs.
Update: From your ment below, it sounds like FireGesture scripts are subject to the SOP. So you won't be able to directly access the content of documents from different origins in a FireGestures script.
You might be able to do something bining FireGestures and GreaseMonkey. GreaseMonkey has an API call, GM_xmlhttpRequest
, that bypasses the SOP — but note that it would be another GET
, you wouldn't be reading the copy of the page that's already in-memory, which you said you wanted to do. Unfortunately, it's entirely possible that you may not be able to do what you want with FireGestures. You may have to write your own add-on entirely (and have it request relevant permissions).
You can use document.getElementsByTagName('a')
.
This does exactly what it sounds like--you get a NodeList of all the a
elements on the page.
本文标签: How to get an array of all links on a website in JavaScriptStack Overflow
版权声明:本文标题:How to get an array of all links on a website in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742309767a2450632.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论