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

bridge-iframe

v1.0.0

Published

A bridge for iframe

Downloads

3

Readme

IFrameBridge

使嵌套 iframe 之间的通信像计算机网络一样简单。

快速开始

将其添加到现有的 Vue 或 React 项目中:

# npm
npm install bridge-iframe
# pnpm
pnpm install bridge-iframe
# yarn
yarn add bridge-iframe

网络模型

                         /——> (子节点1)
(父节点) <———> (节点) <——————> (子节点2)
                         \——> (子节点n) ...
  • 每个页面都与一个上级页面和多个直接下级页面相关联。
  • 页面和上层页面可以直接通信传输数据。
  • 跨级页面可以通过转发的方式进行通信和传输数据。

系统协议

  • 内置地址:
    • Main 请求主窗口地址
    • Parent 向上级请求窗口(无论级别高低都向上级请求)
  • 主窗口提供的方法:
    • @bridge/online 通知主窗口注册地址
    • @bridge/domain 获取名称映射地址
    • @bridge/mapping 获取所有映射地址
  • 所有窗口提供的方法:
    • @bridge/ready 节点准备好了吗?

使用示例

下面展示一些常用的方法

桥接页面

  • 主页面 main.html
<h1>Main</h1>
<iframe src="node1.html" id="node1"></iframe>
import { IFrameBridge } from 'bridge-iframe';

// 创建桥接对象
const bridge = new IFrameBridge;
birdge.ifrme('Node1', document.getElementById('node1') as HTMLIFrameElement);

// 窗口销毁时(单页需要调用)
bridge.destroy();
  • 子页面 node1.html
<h1>Node1</h1>
import { IFrameBridge } from 'bridge-iframe';

// 创建桥接对象
const bridge = new IFrameBridge({ name: 'Node1' });

// 窗口销毁时(单页需要调用)
bridge.destroy();

订阅/发布

  • 窗口初始化
// 当前窗口初始化
bridge.ready(async () => {
    console.log('Main Ready');
});

// 指定窗口初始化
bridge.ready('Node1', async () => {
    console.log('Node1 Ready');
});
  • 订阅消息
// 订阅事件
bridge.on('say', async (vo: IFrameMessage) => {
    // vo.getData(); # 获取请求数据
    // vo.getResult(); # 获取响应结果
    return '我是 Main';
});

// 取消订阅事件
bridge.off('say');
  • 请求&响应
// 请求指定窗口
bridge.request({
    name: 'Node1', // 窗口名称(默认是 Main)
    method: 'say', // 方法名称
}).then((message: string) => {
    console.log('say.then', message);
}).catch((error: Error) => {
    console.log('say.catch', error);
});

// 请求上级页面(任意层级的直接上级)
bridge.request({
    name: 'Parent',
    method: 'say',
});

// 请求顶级页面
bridge.request({
    name: 'Main',
    method: 'say',
});

实现原理

  • 示例 iframe 嵌套层级

    • Main
      • Main/Node1
        • Main/Node1/Node1-1
        • Main/Node1/Node1-2
      • Main/Node2
        • Main/Node2/Node2-1
        • Main/Node2/Node2-2
  • 向上请求 Main/Node1/Node1-1Main

    • <内置协议获取地址>
    • Main/Node1/Node1-1 请求 ↑↑↑Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↑↑↑Main
      • tracks[{Node1-1:U}, {Node1:U}]
    • Main 处理逻辑
    • Main 响应 ↓↓↓Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↓↓↓Main/Node1/Node1-1
      • tracks[]
    • Main/Node1/Node1-1 收到响应
  • 向下请求 MainMain/Node1/Node1-1

    • <内置协议获取地址>
    • Main 请求 ↓↓↓Main/Node1
      • tracks[{Main:D}]
    • Main/Node1 转发 ↓↓↓Main/Node1/Node1-1
      • tracks[{Main:D}, {Node1:D}]
    • Main/Node1/Node1-1 处理逻辑
    • Main/Node1/Node1-1 响应 ↑↑↑Main/Node1
      • tracks[{Main:D}]
    • Main/Node1 转发 ↑↑↑Main
      • tracks[]
    • Main 收到响应
  • 同级请求 Main/Node1/Node1-1Main/Node1/Node1-2

    • <内置协议获取地址>
    • Main/Node1/Node1-1 请求 ↑↑↑Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↓↓↓Main/Node1/Node1-2
      • tracks[{Node1-1:U}, {Node1:D}]]
    • Main/Node1/Node1-2 处理逻辑
    • Main/Node1/Node1-2 响应 ↑↑↑Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↓↓↓Main/Node1/Node1-1
      • tracks[]
    • Main/Node1/Node1-1 收到响应
  • 跨级请求 Main/Node1/Node1-1Main/Node2/Node2-1

    • <内置协议获取地址>
    • Main/Node1/Node1-1 请求 ↑↑↑Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↑↑↑Main
      • tracks[{Node1-1:U}, {Node1:U}]
    • Main 转发 ↓↓↓Main/Node2
      • tracks[{Node1-1:U}, {Node1:U}, {Main:D}]
    • Main/Node2 转发 ↓↓↓Main/Node2/Node2-1
      • tracks[{Node1-1:U}, {Node1:U}, {Main:D}, {Node2:D}]
    • Main/Node2/Node2-1 处理逻辑
    • Main/Node2/Node2-1 响应 ↑↑↑Main/Node2
      • tracks[{Node1-1:U}, {Node1:U}, {Main:D}]
    • Main/Node2 转发 ↑↑↑Main
      • tracks[{Node1-1:U}, {Node1:U}]
    • Main 转发 ↓↓↓Main/Node1
      • tracks[{Node1-1:U}]
    • Main/Node1 转发 ↓↓↓Main/Node1/Node1-1
      • tracks[]
    • Main/Node1/Node1-1 收到响应