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

nw-mwa

v0.2.3

Published

多WebView页面共存App通信SDK(`nw-multi-webview-app-communication`)

Downloads

3

Readme

nw-mwa

多WebView页面共存App通信SDK(nw-multi-webview-app-communication)

背景

  • LOFTER等业务支持使用新WebView打开指定H5 url的能力,这种能力相比直接的location.href跳转,在维持页面状态,支持手势滑动以及页面出入场动画方面有着更好的表现
  • 但是多Webview页面之间的协同通信是一个需要解决的问题,因为WebView页面A的关闭和操作,WebView页面B是无法直接感知的
  • 因此,需要一个通信SDK,让两个WebView页面之间的通信能够更加灵活,更加方便

原理

利用多WebView之间localStorage是实时共享的机制,并且借助storageonchange事件,实现WebView页面之间的通信

使用

import { addMpaListener, removeMpaListener, triggerMpaEvent, Message } from 'nw-mwa'
import { useEffect } from 'react'
// WebView页面A
useEffect(() => {
  addMpaListener('addressSelect', (data: Message) => {
    console.log(data);
    if (data.url === 'https://b.html') {
      console.log('用户选择的地址是:', data.message.address);
    }
  })
  return () => {
    removeMpaListener('addressSelect')
  }
})


// WebView页面B
triggerMpaEvent('addressSelect', {
  address: '北京市海淀区上地十街10号'
})

注意事项

  • 为防止事件错乱,一个页面对同一个事件名称只能同时存在一个监听器,后注册的会覆盖前者,所以移除事件监听也无需传递监听器函数
  • 监听到事件回调后,回调参数中同时含有消息的发送时间以及发送消息的页面URL,可以用来判断消息是否是来自于指定页面
  • 注意及时调用removeMpaListener移除监听,否则可能会导致事件错乱