简体   繁体   English

用于获取跨域URL的iFrame替代方案

[英]iFrame alternative for getting cross-domain URL

I'm creating a web-based game. 我正在创建一个基于网络的游戏。 As a part of it, the player must go to the home page of a website, and follow a series of link until he reaches to the "destination page" in the minor time possible. 作为其中的一部分,玩家必须访问网站的主页,并按照一系列链接,直到他在可能的次要时间内到达“目的地页面”。 Think of it as a sort of link treasure hunt. 把它想象成一种链接寻宝。

In order to control the game, I created a page with a javascript timer on the top, and an iFrame showing the website to surf (which is not mine and I have no control over it). 为了控制游戏,我在顶部创建了一个带有javascript计时器的页面,以及一个显示网站冲浪的iFrame(这不是我的,我无法控制它)。

However this has a huge fault: it is not possible to detect the current cross-domain URL of the iFrame, so my page cannot know when the user has arrived to the destination page and stop the game. 然而,这有一个巨大的错误:无法检测到iFrame的当前跨域URL,因此我的页面无法知道用户何时到达目标页面并停止游戏。

Is there any alternative way I could achieve this? 有没有其他方法可以实现这一目标?

Is there any alternative way I could achieve this? 有没有其他方法可以实现这一目标?

No, there is not. 不,那里没有。

The Same Origin Policy completely prevents you from accessing properties of any window object that displays content from another domain. 同源策略完全阻止您访问显示来自其他域的内容的任何窗口对象的属性。

(If one was a subdomain of the other, then document.domain would help. But from your problem description it doesn't sound like that was the case.) (如果一个是另一个的子域,那么document.domain会有所帮助。但是根据你的问题描述,听起来并非如此。)


And besides the technical issue: Generating traffic and clicks (that will skew their statistics/analytics) on a site that you don't own, just for the purpose of being able to present your own game, would be rather unfair (aka a “dick move”.) 除了技术问题:在您不拥有的网站上生成流量和点击(这将会扭曲他们的统计/分析),只是为了能够展示您自己的游戏,这将是相当不公平的(又名“迪克移动“。)

You can wait upto the contentWindow of the iFrame is resolved and you have a reference to the iFrame's window, after you changed the source pragmatically. 您可以等到iFrame的contentWindow被解析,并且在您实际更改源后,您有对iFrame窗口的引用。 once you have that, you can add a onload handler on iFrame's <body> and inject some postMessage call inside that handler, so once the body is loaded of the iFrame's window it wil call postMessage from the onload handler of body and it will notify your outer window, of-course you have to add message listener in outer window using window.addEventListener("message", callback) 一旦你有了,你可以在iFrame的<body>上添加一个onload处理程序,并在该处理程序中注入一些postMessage调用,所以一旦主体加载了iFrame的窗口,它将从body的onload处理程序调用postMessage,它将通知你外部窗口,当然你必须使用window.addEventListener("message", callback)在外部窗口中添加消息监听器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM