admin管理员组文章数量:1279148
Is it possible to open a site in an InAppBrowser, have that site use window.open
to open another window, then send a message to that other window (and vice versa)?
Is it possible to open a site in an InAppBrowser, have that site use window.open
to open another window, then send a message to that other window (and vice versa)?
2 Answers
Reset to default 7Postmessage is already implemented on not released version. You can fork the most recent dev version on inAppBrowser from their git page: https://github./apache/cordova-plugin-inappbrowser/ Before building it remember to remove the current ponent and add the most recent dev version for using it. As its described in their documentation you can dispatch postmessage like:
inAppBrowserRef.executeScript({ code: "\
var message = 'this is the message';\
var messageObj = {my_message: message};\
var stringifiedMessageObj = JSON.stringify(messageObj);\
webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
});
Or from inside inAppBrowser's app its something like:
const message = 'message'
const messageObj = {message: message}
const stringifiedMessageObj = JSON.stringify(messageObj)
if (window.webkit && Window.webkit.messageHandlers) {
console.log('postmessage call on webkit')
window.webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj)
}
And you can listen for it inside cordova like:
this.inAppBrowserRef.on('message').subscribe((event) => {
console.log(' postmessage received')
const postObject:any = event
if(postObject.data.message){
console.log(postObject.data.message)
}
})
InAppBrowser has a limitation regarding two-way Communication: InappBrowser doesn't allow continuous munication
Solution 1 (Few Limitations)
Using IFRAME:
var myIframe = document.getElementbyId(IFRAME_ID).contentWindow;
Send msg
to iframe
from parent window:
myIframe.postmessage("Hello World", "*")
Receive msg
in iframe
from parent window:
window.addEventListener("message", function(e)
{
// add your code here
});
Send msg
to parent window from iframe
:
window.parent.postmessage("msg from iframe", "*")
Receive msg
in parent window from iframe
:
window.addEventListener("message", function(e)
{
// add your code here
});
Limitation:
If you try to navigate from one domain to another in the end, you may get error related to x-frame-options.
Solution 2 (Remended)
Use cordova-plugin-wizviewmanager: to send a message from one view another:
wizViewMessenger.postMessage(message_data, targetView_name);
To receive message from one view another:
window.addEventListener("message", function(event)
{
// add your code here
});
Advantages:
- Directly municate b/w App (mainView) to other custom (user-created) View.
- No error related to x-Frame Options
Github Link:
https://github./Wizcorp/cordova-plugin-wizviewmanager
本文标签: javascriptInAppBrowserOpen WindowPost MessageStack Overflow
版权声明:本文标题:javascript - InAppBrowser, Open Window, Post Message - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741262487a2367879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论