Reading document.links from an IFrame

2019-06-12 11:38发布

EDIT: Just a quick mention as to the nature of this program. The purpose of this program is for web inventory. Drawing different links and other content into a type of hierarchy. What I'm having trouble with is pulling a list of links from a webpage within an IFrame.


I get the feeling this one is gonna bite me hard. (other posts indicate relevance to xss and domain controls)

I'm just trying something with javascript and Iframes. Basically I have a panel with an IFrame inside that goes to whatever website you want it to. I'm trying to generate a list of links from the webpage within the Iframe. Its strictly read only.

Yet I keep coming up against the permission denied problem.

I understand this is there to stop cross site scripting attacks and the resolution seems to be to set the document domain to the host site. JavaScript permission denied. How to allow cross domain scripting between trusted domains?

However I dont think this will work if I'm trying to go from site to site.

Heres the code I have so far, pretty simple:

function getFrameLinks()
{
/* You can all ignore this. This is here because there is a frame within a frame. It should have no effect ont he program. Just start reading from 'contentFrameElement'*/


//ignore this
var functionFrameElem = document.getElementById("function-IFrame");
console.log("element by id parent frame ");
console.log(functionFrameElem);
var functionFrameData = functionFrameElem.contentDocument;
console.log("Element data");
console.log(functionFrameData);

//get the content and turn it into a doc
var contentFrameElem = functionFrameData.getElementById("content-Frame")
console.log(contentFrameElem);
var contentFrameData = contentFrameElem.contentDocument;
console.log(contentFrameData);

//get the links
//var contentFrameLinks = contentFrameData.links;
var contentFrameLinks = contentFrameData.getElementsByTagName('a');

Goal: OK so due to this being illegal and very similar to XSS. Perhaps someone could point out a solution as to how to locally store the document. I dont seem to have any problems accessing document.links with internal pages in the frame.

Possibly some sort of temp database of cache. The simpler the solution the better.

3条回答
smile是对你的礼貌
2楼-- · 2019-06-12 12:05

You might be interested in using the YQL (Yahoo Query Language) to retrieve filtered results from remote urls..

example of retrieving all the links from the yahoo.com domain

查看更多
唯我独甜
3楼-- · 2019-06-12 12:10

If you want to read it just for your self and in your browser, you can write a simple proxy with php in your server. the most simple code:

<?php /* proxy.php */ readfile($_GET['url']); ?>

now set your iframe src to your proxy file:

<iframe src="http://localhost/proxy.php?url=http://www.google.com"
id="function-IFrame"></iframe>

now you can access the iframe content from your (local) server. if you want set the url with a program remember to encode the url (urlencode in php or encodeURIComponent in js)

查看更多
beautiful°
4楼-- · 2019-06-12 12:10

Here is a bookmarklet you can run on any page (assuming the links are not in an iframe)

javascript:var x=function(){var lnks=document.links,list=[];for (var i=0,n=lnks.length;i<n;i++) {var href = lnks[i].href;  list.push(href)};if (list.length>0) { var w=window.open('','_blank');w.document.write(list.length+' links found<br/><ul><li>'+list.sort().join('</li><li>')+'</ul>');w.document.close()}};void(x());

the other way is for you (on Windows) to save your HTML with extension .HTA Then you can grab whatever lives in the iFrame

查看更多
登录 后发表回答