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

react-native-webviewjavascriptbridge

v0.0.7

Published

WebViewJavascriptBridge For React Native. Implemented on react-native-webview. JS Only. React Native API Supported.

Downloads

3

Readme

WebviewJavascriptBridge for React Native

WebviewJavascriptBridge implemented on react-native-webview

The code logic comes from the WebviewJavascriptBridge. It is fully implemented by JS. In theory, it supports the platform supported by react-native-webview.

  • [x] iOS (Tested)
  • [x] Android (Tested)
  • [x] MacOS (Tested)
  • [x] Windows (Not tested)

Usage reference

https://github.com/marcuswestin/WebViewJavascriptBridge#usage

Install

In the React Native project:

npm i react-native-webviewjavascriptbridge

in the Web project:

npm i webview-javascript-bridge-promised

Or add code to a Web project:

function setupWebViewJavascriptBridge(callback) {
  if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
  if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
  window.WVJBCallbacks = [callback];
  var WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'https://__bridge_loaded__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

Method registration and call

In React Native:

  import WebViewJavascriptBridge from 'react-native-webviewjavascriptbridge';
  
  render() {
    return (
      <WebViewJavascriptBridge ref="bridge"/>
    )
  }
  
  //register
  registerRNApi() {
    this.refs.bridge.registerHandler('rn_method_1',(params, responseCallback) => {
      console.log('call rn_method_1 from web',params);
      responseCallback('success');
    });
  }
  
  //call
  callWebApi() {
    this.refs.bridge.callHandler('web_method_1',{'key':'value'},(res) => {
      console.log('call web_method_1 resp:',res);
    });
  }

In Web:

Using webview-javascript-bridge-promised:

var bridge = require('webview-javascript-bridge-promised')

//register
bridge.registerHandler('web_method_1',(params, responseCallback) => {
    console.log('call web_method_1 from RN',params);
    responseCallback('success');
});

//call
  bridge.callHandler('rn_method_1',{'key':'value'},(res) => {
      console.log('call rn_method_1 resp:',res);
  }); 

Using add code setupWebViewJavascriptBridge(callback):

setupWebViewJavascriptBridge(function(bridge) {

  /* Initialize your app here */

  bridge.registerHandler('web_method_1', function(data, responseCallback) {
    console.log("JS Echo called with:", data)
    responseCallback(data)
  })
  bridge.callHandler('rn_method_1', {'key':'value'}, function responseCallback(responseData) {
    console.log("JS received response:", responseData)
  })
})

Props

  1. protocolScheme

    Message transfer protocol Scheme

    Default:wvjbscheme Invalid on Android when using https or http

    Compatible with WebViewJavaScriptBridge project. It can be customized and needs to be modified synchronously with the web.

  2. bridgeName

    Bridge Name

    Default:WebViewJavascriptBridge

    Compatible with WebViewJavaScriptBridge project. It can be customized and needs to be modified synchronously with the web.

  3. callbacksName

    callbacks Name

    Default:WVJBCallbacks

    Compatible with WebViewJavaScriptBridge project. It can be customized and needs to be modified synchronously with the web.

    Example:

      //RN
      render() {
        return (
          <WebViewJavascriptBridge ref="bridge" 
                                   protocolScheme="rnapi" 
                                   bridgeName="rnbridge" 
                                   callbacksName="rncallbacks"
          />
        )
      } 
    
      //Web,set before require('webview-javascript-bridge-promised')
      window.WebViewJavascriptProtocolScheme = "rnapi"
      window.WebViewJavascriptBridgeName = "rnbridge"
      window.WebViewJavascriptCallbacksName = "rncallbacks"
  4. supportReactNativeApis

    Default : true

    React Native API has been implemented:

    Compoment:

    In addition:

    • Platform
    • DeviceEventEmitter

    When calling RN API in Web, it is the same as RN calling, RN API is window.ReactNativeWebView Called in this global variable.

      window.ReactNativeWebView.Alert.alert("title","message",[{
          text : "OK",
          onPress : () => {
            console.log('OK');
          }
        },{
          text : "Cancel",
          style : "cancel",
          onPress : () => {
            console.log('cancel');
          }
        }],{});

    Warning

    Calling RN API in Web needs to called after the page is loaded, Example:

    (~~Occasionally, there will be uninitialized situations on Android, which need to be solved. It is recommended to call RN API with the promised safely method.~~)

    useEffect(() => {
      window.ReactNativeWebView.Alert.alert()
    });
       
    componentDidMount() {
      window.ReactNativeWebView.Alert.alert()
    }

    If you want to call the RN API safely anywhere, use:

    var bridge = require('webview-javascript-bridge-promised')
    bridge.promised().then(() => {
      window.ReactNativeWebView.Alert.alert()
    })