Accessing an iframe's contents in Adobe Air

2019-04-13 08:05发布

I have been having a lot of trouble figuring out how I can access the content within an iframe in Air. Here is some example jquery code that I have been testing with.


$(document).ready(function(){
    $("#frame").ready(function(){
        air.trace($("#frame").contents().find("body").html());
        air.trace(window.frames["frame"].innerHTML);
        air.trace(document.getElementById("frame").innerHTML);
    });
});

The iframe I am using is.

<iframe src="http://google.com" id="frame" width="100%" sandboxRoot="http://google.com/" documentRoot="/" name="frame" height="600"></iframe>

The output from the above code is


null
undefined


As you can see the contents are always either null, undefined or an empty string. Am I missing something that is preventing me from accessing the content of the iframe? Any suggestions would be greatly appreciated.

3条回答
贼婆χ
2楼-- · 2019-04-13 08:46

The solution was here http://help.adobe.com/en_US/AIR/1.1/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7f08.html

Here is an example of how I got it working.

<iframe src="http://google.com/local/iframe.html" id="frame" width="100%" sandboxRoot="http://google.com/local/" documentRoot="app:/" name="frame" height="600"></iframe>

Anything requested from the sandboxRoot(e.g. http://google.com/local) will be used from the document root and will be treated as if it is from the same domain(google.com).

In iframe.html I can access the iframe to google just as if it were from the same domain.

I knew there had to be a way to do this :)

查看更多
成全新的幸福
3楼-- · 2019-04-13 08:54

Adobe AIR is just a browser (WebKit actually), and thus follows the rules browsers follow. The rule which governs your issue here is referred to as the "same-origin-policy".

If it's not on the same domain, the browser will not allow you to access the data or content from that site, so you will get this result. Use an Json based AJAX API if you would like to get data from another domain.

Example of someone else having this issue:

查看更多
forever°为你锁心
4楼-- · 2019-04-13 08:57

As many others, I also struggled to figure this out.

The solution is given above by tomfmason, but even then it took me ages to actually understand what was going on.

To help anyone else who might still be trying to use AIR html+js to create a web browser or pull data from a remote web page, I've created a very basic AIR 3.2 project (1.7 MB .zip file) that can be found at:

http://adobe-air-js.blogspot.com/2011/12/air-reading-dom-of-remote-web-page.html

AIR is really quite slick actually, once one gets past the initial complexities. I find it an order of magnatude more reliable/usable than Titanium.

Good luck! ~Chris

查看更多
登录 后发表回答