1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| (function(){ var isAndroid = /Android/.test(navigator.userAgent); var callbackMethodName = null;
var cmdMap = { causeReply: "REPLY", replyStatus: "REPLY_RESULT", sendComment: "SEND" };
var cmdPool = { "deviceBridge.device.causeReply": function (action, params) { doRequest(action, params, deviceBridge.call.sendComment); }, "deviceBridge.call.sendComment": function (action, params) { var args = JSON.parse(params); if (!args.actionID) { args.actionID = getReqID(); } try { window['send_reply'](args); } catch(e) {} }, "deviceBridge.result.replyStatus": function (action, params) { doRequest(action, params); } }; function getReqID () { return '_bd_' + new Date().getTime(); } function initCallback (callback, nReqID) { callbackMethodName = 'callbackjs' + nReqID; window[callbackMethodName] = function(res){ window[callbackMethodName] = null; callback(res); } } function sendRequest (action, nReqID, args, callbackMethodName) { var reqData = { action: action, actionID: nReqID, args: args }; if (isAndroid) { if (callbackMethodName != null) { prompt(JSON.stringify(reqData), "javascript:" + callbackMethodName + "('%1$s')"); } else { prompt(JSON.stringify(reqData)); } } else { if (callbackMethodName != null) { reqData.callback = callbackMethodName; } window.location = "chunmiao://#/" + JSON.stringify(reqData); } } function doRequest (action, args, callback) { var nReqID = getReqID(); if (callback) { initCallback(callback, nReqID); sendRequest(action, nReqID, args, callbackMethodName); } else { sendRequest(action, nReqID, args); } }
function ns (namespace, owner) { var names = namespace.split("."); owner = owner || window; for (var i = 0; i < names.length; i++) { var packageName = names[i]; owner[packageName] = owner[packageName] || {}; owner = owner[packageName]; } return owner; }
function currying (fn, cmd) { return function () { var args = [cmd]; args.push.apply(args, arguments); return fn.apply(this, args); } }
function bindFn (cmdStr, fn) { var parts = cmdStr.split("."); var method = parts.pop(); var prefix = parts.join("."); var namespace = ns(prefix); namespace[method] = currying(fn, cmdMap[method]); }
function buildInterface () { for (var key in cmdPool) { bindFn(key, cmdPool[key]); } }
buildInterface(); })();
|