麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > HTML5 > 正文

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

2024-09-05 07:23:06
字體:
供稿:網(wǎng)友

原理

主站點內(nèi)嵌代理頁面, 并向代理頁傳遞數(shù)據(jù), 代理頁根據(jù)主站點的數(shù)據(jù)對目標頁的DOM進行操作.由于代理頁與目標頁同域, 所以代理頁可以獲取并操作目標頁的document對象.

前提條件

需要將proxy.html放到與內(nèi)嵌的iframe頁同域的服務(wù)下, 并且可以被訪問到.

使用

支持2種調(diào)用方式: 使用 postMessage 和 URL params.

postMessage

該方法需要使用 JSON.stringify 將對象轉(zhuǎn)為字符串.

// Reactfunction 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 編碼.

// Reactfunction 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)前站點的域名, 使用postMessage方式時必填, proxy用來校驗發(fā)出消息的源域名.    iframe: 需要內(nèi)嵌的iframe標簽字符串,    includeStyle: 希望添加到iframe頁的css內(nèi)容,    includeScript: 希望添加到iframe頁的js內(nèi)容,    importStyle: 希望引入到iframe頁的css資源鏈接, 如果目標站點使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會被瀏覽器禁止.    importScript: 希望引入到iframe頁的js資源鏈接, 如果目標站點使用安全協(xié)議(https), 資源鏈接使用非安全協(xié)議(http), 該功能會被瀏覽器禁止.}

注意: 處于安全問題, 默認禁用了 includeScript 和 importScript 功能, 如需啟用在proxy.html中將變量 ENABLED_JS_INCLUDE 設(shè)置為 true 即可.

資源

https://github.com/stephenliu1944/cross-domain-iframe-proxy

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 超碰97在线人人 | 欧美高清第一页 | 久久综合狠狠综合久久 | av成人免费在线观看 | 国产一级毛片高清 | h视频在线观看免费 | 黄色片网站免费观看 | 高清国产午夜精品久久久久久 | 久久色网站| 欧美成人一区二区三区电影 | a黄色网 | 日本成年免费网站 | 久久久www成人免费精品 | 18pao国产成人免费视频 | 国产精品久久久久久久hd | av成人免费在线观看 | 精品在线观看一区二区 | www.成人免费视频 | 精品一区在线视频 | 国产一级片| 九一免费版在线观看 | 美女黄页网站免费进入 | 日韩黄a | 黄色网战入口 | 日韩精品中文字幕一区二区 | 九一国产精品 | 国产xxxx岁13xxxxhd| 久久密| 羞羞网站在线观看入口免费 | 国产 一区 精品 | 双性帝王调教跪撅打屁股 | 一级黄片毛片免费看 | 一级免费毛片 | 国产一区二区精品免费 | 99re热精品视频 | 欧美77 | 国产精品a一 | 天天鲁在线视频免费观看 | 欧美一级特黄aaaaaaa什 | 福利在线免费 | 免费看日韩片 |