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

iframe-message-pipe

v0.0.5

Published

A two-way messaging channel between parent and child pages, based on the postMessage API.

Downloads

271

Readme

概述:基于 postMessage API 封装的一款用于父子页面(子页面为iframe形式)双向通信的工具包

核心思想

  • 基于 postMessage API 封装
  • owner:明确当前通道给谁用
  • trustOrigin:信任谁传来的消息 ( 更安全 🎉 ~)

优势

  • 父子页面双向通信,更全面
  • 子页面被其他子页面替换时,自动建立新的父子页面的消息通道,更便捷 🍊
  • 符合消息订阅与发布的使用模式,易于上手 🍓

使用范围

  • 父页面嵌套子页面,子页面使用iframe实现
  • 父子页面紧邻,不是深层嵌套
  • 父页面同时只可以和一个子页面之间建立双向消息通信
  • 允许一个子页面销毁,另一个子页面在创建消息通道时会自动和父页面建立双向通信关系

示例

  • 父页面:假设运行在 http://localhost:5173

import { MESSAGE_PIPE_OWNER, MessagePip } from 'iframe-message-pipe';


/*
 * 创建消息通道
 * 1.owner: 消息通道持有者(父页面)
 * 2.trustOrigin:父页面信任的消息源(正则表达式,匹配子页面iframe的src)
*/
const pip = new MessagePip({
    owner: MESSAGE_PIPE_OWNER.PARENT,
    trustOrigin: /http:\/\/localhost:5174/
});


/*
 * 父页面监听消息
*/
pip.$on('eatFruit', (value:any) => {
    console.log(value);
});

/*
 * 父页面触发事件
 */
pip.$emit('goSleep', {data: 'time to sleep'});
  • 子页面:假设运行在 http://localhost:5174
import { MESSAGE_PIPE_OWNER, MessagePip } from 'iframe-message-pipe';


/*
 * 创建消息通道
 * 1.owner: 消息通道持有者(子页面)
 * 2.trustOrigin:子页面信任的消息源(正则表达式,匹配父页面url)
*/
const pip = new MessagePip({
    owner: MESSAGE_PIPE_OWNER.CHILD,
    trustOrigin: /http:\/\/localhost:5173/
});

/*
 * 子页面触发事件
 */
pip.$emit('eatFruit', {data: 'good for health'});


/*
 * 子页面监听消息
*/
pip.$on('goSleep', (value:any) => {
    console.log(value);
});

核心构造函数

  • 指定消息通道的所有者
  • 指定当前通道所信任的消息源(信任哪个页面传来的消息,更安全)
const pip = new MessagePip({
    owner: MESSAGE_PIPE_OWNER.CHILD,
    trustOrigin: /http:\/\/localhost:5173/
});

API

  • $on (type: string, cb: Function)
    • 绑定事件
    • 同一个key可以绑定多个回调函数
  • $emit (type: string, value: object)
    • 触发事件
  • $off (type: string)
    • 解绑事件
    • 解绑key对应的所有回调函数