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-mitt

v0.1.5

Published

communicate with iframe

Downloads

51

Readme

iframe-mitt

  • 可靠性:避免父级 window 向 iframe 发送的 message 丢失
  • 实用:无需自己监听 message 事件,维护消息队列
  • 用法:利用 mitt 的事件分发机制,接口简洁

安装

$ npm install iframe-mitt --save
or
$ yarn add iframe-mitt

引用方式:

// ES6 模块引用
import IframeMitt from 'iframe-mitt'

// CommonJS 模块引用
var IframeMitt = require('iframe-mitt')

使用

iframe 父级页面调用:

import IframeMitt from 'iframe-mitt';

// iframe 加载之前的前置初始化
IframeMitt.parent.init();

// 可以在任意时刻触发事件(比如::iframe 初始化之前),无需担心 message 丢失问题
IframeMitt.parent.emit('parent:first', {
    message: 'the first message',
    targetOrigin: 'http://local.xxx.com:9001'
});

// 监听 iframe 的 message
IframeMitt.parent.on('child:first', (event) => {
    console.log(event.data); // 'the first child message'
});

// 使用处理函数引用
function onFoo() {}
IframeMitt.parent.on('foo', onFoo)   // 监听
IframeMitt.parent.off('foo', onFoo)  // 注销

iframe 内部调用:

import IframeMitt from 'iframe-mitt';

// 前置初始化
IframeMitt.child.init();

IframeMitt.child.on('parent:first', (event) => {
    console.log(event.data); // 'the first message'
});

IframeMitt.child.emit('child:first', {
    message: 'the first child message',
    targetOrigin: '*'
});

IframeMitt.child.on('*', (type, event) => {
    console.log(type, event.data); 
});

API

on

注册 message 事件的处理函数

参数

  • type: String 类型,事件名称,"*" 表示所有事件
  • handler: Function 类型,响应事件的处理函数,该函数的 event 参数的属性如下,具体参考Window.postMessage
    • data: 从其他 window 传递过来的对象
    • orgin: 调用 postMessage 时消息发送方窗口的 origin . 这个字符串由 协议、“://“、域名、“ : 端口号”拼接而成。
    • source: 对发送消息的窗口对象的引用;

off

移除 message 事件的处理函数

参数

  • type: String 类型,事件名称,"*" 表示所有事件
  • handler: Function 类型,要移除的处理函数

emit

调用该事件的所有处理函数,"*" 的处理函数在具体事件的处理函数之后调用

参数

  • type: String 类型,事件名称
  • event: Object 类型,包含三个参数:{ message, targetOrigin, transfer },与 postMessage 参数含义一致,具体含义可以参考Window.postMessage