From b5dcf8de4490fcc3c4df432e4bc4bae15d02faa1 Mon Sep 17 00:00:00 2001 From: Dariusz Szut Date: Fri, 27 Jan 2017 14:49:21 +0100 Subject: [PATCH] EZS-1330: Studio view mode listening on message event also used by 3rd party => js error --- Resources/public/js/views/ezs-multidomainbrowserview.js | 15 +++++++++++---- Resources/views/multidomain.script.twig | 10 ++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Resources/public/js/views/ezs-multidomainbrowserview.js b/Resources/public/js/views/ezs-multidomainbrowserview.js index 5fe2179..259abde 100644 --- a/Resources/public/js/views/ezs-multidomainbrowserview.js +++ b/Resources/public/js/views/ezs-multidomainbrowserview.js @@ -14,6 +14,7 @@ YUI.add('ezs-multidomainbrowserview', function (Y) { var ATTR_BLOCK_ID = 'data-block-id', ATTR_ZONE = 'data-studio-zone', SELECTOR_BLOCK = '.landing-page__block', + POST_MESSAGE_ID = 'eZEvent', VERSION_PREVIEW_URL_PREFIX = '/content/versionview/'; /** @@ -34,7 +35,11 @@ YUI.add('ezs-multidomainbrowserview', function (Y) { this.on('iframeDataChange', this._tryUpdateIframeHeight, this); this._postMassageHandler = Y.on('message', Y.bind(function (event) { - this.set('iframeData', event._event.data); + var data = event._event.data; + + if (data.id === POST_MESSAGE_ID) { + this.set('iframeData', data); + } }, this)); }, @@ -59,7 +64,7 @@ YUI.add('ezs-multidomainbrowserview', function (Y) { _updateIframeData: function (event) { var origin = this._getActiveSiteaccessRootUrl(); - event.target.getDOMNode().contentWindow.postMessage('data', origin); + event.target.getDOMNode().contentWindow.postMessage({id: POST_MESSAGE_ID}, origin); }, /** @@ -77,7 +82,8 @@ YUI.add('ezs-multidomainbrowserview', function (Y) { content: { selector: selector, content: blockPreview - } + }, + id: POST_MESSAGE_ID }; preview.postMessage(data, origin); @@ -104,7 +110,8 @@ YUI.add('ezs-multidomainbrowserview', function (Y) { attributeName: ATTR_BLOCK_ID, attributeValue: blockId, index: index - } + }, + id: POST_MESSAGE_ID }; preview.postMessage(data, origin); diff --git a/Resources/views/multidomain.script.twig b/Resources/views/multidomain.script.twig index 04610aa..04aca6f 100644 --- a/Resources/views/multidomain.script.twig +++ b/Resources/views/multidomain.script.twig @@ -3,8 +3,9 @@ 'use strict'; global.addEventListener('message', function (event) { - var AcceptableSources = {{ domains|json_encode|raw }}, - isKnownSource = AcceptableSources.some(function (source) { + var acceptableSources = {{ domains|json_encode|raw }}, + eventId = 'eZEvent', + isKnownSource = acceptableSources.some(function (source) { return source === event.origin; }), setBlockContent = function (config) { @@ -28,10 +29,11 @@ response = { height: doc.body.offsetHeight, pathName: global.location.pathname, - href: global.location.href + href: global.location.href, + id: eventId }; - if (!isKnownSource) { + if (!isKnownSource || event.data.id !== eventId) { return; }