Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive postmessage inside Facebook in-app browser?

I'm trying to send postmessage from the opened window to opener in facebook app browser, but the "opener window" never receives messages. What can be the cause of the problem?

Receiver side:

window.addEventListener('message', function (e) {
    window.console.log("on message: " + e.data); 
}, false)

Sender side:

window.opener.postMessage('any Message', document.location.origin);
like image 386
Максим Горбунов Avatar asked Aug 06 '19 07:08

Максим Горбунов


People also ask

Can I use window postMessage?

window. postMessage() is a safe way to send messages between windows in different domains or origins. One can also post to an IFrame. The data being sent is serialized using the structured clone algorithm and will accept almost any type of simple or complex data.

What is window postMessage () used for?

postMessage() The window. postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.


1 Answers

It's hard to tell without seeing more of your code, but as this Opening facebook connect window via javascript? answer states, if you're trying to access the oAuth page, it's not possible.

Show us where you get the variable window.opener, that might add some context.

If you opened it from window.open(/page/), it appears that it is specifically blocked: How do I get around window.opener cross-domain security as mentioned in that question:

NOTE

Social signups do not work for google, FB, etc within an iframe. I believe they disallow them for security reasons.

Also from window.opener is null after redirect

window.opener is removed whenever you navigate to a different host (for security reasons), there is no way around it. The only option should be doing the payment in a frame if it is possible. The top document needs to stay on the same host.

But as mentioned in the second answer quoted, instead of using window.opener on the opened page, do everything from the origninal page, and (IF you have access to the source of the popup), make an onmessage on the other page, like mentioned in the accepted answer there, that the correct way to do it is only in reverse:

Do it the other way around. Track the state of the child popup window from the main (opener) window, and you could easily know when the child window has been navigated back to you domain, so you could "talk" to it again. But don't close the child window by itself. Let the opener window obtain the result from the child window and then close it.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Example

For example, in your started page, do something like:

var popup = window.open(/*some URL*/);
popup.postMessage("hi");
addEventListener("message", function(e) {
    console.log(e);
    e.source.postMessage("Hi there"); //official workaround for window.opener on other page
})

then in your "/some URL/" source code page, do something like:

addEventListener("message", function(e) {
    console.log(e);
    e.source.postMessage("hi back");
});

and just play around with that strategy, but it appears window.opener is out of the picture. Just try console.logging it, it just say null.

like image 94
B''H Bi'ezras -- Boruch Hashem Avatar answered Oct 23 '22 18:10

B''H Bi'ezras -- Boruch Hashem