跨域修改iframe頁(yè)面內(nèi)容詳解
原理

主站點(diǎn)內(nèi)嵌代理頁(yè)面, 并向代理頁(yè)傳遞數(shù)據(jù), 代理頁(yè)根據(jù)主站點(diǎn)的數(shù)據(jù)對(duì)目標(biāo)頁(yè)的DOM進(jìn)行操作.由于代理頁(yè)與目標(biāo)頁(yè)同域, 所以代理頁(yè)可以獲取并操作目標(biāo)頁(yè)的document對(duì)象.
前提條件
需要將proxy.html放到與內(nèi)嵌的iframe頁(yè)同域的服務(wù)下, 并且可以被訪問(wèn)到.
使用
支持2種調(diào)用方式: 使用 postMessage 和 URL params.
postMessage
該方法需要使用 JSON.stringify 將對(duì)象轉(zhuǎn)為字符串.
// React
function IframeProxy(props) {
handleLoad = (e) => {
e.target.contentWindow.postMessage(JSON.stringify({
iframe: `<iframe name="target" title="target" className="target" src="http://www.targetdomain.com/target.html" frameBorder="0" scrolling="no" style="width: 100%;height:100%"></iframe>`,
includeStyle: `
body {
background-color: yellow;
}
header {
display: none;
}
footer {
display: none;
}
`,
includeScript: `
window.addEventListener('load', function() {
alert(document.querySelector('body').innerHTML);
});
`,
importStyle: `http://www.mydomain.com/assets/css/import.css`,
importScript: `http://www.mydomain.com/assets/js/import.js`
}), 'https://www.target.com');
}
return <iframe name="proxy" title="proxy" className="proxy" width="100%" height="100%" onLoad={handleLoad} src={`http://www.targetdomain.com/proxy.html?origin=${window.location.protocol}//${window.location.host}`} frameBorder="0" scrolling="no"></iframe>;
}
URL params
該方法需要將傳遞的內(nèi)容用 encodeURIComponent 編碼.
// React
function IframeProxy(props) {
var params = 'iframe=' + encodeURIComponent(`
<iframe name="target" title="target" className="target" src="http://www.targetdomain.com/target.html" frameBorder="0" scrolling="no" style="width: 100%;height:100%"></iframe>
`);
params += '&includeStyle=' + encodeURIComponent(`
body {
background-color: red;
}
header {
display: none;
}
footer {
display: none;
}
`);
params += '&includeScript=' + encodeURIComponent(`
window.addEventListener('load', function(event) {
alert(document.querySelector('body').innerHTML);
});
`);
params += '&importStyle=' + encodeURIComponent(`
http://www.mydomain.com/assets/css/import.css
`);
params += '&importScript=' + encodeURIComponent(`
http://www.mydomain.com/assets/js/import.js
`);
return <iframe name="proxy" title="proxy" className="proxy" width="100%" height="100%" src={`http://www.targetdomain.com/proxy.html?${params}`} frameBorder="0" scrolling="no"></iframe>;
}
API
<iframe src="http://www.targetdomain.com/proxy.html?params"></iframe>;
params: {
origin: 當(dāng)前站點(diǎn)的域名, 使用postMessage方式時(shí)必填, proxy用來(lái)校驗(yàn)發(fā)出消息的源域名.
iframe: 需要內(nèi)嵌的iframe標(biāo)簽字符串,
includeStyle: 希望添加到iframe頁(yè)的css內(nèi)容,
includeScript: 希望添加到iframe頁(yè)的js內(nèi)容,
importStyle: 希望引入到iframe頁(yè)的css資源鏈接, 如果目標(biāo)站點(diǎn)使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會(huì)被瀏覽器禁止.
importScript: 希望引入到iframe頁(yè)的js資源鏈接, 如果目標(biāo)站點(diǎn)使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會(huì)被瀏覽器禁止.
}
注意: 處于安全問(wèn)題, 默認(rèn)禁用了 includeScript 和 importScript 功能, 如需啟用在proxy.html中將變量 ENABLED_JS_INCLUDE 設(shè)置為 true 即可.
資源
https://github.com/stephenliu1944/cross-domain-iframe-proxy
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于iframe跨域使用postMessage的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于iframe跨域使用postMessage的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一2019-10-29HTML5 window/iframe跨域傳遞消息 API介紹
window.postMessage允許多個(gè) window/frame之間跨域傳遞數(shù)據(jù)和信息。下面為大家介紹下window.postMessage的工作原理,以及如何在FireFox,IE8+,Opera,Safari和Chrome中使用2013-08-26Iframe 高度自適應(yīng)(兼容IE/Firefox、同域/跨域)
在實(shí)際的項(xiàng)目進(jìn)行中,很多地方可能由于歷史原因不得不去使用iframe,包括目前正火熱的應(yīng)用開(kāi)發(fā)也是如此。2010-03-17

