百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

post-robot

> 编程语言
开源

在客户端使用简单的监听器/客户端模式来实现跨域后端消息传递。

785 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在客户端使用简单的监听器/客户端模式来实现跨域后端消息传递。

post-robot [:]-\- {

let user = await getUser(data.id);

return { id: data.id, name: user.name, }; });


```javascript
try {
  let { source, origin, data } = await postRobot.send(someWindow, `getUser`, {
    id: 1337,
  });
  console.log(source, origin, "Got user:", data.name);
} catch (err) {
  console.error(err);
}

Secure Message Channel

For security reasons, it is recommended that you always explicitly specify the window and domain you want to listen to and send messages to. This creates a secure message channel that only works between two windows on the specified domain:

postRobot.on(
  "getUser",
  { window: childWindow, domain: "http://zombo.com" },
  function (event) {
    return {
      id: event.data.id,
      name: "Frodo",
    };
  }
);
postRobot
  .send(someWindow, "getUser", { id: 1337 }, { domain: "http://zombo.com" })
  .then(function (event) {
    console.log(event.source, event.origin, "Got user:", event.data.name);
  })
  .catch(function (err) {
    console.error(err);
  });

Functions

Post robot lets you send across functions in your data payload, fairly seamlessly.

For example:

postRobot.on("getUser", function (event) {
  return {
    id: event.data.id,
    name: "Nogbad the Bad",

    logout: function () {
      currentUser.logout();
    },
  };
});
postRobot.send(myWindow, "getUser", { id: 1337 }).then(function (event) {
  var user = event.data;

  user.logout().then(function () {
    console.log("User was logged out");
  });
});

…


   [ Parent page ]

+---------------------+          [ Popup ]
|        xx.com       |
|                     |      +--------------+
|  +---------------+  |      |    yy.com    |
|  |    [iframe]   |  |      |              |
|  |               |  |      |              |
|  | yy.com/bridge |  |      |              |
|  |               |  |      |              |
|  |               |  |      |              |
|  |               |  |      |              |
|  |               |  |      +--------------+
|  +---------------+  |
|                     |
+---------------------+

a. Use the special ie build of post-robot: dist/post-robot.ie.js.

b. Create a bridge path on the domain of your popup, for example http://yy.com/bridge.html, and include post-robot:

c. In the parent page on xx.com which opens the popup, include the following javascript:

Now xx.com and yy.com can communicate freely using post-robot, in IE.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScripthacktoberfestpopuppopup-messagingpopup-windows

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言