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

cic

v2.0.10

Published

Safe cross-iframe-communication js framework

Downloads

21

Readme

cic

简单易用的iframe通信库

索引

浏览器兼容性

  • 兼容IE8+

使用方法

  1. parentiframe中都使用var connection = createConnection():Connection初始化连接对象
  2. connection实例添加监听方法:onMessage,onConnect,onDisconnect
  3. 接下来需要其中一方发起通信连接请求connection.connect(domWindow)
  4. 注意
    1. **onConnection**在回调执行后,才可以connection.sendMsg(msg)进行消息传输
    2. 或者在执行connection.sendMsg(msg)前判断connection.connectedtrue
  5. 如果通信连接已建立
    1. 那么iframe窗口内部页面刷新或者跳转到新的url地址之前,都会出发disconnect事件
  6. 主动断开连接或收到断开连接事件后,如果重新连接,需要其中一方再次主动调用connection.connect(domWindow)方法

库引入方法

引入库代库后执行一个工厂方法,生产一个Connection类的实例

第一种 直接全局引入cic.js文件

<script src="https://unpkg.com/[email protected]/dist/cic.min.js" type="text/script"></script>

<script type="text/script">
  var connection = window.Cic.createConnection();
</script>

第二种 module形式引入

import { createConnection } from 'cic';
var connection = createConnection();

API

1. 静态工厂方法Cic.createConnection():Connection

返回一个Connection类实例

2. Connection

2.1 实例属性connection.connected:Boolean

跨iframe通信是否已连通

2.2 实例属性connection.destroyed:Boolean

如果实例已销毁,那么当前实例不可再次发起连接请求

2.3 connection.onConnect(callback)

添加通信连接成功时的回调方法

2.4 connection.offConnect(callback)

移除通信连接成功时的回调方法

注意:**其中callback和传入onConnect的回调方法是同一个。**如果无法理解可参考addEventListener

2.5 connection.onDisconnect(callback)

添加通信断开连接时的回调方法

2.6 connection.offDisconnect(callback)

移除通信断开连接时的回调方法

注意:**其中callback和传入onDisconnect的回调方法是同一个。**如果无法理解可参考addEventListener

2.7 connection.onMessage(callback:(msg:any)=>void)

添加接收消息的回调方法

2.8 connection.offMessage(callback:(msg:any)=>void)

移除接收消息的回调方法。

注意:**其中callback和传入onMessage的回调方法是同一个。**如果无法理解可参考addEventListener

2.9 connection.destroy()

资源回收。如果连接已建立那么触发disconnect事件,通知对方连接断开

2.10 connection.connect(domWindow)

  • 说明:发起连接请求
  • 参数:domWindow表示需要连接的window对象
  • parentiframe内部都可以主动建立连接,主动建立连接的一方需要传入domWindow

2.11 connection.sendMsg(msg:any)

  • 说明:发送消息
  • 参数:msg可以是任意对象
  • 注意:发送消息前需要确定在onConnect回调执行以后

示例代码

parent窗口代码,文件名index.html

<iframe id="iframeWindow" src="./inside.html"></iframe>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/cic.js"></script>
<script type="text/javascript">
    var connection = window.Cic.createConnection();

    connection.onMessage(function(dataFromIframe) {
        console.log('< parent: dataFromIframe =');
        console.log(dataFromIframe);
        if (dataFromIframe.text === 'world') {
            //console.log('< 外层窗口 主动发起断开连接');
            //connection.disconnect();
        }
    });

    connection.onConnect(function() {
        connection.sendMsg({
            text: 'hello'
        })
    });

    connection.onDisconnect(function() {
        console.log('内部断开链接,可能是内部网址发生变化。');
        
    });

    // 发起连接
    connection.connect(document.getElementById('iframeWindow'));
</script>

iframe窗口代码,文件名inside.html

  <script type="text/javascript" src="https://unpkg.com/[email protected]/dist/cic.js"></script>
  <script type="text/javascript">
  // 用setTimeout故意延迟几秒,等待重试连接
  setTimeout(function() {
    var connection = window.Cic.createConnection();

    connection.onConnect(function() {
      console.log('> 连接已建立');
    });

    connection.onMessage(function(dataFromParent) {
      console.log('> dataFromParent =');
      console.log(dataFromParent);

      if (dataFromParent.text === 'hello') {
        connection.sendMsg({
          text: 'world'
        });
      }
    });

    connection.onDisconnect(function() {
      console.log('> iframe 收到断开链接的请求。然后执行链接销毁功能');
      connection.destroy();
    });
  }, 1000);

  setTimeout(function() {
    location.href = "https://baidu.com";
  }, 3000);
  </script>