简体   繁体   中英

Access cross-domain iframe elements like in Optimizely

I'd like to be able to manually select text from cross-domain iframe with javascript on the frontend.

I know it's pretty difficult to achieve such result as the use of postMessage is required if I decided to use a 100% javascript solution. I'm opened to backend solutions (either php or nodejs)

Optimizely can access any dom element from the page, I'd like to understand how they are doing it. Is it because the user needs to install a widget on his website first? If yes, how does it works exactly?

Here's an example: 例

Reproduced from #2 of this answer :

Let's say you want to create an experiment on http://www.mypage.com ... The Optimizely Editor loads http://www.mypage.com inside an iframe and uses window.postMessage to communicate with the page. This only works if that page already has a snippet like [ <script src="//cdn.optimizely.com/js/XXXXXX.js"></script> ] on it. If that's not the case, the editor will timeout while waiting for a message from the iframe'd page, and will load it again via a proxy that actually inserts the snippet onto the page. This loading process allows the editor to work with pages that a. contain an account snippet b. do not contain an account snippet, or c. sit behind a firewall (c. requires the snippet.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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