UNPKG

@dead_fish/jsbridge

Version:

A lightweight and easy-to-use jsBridge for Android and IOS platforms.

2 lines (1 loc) 2.44 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e={parseUrl:function(e){var n=window.location.href.split("?").slice(1),t={};return n.forEach((function(e){var n=e.indexOf("/");(-1!==n?e.substring(0,n):e).split("&").forEach((function(e){var n=e.split("="),i=n[0],r=n[1],o=void 0===r||r;if(t.hasOwnProperty(i)){var a=t[i];Array.isArray(a)?a.push(o):t[i]=[o]}else t[i]=o}))})),void 0!==e?t[e]||void 0:t}};function n(e){return"[object Function]"===Object.prototype.toString.call(e)}function t(){return/Android/i.test(window.navigator.userAgent)}function i(){return/iPhone|iPad|iPod/i.test(window.navigator.userAgent)}function r(){return/HarmonyOS/i.test(window.navigator.userAgent)||/ohos/i.test(window.navigator.userAgent)||/arkos/i.test(window.navigator.userAgent)||/arkcompiler/i.test(window.navigator.userAgent)||/arkui/i.test(window.navigator.userAgent)}function o(e){if("string"!=typeof e)return!1;try{return JSON.parse(e),!0}catch(e){return!1}}function a(e){if(window.WebViewJavascriptBridge)return e(window.WebViewJavascriptBridge);if(window.WVJBCallbacks)return window.WVJBCallbacks.push(e);window.WVJBCallbacks=[e];var n=document.createElement("iframe");n.style.display="none",n.src="wvjbscheme://__BRIDGE_LOADED__",document.documentElement.appendChild(n),setTimeout((function(){document.documentElement.removeChild(n)}),0)}function s(e){if(window.WebViewJavascriptBridge)return e(window.WebViewJavascriptBridge);document.addEventListener("WebViewJavascriptBridgeReady",(function(){return e(window.WebViewJavascriptBridge)}),!1)}!function(){if(!t())return!1;s((function(e){var n;return null===(n=null==e?void 0:e.init)||void 0===n?void 0:n.call(e)}))}();var u={registerHandler:function(e){var n=e.eventName,r=e.handler,u=function(e){e.registerHandler(n,(function(e,n){o(e)&&(e=JSON.parse(e)),"function"==typeof r&&r(e,n)})),"function"!=typeof r&&e.registerHandler(n,r)};t()&&s(u),i()&&a(u)},callHandler:function(e){var u=e.eventName,d=e.handler,c=e.data,l=void 0===c?"":c,f=function(e){e.callHandler(u,l,(function(e){o(e)&&(e=JSON.parse(e)),"function"==typeof d&&d(e)}))};if(t()&&s(f),i()&&a(f),r()){var w=u.split("."),v=w.slice(0,w.length-1),p=w[w.length-1],g=window;v.forEach((function(e){return g=g[e]})),p?n(g)&&g(p,l,d):n(g)&&g(l,d)}}},d={jsBridge:u,utils:e,isAndroid:t,isHarmonyOS:r,isIOS:i};exports.default=d,exports.isAndroid=t,exports.isHarmonyOS=r,exports.isIOS=i,exports.jsBridge=u,exports.utils=e;