How to fool Iframe that it is top most window

2019-03-04 11:16发布

问题:

I wonder, is there a way to override the iframe's window.top and window.parent such that from it's context the following is true

window === window.parent && window === window.top

The case is that the app that I want to embed in an iframe has many references to window.top, it stores objects and uses them to communicate back and forth with its own child iframes.

<iframe sandbox=""></iframe>

Without "allow-scripts" is not an option for me as any access to window.top throws and Error, while I want things to work pretty much as if the iframe was a top level window

Note: I saw a similar question asked How to Trick/Fool an Iframe to thinking its the TOP element in DOM but there while the same requirement, the actual problem - changing the top.location could be worked-around, this is why I guess the discussion was discontinued.

回答1:

The simple answer is no you can not change a read-only property.

The more complex answer is that you could parse the other site through a proxy server and dynamically rewrite the JS. However I strongly expect that is more effort than it is worth and will likely break copyright laws at some point.

The question you should be asking is does the third-party want to be in your iFrame? If yes get them to fix their code, if no then you shouldn't be doing this.