标签: postmessage

如何使postMessage适用于所有子域

window.postMessage第二个属性中指定可以将消息发送到的域.有没有办法指定它适用于所有子域.

事情尝试:

iframe.contentWindow.postMessage('The message to send.','http://*.wordpress.com');
iframe.contentWindow.postMessage('The message to send.','http://wordpress.com');
Run Code Online (Sandbox Code Playgroud)

html5 postmessage

18
推荐指数
2
解决办法
5813
查看次数

Uncaught SyntaxError:无法在'Window'上执行'postMessage':调用'postMessage'时无效的目标原点'my_page'

我有以下脚本

父页面(pair_pixel_filter.php):

 window.addEventListener("message", function(e) {
            $('#log').append("Received message: " + (e.data));
        }, false);
 $('.photo-upload-btn').click(function(event) {
            event.preventDefault();
            window.open($(this).attr("href"), "popupWindow", "width=600,height=600,scrollbars=yes");
        });
Run Code Online (Sandbox Code Playgroud)

儿童页面

$.ajax({
            type: 'post',
            url: url,
            data: {
                base64data: dataURL
            },
            success: function(data) {
                window.opener.postMessage(data, "pair_pixel_filter.php");
                window.close(); }
        });
Run Code Online (Sandbox Code Playgroud)

基本上打开Popup然后在弹出窗口上做一些ajax并将结果返回给父级.但是从Child我得到了这个错误.

未捕获的SyntaxError:无法在'Window'上执行'postMessage':调用'postMessage'时无效的目标原点'pair_pixel_filter.php'

javascript jquery postmessage cross-browser

17
推荐指数
1
解决办法
4万
查看次数

如何通过PostMessage发送字符串?

在我的应用程序中,我想从另一个线程向对话框发送消息.我想将std :: exception派生类引用传递给对话框.

像这样的东西:

try {
       //do stuff
}
catch (MyException& the_exception) {
    PostMessage(MyhWnd, CWM_SOME_ERROR, 0, 0); //send the_exception or the_exception.error_string() here
}
Run Code Online (Sandbox Code Playgroud)

我想在对话框中收到消息并显示错误 the_exception.error_string()

LPARAM CMyDlg::SomeError(WPARAM, LPARAM)
{
    show_error( ?????
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

std::string the_exception.error_string()我猜,传递使用PostMessage也没关系.

c++ mfc multithreading postmessage cdialog

16
推荐指数
1
解决办法
2万
查看次数

使用postMessage在两个iframe孩子之间进行通信

我有一些用户可以放在他们网站上的嵌入代码.它会在页面上创建两个子iframe.我想让这些孩子能够沟通.

我正在使用javascript的window.postMessage https://developer.mozilla.org/en-US/docs/DOM/window.postMessage

由于两个iframe孩子无法直接通信,我使用父亲作为消息的中继.但是,父级可以位于不同的域中,因为它是可嵌入的代码.

当所有三个(父项和两个孩子)都在同一个域上时,它非常简单,我使用安全检查检查 e.origin我自己的网站

# coffeescript
# host = "http://www.mysite.com"
host = "http://localhost"

receive_message = (e) ->
  console.log("received message from " + e.origin + ": " + e.data)
  return if e.origin != host

  if e.data == "show"
    ...
  else if e.data == "hide"
    ...

window.addEventListener("message", receive_message, false)
Run Code Online (Sandbox Code Playgroud)

当父级可以在任何域上时,检查原点的优雅方法是什么?

允许调试源可以是localhost的脚本的好方法是什么?

如果传递非破坏性/变化的消息,只检查数据参数是否足够?

谢谢!

javascript iframe postmessage

14
推荐指数
1
解决办法
7557
查看次数

top.postMessage原始错误没有被捕获

我正在尝试与postMessage实现通信.有一个主页面打开一个带有iframe的弹出窗口,它来自不同的域.到目前为止这工作正常但我想捕获以下错误,当我打开具有错误原点的iFrame时会发生这种错误.

无法在'DOMWindow'上执行'postMessage':提供的目标原点('myOriginURL')与收件人窗口的原点('myWindowsOrigin')不匹配.

origin = 'http://www.myorigin.ch';
if (window.postMessage) {
  try {
     top.postMessage('hello', origin);
  } 
  catch(ex) {
     alert('an error occured');
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是代码永远不会进入catch块.有趣的是,chrome在控制台中显示错误,而所有其他主要浏览器都没有做任何事情(没有警报,没有错误)

如何处理postMessage中的错误?

javascript postmessage try-catch

14
推荐指数
1
解决办法
1379
查看次数

在Cordova上保护postMessage()

我的Cordova移动应用程序使用iframe加载网站.我想通过iframe发送和接收内容postMessage().但是,通过我的测试我的移动应用程序来源总是localhost:8000file://.本网站上的每个其他示例都使用一个独特的域名和来源(例如www.example.com),但我的起源显然不是唯一的.

如果我的来源是localhost:8000/ file://?如何保护我的移动应用程序和我的网站之间的通信?如果由于某种原因我不能,我可以使用访问令牌来验证如下所示的任何通信吗?

移动应用

var iframe = document.getElementById('iframe');
var data = {
  'access_token': 'whatever'
};
iframe.contentWindow.postMessage(data, 'localhost:8000');
Run Code Online (Sandbox Code Playgroud)

website.com

window.addEventListener('message', function(event) {
  if (!event.data || !event.data.access_token) {return;}
  // ajax request to validate the token here
});
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,我正在做的事情是:

  • 检测iframe何时完成加载.iframe发回一个对象.{'loaded':true}
  • 当用户注销时, iframe 发送带有布尔值的对象.{'logout':true}
  • 当用户按下打印按钮时, iframe 发送带有布尔值和html字符串的对象.{'print':true, 'html':htmlString}
  • 将加密的有效负载发送到iframe,然后等待iframe返回一个对象. {'success':true}

iframe postmessage cordova

14
推荐指数
1
解决办法
398
查看次数

Flutter Web 监听通过 iFrame 发布的事件

我的目标是在我的主页上的iframe的帮助下集成一个 flutter 小部件。主页不是用flutter写的。但是,我需要一个界面,通过该界面主页可以与小部件进行通信。所以我想到在主页上使用postMessage()并在 flutter widget 中使用html.window.addEventListener() 。现在我在 flutter 中收到一个事件,但看不到它的内容。我的临时解决方案是将消息保存在本地存储中,并仅使用侦听器作为通知程序,但我不太喜欢这种方法。有人对我的问题有更好的解决方案吗?

i框架测试

颤振测试小部件:

// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'package:simple_cookies/simple_cookies.dart';


 class MessageListener extends StatefulWidget {
      @override
      _MessageListenerState createState() => _MessageListenerState();
  }

class _MessageListenerState extends State<MessageListener> {
  String last;

  @override
  void initState() {
    html.window.addEventListener('message', listen, true);
    super.initState();
  }

  @override
  void dispose() {
    html.window.removeEventListener('message', listen, true);
    super.dispose();
  }

  void listen(html.Event event) {
    last = Cookies.get('iframe_message') ?? 'Error';
    setState(() {});
  }

  @override
  Widget build(BuildContext …
Run Code Online (Sandbox Code Playgroud)

iframe postmessage local-storage flutter flutter-web

14
推荐指数
1
解决办法
9530
查看次数

13
推荐指数
2
解决办法
3655
查看次数

javascript - postMessage to sandboxed iframe,为什么收件人窗口来源为null?

2 postMessage调用测试:1使用星号表示targetOrigin,其中一个使用父文档和子文档的相同https URL.

按钮1:

$('.iframed')[0].contentWindow.postMessage( messageData , '*' );
Run Code Online (Sandbox Code Playgroud)

按钮2:

$('.iframed')[0].contentWindow.postMessage( messageData , 'https://myurl.net' );
Run Code Online (Sandbox Code Playgroud)

父html文档中的iframe元素,指向同一域中的子html文件,位于同一目录中:

<iframe name="childFrame" class="iframed" src="child.html" sandbox="allow-scripts"></iframe>
Run Code Online (Sandbox Code Playgroud)

在单击按钮以触发postMessage之前,两个文档都已完全加载.

==========================================

使用iframe元素如上所述,按钮1对子iframe执行postMessage并成功触发子的postMessage侦听器(尽管它使用了asOisk用于targetOrigin,我不想这样做.)但是,按钮2会产生以下结果控制台中的错误:

"无法在'DOMWindow'上执行'postMessage':提供的目标源(' https://myurl.net ')与收件人窗口的原点('null')不匹配."

==========================================

如果我将"allow-same-origin"添加到iframe的沙箱参数,则两个按钮都会成功传递postMessage数据(按钮2 postMessage调用没有"null"错误,并为targetOrigin提供了url.)但是,我不知道我想这样做,因为我使用iframe的沙盒行为来阻止iframe内容来调用父文档中的js函数.这是一个系统允许"任意"内容(html/js/images/pdfs - 没有像php这样的服务器可执行文件)加载到子iframe中.

也许值得注意的是,iframe内容中的postMessage到父文档的类似按钮工作正常,无论allow-same-origin参数还是asterisk/url的存在:

我陷害按钮1:

parent.postMessage( messageData , 'https://myurl.net' ); 
Run Code Online (Sandbox Code Playgroud)

iframed按钮2:

parent.postMessage( messageData , '*' ); 
Run Code Online (Sandbox Code Playgroud)

==========================================

所以,如果我不添加"allow-same-origin"(为什么这个问题不会影响iframe postMessage到父级),为什么postMessage从父级到iframe会导致上面的错误?我尝试将iframe src设置为child.html文档的绝对https网址,但结果是相同的.我还在不同的非ssl-cert服务器位置测试了相同的代码,并且具有相同的结果(所以不要认为它是https贡献...).我必须使用asterisk作为targetOrigin,并且/或者在沙箱参数中使用allow-same-origin?

关于这个问题的其他关于SO的谈话似乎是死路一条,因此希望对解决方案有新的看法......

html javascript iframe postmessage sandbox

13
推荐指数
1
解决办法
834
查看次数

未捕获的DOMException:无法在“窗口”上执行“ postMessage”

我出错了

content-script.js:24 Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'Window': An object could not be cloned.
    at Object.t.messageJumpContext (chrome-extension://elgalmkoelokbchhkhacckoklkejnhcd/build/content-script.js:24:9921)
    at chrome-extension://elgalmkoelokbchhkhacckoklkejnhcd/build/content-script.js:24:8583
Run Code Online (Sandbox Code Playgroud)

我在代码中的任何地方都没有使用window.postMessage。知道为什么会这样吗?

postmessage window angular

13
推荐指数
1
解决办法
2559
查看次数