Skip to content

Instantly share code, notes, and snippets.

@ar5had
Created July 13, 2017 00:45
Show Gist options
  • Save ar5had/5a90e3aa77b1dee7684ed5a044ce3bd1 to your computer and use it in GitHub Desktop.
Save ar5had/5a90e3aa77b1dee7684ed5a044ce3bd1 to your computer and use it in GitHub Desktop.

Revisions

  1. ar5had created this gist Jul 13, 2017.
    31 changes: 31 additions & 0 deletions sendState.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    const { Provider } = ReactRedux;

    window.addEventListener("message", e => {
    // extentions and other 3rd party scripts talk via postMeessage api(same orgin)
    // so it is very important to filter those events
    if (e.origin !== window.location.origin || !e.data || e.data.source !== "dialog-message") {
    return;
    }

    // parent and window are same thing if the current page is not in any frame
    if (window !== parent) {
    parent.postMessage(
    { messageRecieved: true, source: "dialog-message" },
    `${window.location.origin}/iframe-testing-ground`
    );
    }

    const state = Object.assign({}, e.data);
    window.redux_store.dispatch(window.redux_actions.loadInitialState(state));
    });

    window.sendState = () => {
    window.postMessage(window.redux_store.getState(), `${window.location.origin}`);
    };

    ReactDOM.render(
    <Provider store={window.redux_store}>
    <PrintDialog />
    </Provider>,
    document.getElementById("root")
    );