經過postMessage進行跨域通訊

最近工做中遇到一個需求,場景是:h5頁做爲預覽模塊內嵌在pc頁中,用戶在pc頁中可以作一些操做,而後h5作出響應式變化,達到預覽的效果。api

這裏首先想到就是把h5頁面用iframe內嵌到pc網頁中,而後pc經過postMessage方法,把變化的數據發送給iframe,iframe內嵌的h5經過addEventListener接收數據,再對數據作響應式的變化。併發

這裏總結一下postMessage的使用,api很簡單:dom

otherWindow.postMessage(message, targetOrigin, [transfer]);xss

otherWindow是目標窗口的引用,在當前場景下就是iframe.contentWindowpost

message是發送的消息,在Gecko 6.0以前,消息必須是字符串,而以後的版本能夠作到直接發送對象而無需本身進行序列化;this

targetOrigin表示設定目標窗口的origin,其值能夠是字符串"*"(表示無限制)或者一個URI。在發送消息的時候,若是目標窗口的協議、主機地址或端口這三者的任意一項不匹配targetOrigin提供的值,那麼消息就不會被髮送;只有三者徹底匹配,消息纔會被髮送。對於保密性的數據,設置目標窗口origin很是重要;code

可選參數transfer 是一串和message 同時傳遞的 Transferable 對象. 這些對象的全部權將被轉移給消息的接收方,而發送一方將再也不保有全部權。對象

那麼,當iframe初始化後,能夠經過下面代碼獲取到iframe的引用併發送消息:rem

// 注意這裏不是要獲取iframe的dom引用,而是iframe window的引用
const iframe = document.getElementById('myIFrame').contentWindow;
iframe.postMessage('hello world', 'http://yourhost.com');

在iframe中,經過下面代碼便可接收到消息。字符串

window.addEventListener('message', msgHandler, false);

在接收時,能夠根據須要,對消息來源origin作一下過濾,避免接收到非法域名的消息致使的xss攻擊。

最後,爲了代碼複用,把消息發送和接收封裝成一個類,同時模擬了消息類型的api,使用起來很是方便。具體代碼以下:

export default class Messager {
    constructor(win, targetOrigin) {
        this.win = win;
        this.targetOrigin = targetOrigin;
        this.actions = {};
        window.addEventListener('message', this.handleMessageListener, false);
    }

    handleMessageListener = event => {
        if (!event.data || !event.data.type) {
            return;
        }
        const type = event.data.type;
        if (!this.actions[type]) {
            return console.warn(`${type}: missing listener`);
        }
        this.actions[type](event.data.value);
    }

    on = (type, cb) => {
        this.actions[type] = cb;
        return this;
    }

    emit = (type, value) => {
        this.win.postMessage({
            type, value
        }, this.targetOrigin);
        return this;
    }

    destroy() {
        window.removeEventListener('message', this.handleMessageListener);
    }
}
相關文章
相關標籤/搜索