webview页面和壳通信的库(精简版)
2015-06-25 15:22
441 查看
// PG精简版 (function() { var PG = { iosBridge: null, callbackId: 0, callbacks: [], commandQueue: [], commandQueueFlushing: false }, ua = navigator.userAgent, isIOS = (ua.indexOf("iPhone") > -1 || ua.indexOf("iPad") > -1 || ua.indexOf("iPod") > -1) ? true : false; PG.getAndClearQueuedCommands = function () { var commandQueue_json = JSON.stringify(PG.commandQueue); PG.commandQueue = []; return commandQueue_json; }; PG.exec = function(method, callback, args) { var callbackId = ''; if (typeof(callback) == "undefined") { callback = null; } if (typeof(args) == "undefined") { args = {}; } if (callback && typeof(callback) == 'function') { callbackId = method + PG.callbackId++; PG.callbacks[callbackId] = callback; } var obj = { Method: method, CallbackId: callbackId, Args: args }; if (isIOS) { if (PG.iosBridge == null) { PG.iosBridge = document.createElement("iframe"); PG.iosBridge.setAttribute("style", "display:none;"); PG.iosBridge.setAttribute("height", "0px"); PG.iosBridge.setAttribute("width", "0px"); PG.iosBridge.setAttribute("frameborder", "0"); document.documentElement.appendChild(PG.iosBridge); } PG.commandQueue.push(JSON.stringify(obj)); if (!PG.commandQueueFlushing) { PG.iosBridge.src = 'pg://ready'; } } else if (window.comjs) { // android window.comjs.notify('pg://' + encodeURIComponent(JSON.stringify(obj))); } else { console.log("非ios或android平台,不合适吧"); } }; PG.callback = function(callbackId, args) { if (PG.callbacks[callbackId]) { try { var temp = decodeURIComponent(args), obj = JSON.parse(temp); PG.callbacks[callbackId](obj); } catch (e) { console.log("Error in success callback: " + callbackId + " = " + e); } delete PG.callbacks[callbackId]; } }; if (typeof(window) === "object" && typeof(window.document) === "object") { window.PG = PG; } })();
相关文章推荐
- iOS开发之AsyncSocket使用教程
- android学习之路(四)----RenderScript
- You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Cocos2dx------详细介绍如何编写扫雷这个游戏含源码(一)
- vs 2015 编译cocos2dx 报错
- Android横竖屏切换小结
- cocos js LabelBMFont Bitmap Font Generator
- Error:Execution failed for task ':app:compileDebugAidl'. > Executor Singleton not started
- 破解Revealapp的试用时间限制
- Firefly 3288重新制作android和lubuntu双系统固件
- android AndroidManifest.xml 多个android.intent.action.MAIN (
- Android SQLiteStatement 编译、执行 分析
- Android的Library工程
- IOS 设置View背景色
- android 加载图片oom的好的解决方法
- Android 中使用自定义字体的方法
- mono for Android 问题集合
- android 4.4 强制所有应用横屏显示
- iOS 中删除cookie的正确做法
- [Android Studio] 取消引用库打包出现异常-- provided dependencies can only be jars