国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > HTML5 > 正文

跨域修改iframe頁面內容詳解

2024-09-05 07:23:06
字體:
來源:轉載
供稿:網友

原理

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

前提條件

需要將proxy.html放到與內嵌的iframe頁同域的服務下, 并且可以被訪問到.

使用

支持2種調用方式: 使用 postMessage 和 URL params.

postMessage

該方法需要使用 JSON.stringify 將對象轉為字符串.

// 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

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

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

資源

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霍州市| 密云县| 荣昌县| 兰州市| 道孚县| 衡南县| 兴仁县| 吴江市| 阿图什市| 安吉县| 安图县| 十堰市| 榆林市| 新绛县| 越西县| 平阳县| 徐水县| 察隅县| 儋州市| 东方市| 临泽县| 吉安市| 德阳市| 赣榆县| 汾西县| 双江| 阿城市| 青浦区| 江都市| 漠河县| 突泉县| 定州市| 凤山县| 光山县| 福鼎市| 杂多县| 盘山县| 大同县| 根河市| 巴彦淖尔市| 武平县|