npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kaokei/post-bridge

v1.0.2

Published

jsbridge for iframes or windows by window.postMessage

Downloads

9

Readme

github 地址

解决了什么问题?

主要是解决了父子 iframe 之间互相通信的问题。可以像 rpc 一样简单的调用对方的方法,并且获取返回值,并支持 Promise。

整体方案以及使用方式

考虑这样的场景,一个页面 A 包含两个 iframe,分别是 B 和 C。

那么在 A 页面中:

PostBridge.registerMethods({
  sayHello() {
    console.log('hello from Page A');
  },
  getUserName() {
    return 'userNameA';
  },
});
PostBridge.start();

// 需要手动获取 iframeB 和 iframeC 的dom对象
const postBridgeB = new PostBridge(iframeB.contentWindow);
const postBridgeC = new PostBridge(iframeC.contentWindow);

// iframeB 中输出 `hello from Page B`
postBridgeB.call('sayHello');
// iframeA 中获取 userName,并打印出 `userNameB`
postBridgeB.request('getUserName').then(name => console.log(name));

// iframeC 中输出 `hello from Page C`
postBridgeC.call('sayHello');
// iframeA 中获取 userName,并打印出 `userNameC`
postBridgeC.request('getUserName').then(name => console.log(name));

然后在 B 页面中:

PostBridge.registerMethods({
  sayHello() {
    console.log('hello from Page B');
  },
  getUserName() {
    return 'userNameB';
  },
});
PostBridge.start();

// 其实也可以使用 window.top
const postBridgeA = new PostBridge(window.parent);

// iframeA 中输出 `hello from Page A`
postBridgeA.call('sayHello');
// iframeB 中获取 userName,并打印出 `userNameA`
postBridgeA.request('getUserName').then(name => console.log(name));

页面 C 和页面 B 是类似的,就不再重复了。

可以发现我们使用静态类 PostBridge 本身代表当前页面,主要是通过 registerMethods 来暴露对外的接口,以及通过 start 来监听所有事件。

我们用 PostBridge 的实例对象来代表我们想要通信的页面,比如在 A 页面中我们需要和 B 以及 C 通信,那么就需要实例化 postBridgeB 和 postBridgeC 两个对象。并且需要传递 iframeDom.contentWindow 作为参数。同样的在页面 B 和页面 C 中则需要传递 window.parent 作为参数。

最终的效果是我们可以使用 postBridgeA 对象调用页面 A 中的函数,使用 postBridgeB 对象调用页面 B 中的函数,使用 postBridgeC 对象调用页面 C 中的函数。

父子页面的通信比较简单直接,如果想要实现页面 B 和页面 C 之间的互相通信,则比较麻烦了,因为页面 B 和页面 C 之间是互相没有感知的。如果确实有这个需求,我们可以通过父页面来中转消息。