webview页面和壳通信的库(精简版)
// 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; } })();
合乎自然而生生不息。。。