I need to implement secure communication between Visualforce embedded on a Lightning Component. The LC sits on a Flexipage.
One of my goals is, that the VF-page sends it's actual height to the LC so that the LC can adjust the height of the iframe. Fixed heights are always killing UX. But many, many other meaningful and painfully needed use cases can be solved using the same secure communication method.
Now without Locker Service the world is fine with this markup:
<aura:handler name="init" value="{!this}" action="{!c.init}" />
<iframe src="/apex/yourVisualforcePageName" width="100%"></iframe>
And this controller:
init : function(cmp, evt, hlp) {
window.addEventListener("message", elfMessageRouter, false);
function elfMessageRouter(event) {
console.log('RECEIVED',event.data);
}
},
Usage is simple:
window.parent.postMessage( { foo: "bar", height: 123 } ,'*' );
And you get something nice in your console:
RECEIVED Object {foo: "bar", height: 123}
But now comes Locker Service... I go to Setup > Critical Updates and turn that baby on, I change nothing, reload my page in LEX and get only:
RECEIVED Object {}
Which shows that the process works but the parameter is busted.
How this can be optimized? Is this communication method at all accepted or considered as some kind of threat or unwanted feature?
Btw. the fixed-iframe-height was my main pain point for nearly 10 years of Aloha-live... Many have asked for dynamic sized embedding containers on standard layouts - but they never came. Then (out of desperation) we came up with crap like these hacks: Why do we still need to hack the Sidebar? Usecases - Workarounds - Alternatives
How we can do it right now and secure from the beginning?
window.parent.postMessage( { foo: "bar", height: 123 } ,'*' );
<script>window.parent.postMessage( { foo: "bar", height: 123 } ,'*' );</script>
and the console prints an object with both thefoo
andheight
property set. This works for me in a standalone app and in LEX for an app built via Lightning App Builder.