校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 跨域修改iframe頁面內容詳解

跨域修改iframe頁面內容詳解

熱門標簽:ok電銷機器人 黃石ai電銷機器人呼叫中心 惡搞電話機器人 欣鼎電銷機器人 效果 智能電銷機器人被禁用了么 地圖標注軟件打印出來 如何查看地圖標注 高德地圖標注商戶怎么標 電話機器人技術

原理

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

前提條件

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

使用

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

postMessage

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

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

該方法需要將傳遞的內容用 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: 當前站點的域名, 使用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

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

標簽:聊城 綏化 金昌 中山 盤錦 阿壩 赤峰 萍鄉

巨人網絡通訊聲明:本文標題《跨域修改iframe頁面內容詳解》,本文關鍵詞  跨域,修改,iframe,頁面,內容,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《跨域修改iframe頁面內容詳解》相關的同類信息!
  • 本頁收集關于跨域修改iframe頁面內容詳解的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 开封县| 罗平县| 长治县| 霸州市| 阳东县| 应用必备| 瑞安市| 通渭县| 岳阳县| 石嘴山市| 渭南市| 隆安县| 天柱县| 东乌珠穆沁旗| 洛宁县| 阳信县| 惠东县| 永和县| 中阳县| 孝义市| 新蔡县| 蓝山县| 呼图壁县| 台湾省| 潞城市| 尼勒克县| 德钦县| 镇雄县| 洛扎县| 昂仁县| 宝鸡市| 淳化县| 泰来县| 治多县| 贺州市| 丰原市| 赤壁市| 唐海县| 连云港市| 惠水县| 吐鲁番市|