mx-h5-jsbridge
Version:
支持H5和iOS/Android/Harmony进行双向通信,H5 和原生通讯只需一套代码
244 lines (230 loc) • 8.24 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["JSBridge"] = factory();
else
root["JSBridge"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// CONCATENATED MODULE: ./src/utils.js
function getBrowserInfo() {
const ua = window.navigator.userAgent.toLowerCase() || navigator.vendor.toLowerCase() || window.opera.toLowerCase();
const isAndroid = /Android/i.test(ua);
const isIOS = /iPhone|iPad|iPod/i.test(ua);
const isHarmonyOS = /HarmonyOs|OpenHarmony/i.test(ua) && !isAndroid;
return { isIOS, isAndroid, isHarmonyOS };
}
function isJSON(str) {
if (typeof str === 'string') {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
return false;
}
function getPlatform() {
return getBrowserInfo();
}
// CONCATENATED MODULE: ./src/jsBridge_ios.js
function connectJSBridge(callback) {
if (window.WebViewJavascriptBridge) return callback(window.WebViewJavascriptBridge);
if (window.WVJBCallbacks) return window.WVJBCallbacks.push(callback);
window.WVJBCallbacks = [callback];
const WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(() => {
document.documentElement.removeChild(WVJBIframe);
}, 0);
}
// CONCATENATED MODULE: ./src/jsBridge_android.js
function jsBridge_android_connectJSBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(window.WebViewJavascriptBridge);
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function () {
callback(window.WebViewJavascriptBridge);
}, false);
}
}
// CONCATENATED MODULE: ./src/jsBridge_harmony.js
function jsBridge_harmony_connectJSBridge(callback) {
if (window.WebViewJavascriptBridge) return callback(window.WebViewJavascriptBridge);
if (window.WVJBCallbacks) return window.WVJBCallbacks.push(callback);
window.WVJBCallbacks = [callback];
const WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'wvjbscheme://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(() => {
document.documentElement.removeChild(WVJBIframe);
}, 0);
}
// CONCATENATED MODULE: ./src/jsBridge.js
const { isAndroid, isIOS, isHarmonyOS } = getBrowserInfo();
/**
* 函数描述:js注册方法给app调用
*
* jsBridge.registerHandler(name, callback(data, appCallback))
* @param {String} name 方法名
* @param {Function} callback 回调函数
* @param {Any} callback.data app返回的数据
* @param {Function} callback.appCallback app返回的回调
* @return
*/
function registerHandler(name, callback) {
const connectBridge = bridge => {
bridge.registerHandler(name, (data, appCallback) => {
if (isJSON(data)) data = JSON.parse(data);
if (typeof callback === 'function') callback(data, appCallback);
});
bridge.registerHandler(name, callback);
};
if (isIOS) {
connectJSBridge(connectBridge);
} else if (isHarmonyOS) {
jsBridge_harmony_connectJSBridge(connectBridge);
} else if (isAndroid) {
jsBridge_android_connectJSBridge(connectBridge);
}
}
/**
* 函数描述:js调用app方法
*
* jsBridge.callHandler(name, params, callback)
* @param {String} name 方法名
* @param {Object} params 参数
* @param {Function} callback 回调函数
* @return
*/
function callHandler(name, params, callback = res => {}) {
const connectBridge = bridge => {
bridge.callHandler(name, params, data => {
if (isJSON(data)) data = JSON.parse(data);
if (typeof callback === 'function') callback(data);
});
};
if (isIOS) {
connectJSBridge(connectBridge);
} else if (isHarmonyOS) {
jsBridge_harmony_connectJSBridge(connectBridge);
} else if (isAndroid) {
jsBridge_android_connectJSBridge(connectBridge);
}
}
function onInit() {
if (isAndroid) {
jsBridge_android_connectJSBridge(bridge => {
bridge.init();
});
}
}
onInit();
const JsBridge = {
registerHandler,
callHandler,
getPlatform: getPlatform
};
window.JsBridge = JsBridge;
/* harmony default export */ var jsBridge = __webpack_exports__["default"] = (JsBridge);
/***/ })
/******/ ]);
});