Javascript 无法使用Chrome扩展中的postMessage从父窗口向iframe发送数据

Javascript 无法使用Chrome扩展中的postMessage从父窗口向iframe发送数据,javascript,iframe,google-chrome-extension,cross-domain,postmessage,Javascript,Iframe,Google Chrome Extension,Cross Domain,Postmessage,我正在开发一个Google Chrome扩展,它在网页上注入一个脚本,从网页收集一些数据,调用一个iframe并将数据发送到iframe。由于此过程需要跨域,因此我只能考虑使用postMessage来实现此过程,但我无法在iframe中接收此数据。(顺便问一下,是否有其他方法可以实现相同的功能? 下面是我的脚本,当用户单击扩展时,我将其注入到网页中。 我在控制台中没有看到任何错误,但是receiveMessage1功能无法工作。可能的原因是什么? P.S.我能够从iframe向父级发送消

我正在开发一个Google Chrome扩展,它在网页上注入一个脚本,从网页收集一些数据,调用一个iframe并将数据发送到iframe。由于此过程需要跨域,因此我只能考虑使用postMessage来实现此过程,但我无法在iframe中接收此数据。
(顺便问一下,是否有其他方法可以实现相同的功能?

下面是我的脚本,当用户单击扩展时,我将其注入到网页中。


我在控制台中没有看到任何错误,但是
receiveMessage1
功能无法工作。可能的原因是什么?


P.S.我能够从iframe向父级发送消息,但不能从父级发送消息。

在我看来,使用

postMessage()
方法仍然是跨域传递消息的更安全的方法

要使用此方法,请注意它接受两个参数:

  • 消息
    –将发送给接收方的字符串或对象 窗户
  • targetOrigin
    –发送消息的窗口的URL 到目标窗口的协议、端口和主机名必须匹配 此参数用于要发送的消息。指定“*”将匹配 但是,出于安全原因,强烈反对使用任何URL
并且,应该在消息设置为的窗口上调用此方法。可以通过多种不同的方式获得对目标窗口的引用:

  • 使用
    window.open()
    时,将显示对新窗口的引用 由
    open()
    方法返回
  • 对于iFrame,您可以访问所需服务器上的
    contentWindow
    属性 iframe。
    targetWindow.postMessage('Hello World!','http://example.com');


使用技巧、有关实现的更多信息和完整指南都在本博客中提供,我认为这篇文章也会很有帮助。

试着不要使用setTimeout和window.onload,只需这样:

<script>

    function receiveMessage1(e) {
        console.log('mSG');
        if (e.origin !== currentUrl)
            return;

        parentTitle = e.data;
        console.log(parentTitle);
    }
    if (window.addEventListener){
        console.log('if');
      window.addEventListener("message", receiveMessage1, false);
      console.log('if end');
    } else {
        console.log('else');
      attachEvent("onmessage", receiveMessage1);
      console.log('else end');
    }
</script>

函数receiveMessage1(e){
console.log('mSG');
如果(如来源!==当前URL)
返回;
parentTitle=e.data;
console.log(parentTitle);
}
if(window.addEventListener){
console.log('if');
window.addEventListener(“消息”,receiveMessage1,false);
console.log('if end');
}否则{
console.log('else');
附件(“onmessage”,receiveMessage1);
console.log('else end');
}

不应在iframe中使用setTimeout,否则将在消息后添加消息事件。@jcubic我最初没有使用setTimeout,但这也不起作用。只是在试验..澄清一下:您的iframe确实会打印
if
if end
,但不会打印
mSG
?同样,是的。您正在等待附加一个侦听器,这似乎不是一个好主意。@Xan正是。。它会打印IF,IF END,但不打印mSGThanks以获取答案,但它仍然没有显示我
console.log('mSG')
。我在这方面可能会犯什么错误?最后我看到了这一点,但它并不总是有效的。对我来说,这听起来很愚蠢,但实际情况是,10次中只有2次有效。“WORKING”的意思是它在控制台中显示“mSG”。@TusharShukla可能使用
$('iframe').load(function(){
而不是
setTimeout
。1000票赞成(如果我可以:p)…非常感谢
window.onload = function() {
        function receiveMessage1(e) {
            console.log('mSG');
            if (e.origin !== currentUrl)
                return;

            parentTitle = e.data;
            console.log(parentTitle);
        }
        if (window.addEventListener){
            console.log('if');
          window.addEventListener("message", receiveMessage1, false);
          console.log('if end');
        } else {
            console.log('else');
          attachEvent("onmessage", receiveMessage1);
          console.log('else end');
        }
}
<script>

    function receiveMessage1(e) {
        console.log('mSG');
        if (e.origin !== currentUrl)
            return;

        parentTitle = e.data;
        console.log(parentTitle);
    }
    if (window.addEventListener){
        console.log('if');
      window.addEventListener("message", receiveMessage1, false);
      console.log('if end');
    } else {
        console.log('else');
      attachEvent("onmessage", receiveMessage1);
      console.log('else end');
    }
</script>