【问题标题】:How to postMessage into iFrame?如何将消息发布到 iFrame 中?
【发布时间】:2020-08-16 06:56:53
【问题描述】:

我正在开发一个 Chrome 扩展程序,用于我自己的 Web 应用程序,处理网页(包括跨域网页),加载到我的应用程序主页上的 iFrame。所以我必须发送包含要处理页面的 URL 的消息,从我的主页到内容脚本,注入目标 iFrame 以访问已处理页面的窗口。

我尝试实施here 描述的方法。以下是所有相关文件的基本部分:

index.html:

...
<head>
    ...
    <script type="text/javascript" src="base.js"></script>
</head>
<body>
    <div>
        <input type="text" id="page_url">&nbsp;<input type="button" id="get_page" value="Get page">
    </div>
    <iframe id="cross_domain_page" src=""></iframe>
</body>

base.js:

(function() {
    "use strict";
    function pageProcessing() {
        let urlButton = window.document.getElementById("get_page");
        urlButton.addEventListener("click", () => {
            let url = window.document.getElementById("page_url").value;
            window.postMessage(
                {
                    sender: "get_page_button1",
                    message: url
                },
                "*"
            );
            window.postMessage(
                {
                    sender: "get_page_button2",
                    message: url
                },
                "*"
            );
        });
    }
    window.document.addEventListener('readystatechange', () => {
            if (window.document.readyState == 'complete') {
                pageProcessing();
            }
        }
    );
})();

manifest.json:

{
  "manifest_version": 2,
  "name": "GetPage",
  "version": "0.1",
  "content_scripts": [
    { "js": ["main.js"], "matches": ["<all_urls>"] },
    { "js": ["frame.js"], "matches": ["<all_urls>"], "all_frames": true, "match_about_blank": true }
  ],
  "permissions": ["activeTab"]
}

main.js:

(function() {
    "use strict";
    window.isTop = true;
})();

frame.js:

(function() {
    "use strict";
    window.addEventListener("message", (event) => {
        if (event.data &&
            event.data.sender == "get_page_button1") {
            if (window.isTop) {
                alert("Main window alert");
            } else {
                alert("Frame window alert 1");
            }
        }
    });
    if (!window.isTop) {
        window.addEventListener("message", (event) => {
            if (event.data &&
                event.data.sender == "get_page_button2") {
                alert("Frame window alert 2");
            }
        });
    }
})();

问题是当我单击“get_page”按钮时,我看到的唯一警报是主窗口警报。至于我的理解,这意味着从主窗口发布的消息没有到达内容脚本,注入iFrame

我的脚本有什么问题以及如何解决问题?

【问题讨论】:

    标签: javascript iframe google-chrome-extension postmessage content-script


    【解决方案1】:

    您的网络应用程序中的window.postMessage 发送到主文档的window,而不是iframe。

    指定 iframe 的窗口对象:

    document.getElementById('cross_domain_page').contentWindow.postMessage(.......)
    

    或者,您可以切换到更安全的externally_connectable 消息传递。

    【讨论】:

    • 以这种方式工作的函数被命名为“POSTmessage”对我来说看起来很奇怪,因为它实际上为特定窗口获取消息。命名约定有时很奇怪。 )) 但是您的解决方案有效,非常感谢@wOxxOm。
    • 您的回答是否遵循将消息从iFrame 发送回主窗口我必须使用if (!window.isTop) { ... window.parent.postMessage(...); } 之类的东西?
    • postMessage 是否适用于跨域 iframe?我无法访问加载跨域页面的 iframe 的 contentWindow。
    • postMessage 旨在与跨域 iframe/windows 一起使用。使用 MCVE 提出新问题。
    • 谢谢@wOxxOm!花了这么多时间!
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2022-07-15
    • 2016-10-22
    • 2018-11-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多