我希望能够在前端使用 javascript 从跨域 iframe 中手动选择文本。
我知道要达到这样的结果是相当困难的,因为如果我决定使用 100% javascript 解决方案,则需要使用 postMessage。我对后端解决方案持开放态度(php 或 nodejs)
Optimizely 可以从页面访问任何 dom 元素,我想了解他们是如何做到的。是因为用户需要先在他的网站上安装一个小部件吗?如果是,它到底是如何工作的?
这是一个例子:
最佳答案
转载自#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.)
关于javascript - 像 Optimizely 一样访问跨域 iframe 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55625620/