从 iframe 进行数据挖掘
我有兴趣开发一些作为浏览器书签驻留的 javascript,可以从嵌套 iframe 中挖掘数据,并通过 AJAX 将数据返回到远程服务器。此类脚本的一小部分将用于协调流行在线游戏的玩家统计数据和攻击策略。
我是一个新手 javascript 程序员,不知道这是否可行。到目前为止,我的尝试都失败了,尽管我已经编写了在其他应用程序中运行良好的其他 JavaScript 代码。
是否有可能违反同源政策?有问题。应该工作的代码给我什么也没有。
像这样的代码:
javascript:
y=document.getElementsByTagName('iframe')[2];
alert(y.src);
给了我预期的结果,但是当我尝试访问我认为应该是 iframe 的 innerHTML
的内容时,没有结果。
有什么想法吗?
I am interested in developing some javascript that resides as browser bookmark that can mine data from nested iframes and AJAX the data back to a remote server. A small collection such scripts would be used to coordinate player stats and attack strategy for a popular online game.
I am a novice javascript programmer and don't know if this is possible. My attempts thus far have failed, although I have authored other javascript code that works great in other applications.
Is it possible that I am violating the same-origin policy? Something is wrong. Code that should work gives me nothing.
Code such as this:
javascript:
y=document.getElementsByTagName('iframe')[2];
alert(y.src);
gives me the result one would expect, but when I try to access what I think should be the innerHTML
of the iframe, there is no result.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能违反了同源策略。
回想一下,您的 scriptlet 将在当前加载页面的域中执行 javascript。它还在对该页面施加相同的限制的情况下运行,这意味着如果
IFRAME
包含不同域中的页面,则不允许它访问该内容。您可以访问
src
属性,因为该属性存在于托管IFRAME
的页面中的标记上,但您将无法访问其他内容,包括 <代码>innerHTML。也许您可以将该文档(托管在
iframe
中的文档)加载到浏览器中,然后针对该文档运行您的 scriptlet?It is very likely that you are violating the same-origin policy.
Recall that your scriptlet will be executing javascript in the domain of the currently loaded page. It also runs with the same restrictions imposed on that page, meaning that it is not allowed to access the content of an
IFRAME
if it contains a page in a different domain.You are able to access the
src
property, because that exists on the tag in the page hosting theIFRAME
but you won't be able to access much else, including theinnerHTML
.Perhaps you could load that document (the one hosted in the
iframe
) into the browser, and then run your scriptlet against that?