jslib-tools
Version:
js工具库 封装常用的工具函数 如深拷贝 时间转换日期格式化、浏览器判断等,提高开发效率
223 lines (195 loc) • 9.63 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: plugins/jsBridge/index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: plugins/jsBridge/index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* @Author: zhangyu
* @Email: zhangdulin@outlook.com
* @Date: 2021-06-11 11:00:48
* @LastEditors: zhangyu
* @LastEditTime: 2021-06-16 18:07:04
* @Description:
*/
//JS注册事件监听
var initflag = false
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener(
'WebViewJavascriptBridgeReady'
, function() {
callback(WebViewJavascriptBridge)
},
false
);
}
}
//注册回调函数,第一次连接时调用 初始化函数
// connectWebViewJavascriptBridge(function(bridge) {
// //初始化
// bridge.init(function(message, responseCallback) {
// initflag = true
// var data = {
// 'Javascript Responds': 'Wee!'
// };
// // alert("jasdashjd");
// responseCallback(data);
// });
// })
/**
* @description: jsbridge callHandler
* @param {*} callHandlerData
* @param {*} name n请求的方法
* @param {*} data 请求时传的参数
* @return {*} function
*/
export function callHandler (callHandlerData={name:'', data:{}}) {
let _resolve
let _reject
// 生成promise对象,保留resolve,reject
const readyPromise = new Promise((resolve, reject) => {
_resolve = resolve
_reject = reject
})
connectWebViewJavascriptBridge(function(bridge) {
//初始化
if (!initflag) {
bridge.init(function(message, responseCallback) {
initflag = true
var data = {
'Javascript Responds': 'Wee!'
};
responseCallback(data);
});
try {
bridge.callHandler(callHandlerData.name, callHandlerData.data, (response) => {
// 调用成功,使用保留的resolve改变返回的promise状态
_resolve(response)
})
} catch (e) {
// 调用成功,使用保留的reject改变返回的promise状态
_reject(e)
}
} else {
try {
bridge.callHandler(callHandlerData.name, callHandlerData.data, (response) => {
// 调用成功,使用保留的resolve改变返回的promise状态
_resolve(response)
})
} catch (e) {
// 调用成功,使用保留的reject改变返回的promise状态
_reject(e)
}
}
})
return readyPromise
}
/*
* @Author: zhangyu
* @Email: zhangdulin@outlook.com
* @Date: 2021-06-11 11:00:48
* @LastEditors: zhangyu
* @LastEditTime: 2021-06-11 14:05:50
* @Description:
*/
// 注册方法获取WebViewJavascriptBridge对象
function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
return callback(window.WebViewJavascriptBridge)
}
if (window.WVJBCallbacks) {
return window.WVJBCallbacks.push(callback)
}
// 兼容安卓
document.addEventListener(
'WebViewJavascriptBridgeReady',
() => callback(window.WebViewJavascriptBridge),
false
)
// 兼容ios
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)
}
// 获取WebViewJavascriptBridge对象
function getBirdge() {
return new Promise(resolve => {
if (window.WebViewJavascriptBridge) {
return resolve(window.WebViewJavascriptBridge)
}
setupWebViewJavascriptBridge(bridge => {
// 初始化
bridge.init(function(message, responseCallback) {
var data = {
'Javascript Responds': 'Wee!'
};
responseCallback(data);
});
resolve(bridge)
})
})
}
// 重写之后的birdge
/**
* @description: jsbridge callHandler
* @param {*} callHandlerData
* @param {*} name n请求的方法
* @param {*} data 请求时传的参数
* @return {*} function
*/
export default {
ctx: null, // WebViewJavascriptBridge
callHandler(callHandlerData={name:'', data:{}}) {
return new Promise(async resolve => {
if (!this.ctx) {
this.ctx = await getBirdge()
}
this.ctx.callHandler(callHandlerData.name, callHandlerData.data, res => resolve(res))
})
},
registerHandler(jsHandlerName) {
// 重写事件注册方法
this.ctx.registerHandler(jsHandlerName, (data, callback) => {
if (callback) {
callback(
`返回一个字符串,告诉ObjC:我已收到数据${JSON.stringify(data)}`
)
}
})
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#alias">alias</a></li><li><a href="global.html#arrayMove">arrayMove</a></li><li><a href="global.html#arrayMoveMutate">arrayMoveMutate</a></li><li><a href="global.html#arrayToObject">arrayToObject</a></li><li><a href="global.html#arrayToTree">arrayToTree</a></li><li><a href="global.html#arrFibonacci">arrFibonacci</a></li><li><a href="global.html#arrVals">arrVals</a></li><li><a href="global.html#callHandler">callHandler</a></li><li><a href="global.html#camelize">camelize</a></li><li><a href="global.html#checkInt">checkInt</a></li><li><a href="global.html#checkNum">checkNum</a></li><li><a href="global.html#clipboardObj">clipboardObj</a></li><li><a href="global.html#compareSize">compareSize</a></li><li><a href="global.html#cookie">cookie</a></li><li><a href="global.html#cursortPosition">cursortPosition</a></li><li><a href="global.html#dasherize">dasherize</a></li><li><a href="global.html#dateFormat1">dateFormat1</a></li><li><a href="global.html#dateFormat2">dateFormat2</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#decode_url_param">decode_url_param</a></li><li><a href="global.html#def">def</a></li><li><a href="global.html#elDateFormat">elDateFormat</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#exportXls">exportXls</a></li><li><a href="global.html#extend">extend</a></li><li><a href="global.html#getBrowserModel">getBrowserModel</a></li><li><a href="global.html#getChangedData">getChangedData</a></li><li><a href="global.html#getChildren">getChildren</a></li><li><a href="global.html#getCookie">getCookie</a></li><li><a href="global.html#getDefaultAvatar">getDefaultAvatar</a></li><li><a href="global.html#getDeviceModel">getDeviceModel</a></li><li><a href="global.html#getHttpBase64">getHttpBase64</a></li><li><a href="global.html#getImgBase64">getImgBase64</a></li><li><a href="global.html#getThumbnails">getThumbnails</a></li><li><a href="global.html#getType">getType</a></li><li><a href="global.html#handleEmoji">handleEmoji</a></li><li><a href="global.html#handleParam">handleParam</a></li><li><a href="global.html#handleText">handleText</a></li><li><a href="global.html#isArray">isArray</a></li><li><a href="global.html#isArrayLike">isArrayLike</a></li><li><a href="global.html#isDiff">isDiff</a></li><li><a href="global.html#isFunc">isFunc</a></li><li><a href="global.html#isInt">isInt</a></li><li><a href="global.html#isNum">isNum</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isObjEmpty">isObjEmpty</a></li><li><a href="global.html#isUInt">isUInt</a></li><li><a href="global.html#isUNum">isUNum</a></li><li><a href="global.html#makeArray">makeArray</a></li><li><a href="global.html#objectConversionToList">objectConversionToList</a></li><li><a href="global.html#objectToArray">objectToArray</a></li><li><a href="global.html#objTools">objTools</a></li><li><a href="global.html#parseTime">parseTime</a></li><li><a href="global.html#setVideoPlay">setVideoPlay</a></li><li><a href="global.html#sleepAction">sleepAction</a></li><li><a href="global.html#sleepSync">sleepSync</a></li><li><a href="global.html#sortMapByKey">sortMapByKey</a></li><li><a href="global.html#storage">storage</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#titleize">titleize</a></li><li><a href="global.html#toArray">toArray</a></li><li><a href="global.html#toLower">toLower</a></li><li><a href="global.html#toMap">toMap</a></li><li><a href="global.html#toObjectMap">toObjectMap</a></li><li><a href="global.html#toSimplifiedChinese">toSimplifiedChinese</a></li><li><a href="global.html#toUpper">toUpper</a></li><li><a href="global.html#unescapeHTML">unescapeHTML</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Wed Aug 25 2021 17:57:45 GMT+0800 (GMT+08:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>