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

javascript-jsbridge-sdk

v1.0.4

Published

A lightweight jsbridge for sending messages between javascript and native code(iOS/Android/ReactNative/Flutter) in WebViews.

Downloads

352

Readme

介绍

一个轻量级的 jsbridge,用于在 WebView 中的 javascript 和 flutter 之间发送消息。

功能

  • 支持渠道设置,兼容 flutter、reactnative、iframe 等渠道,默认 flutter
  • 支持查看消息日志(debug 模式)
  • 支持注册方法
  • 支持取消注册方法
  • 支持 Flutter 和 Javascript 之间方法互相调用,传递参数、接收返回结果

开始

  • html文件中引用script标签
<script src="https://unpkg.com/javascript-jsbridge-sdk@latest/dist/jsbridge.umd.js"></script>
  • 在使用 window.WebViewJSBridge 之前,调用 setupWebViewJSBridge 方法确保 window.WebViewJSBridge 存在
var isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
var isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
function setupWebViewJSBridge (callback) {
 // Android使用
 if (isAndroid) {
   if (window.WebViewJSBridge) {
     callback(WebViewJSBridge)
   } else {
     document.addEventListener(
       'WebViewJSBridgeReady',
       function () {
         callback(WebViewJSBridge)
       },
       false
     )
   }
 }

 // iOS使用
 if (isiOS) {
   if (window.WebViewJSBridge) {
     return callback(WebViewJSBridge)
   }
   if (window.WVJBCallbacks) {
     return window.WVJBCallbacks.push(callback)
   }
   window.WVJBCallbacks = [callback]
   var WVJBIframe = document.createElement('iframe')
   WVJBIframe.style.display = 'none'
   WVJBIframe.src = 'wvjbscheme://__bridge_loaded__'
   document.documentElement.appendChild(WVJBIframe)
   setTimeout(function () {
     document.documentElement.removeChild(WVJBIframe)
   }, 0)
 }
}

用法

初始化配置

  window.WebViewJSBridge.init({debug: true, channel: ['flutter']});

| 参数 | 说明 | 默认值和类型 | 必传 | | ------- | --------------------------------------------- | ------------------- | ---- | | debug | 调试模式 | false(Boolean) | 否 | | channel | 渠道,支持 android、ios、reactnative、flutter | ['flutter'] (Array) | 否 |

注册方法

callback 模式

  window.WebViewJSBridge.registerHandler('JSEcho', function (data, success, fail) {
    success('success response from javascript');
    // fail('fail response from javascript');
  });

| 参数 | 说明 | 默认值和类型 | 必传 | | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---- | | handlerName | 注册的方法名称 | (String) | 是 | | handler | 注册的方法实现,没有返回值data:发送过来的数据success:js 端业务处理成功时通知 native 端fail:js 端业务处理失败时通知 native 端 | (Function) | 是 |

promise 模式

  window.WebViewJSBridge.registerHandler('JSEcho', async function (data) {
    // return Promise.resolve('success response from javascript');
    return 'success response from javascript';
    // return Promise.reject('fail response from javascript');
    // throw Error('fail response from javascript');
  });

| 参数 | 说明 | 默认值和类型 | 必传 | | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------ | ---- | | handlerName | 注册的方法名称 | (String) | 是 | | handler | 注册的方法实现,返回 Promisedata:发送过来的数据Promise.resolve:js 端业务处理成功时通知 native 端Promise.reject:js 端业务处理失败时通知 native 端 | (Function) | 是 |

取消注册方法

  window.WebViewJSBridge.unregisterHandler('JSEcho');

| 参数 | 说明 | 默认值和类型 | 必传 | | ----------- | -------------- | ------------ | ---- | | handlerName | 注册的方法名称 | (Boolean) | 是 |

调用方法

callback 模式

  window.WebViewJSBridge.callHandler('FlutterEcho', {
    data: 'request from javascript',
    success: function (data) {
      print('[call handler] success response: ' + data);
    },
    fail: function (err) {
      print('[call handler] fail response: ' + err);
    },
  });

| 参数 | 说明 | 默认值和类型 | 必传 | | ----------- | ----------------------------------------------------------------------------------------------------------------- | ------------ | ---- | | handlerName | 调用的方法名称 | (String) | 是 | | payload | 参数对象data:发送过来的数据success:native 端业务处理成功时的回调fail:native 端业务处理失败时的回调 | (Object) | 否 |

promise 模式

  try {
    var data = await window.WebViewJSBridge.callHandler('FlutterEcho', {
      data: 'request from javascript',
    });
    print('[call handler] success response: ' + data);
  } catch (err) {
    print('[call handler] fail response: ' + err);
  }

| 参数 | 说明 | 默认值和类型 | 必传 | | ----------- | ----------------------------------------------------------------------------------------------- | ------------ | ---- | | handlerName | 调用的方法名称 | (String) | 是 | | payload | 参数对象data:发送过来的数据 | (Object) | 是 | | return | 返回 Promiseresolve:native 端业务处理成功时的回调reject:native 端业务处理失败时的回调 | (Promise) | 否 |