1. Description:
[How to make JS and Java securely call each other in WebView] (http://www.pedant.cn/2014/07/04/webview-js-java-interface-research/) "Core JS full parsing
2. Core JS parsing is as follows, welcome to shoot Bricks!!!
javascript: (function (Win) { console.log ("hostapp initialization Begin "); //win. Hostapp Object var h = { // callback function Queue queue : [], After the //java executes, call this callback method and pass in an index. This callback method finds the corresponding callback method according to the index and goes to the callback function queue callback : function () { //get all the parameter arrays passed in Java var argArr = Array.prototype.slice.call (arguments, 0); console.log ("QUEQE:" + this.queue); console.log ("Argarr:" + argarr); //gets the first parameter--the index of the callback method var index = argarr.shift (); console.log ("index:" + index); //gets the second parameter-whether it is a permanent method (default is not permanent). When a non-permanent method is used, this method is removed after the callback method in the queue is exhausted. var isPerManent = Argarr.shift (); console.log (" Ispermanent: " + ispermanent); console.log ("Argarr:" + argarr); console.log ("Queue[index]:" + this.queue[index]); //using Apply,Passes the current context and parameters into the callback method //( Note that the first two parameters of the parameter array Argarr have been removed by shift, and the remaining parameters are returned for this callback method, so the callback method is passed in) this.queue[index].apply (This, argarr); //if the JS method is non-permanent, after this method is recalled, remove this method from the queue //(Note that the method is deleted, but only the index is deleted.) That is, if the index of this method is reserved elsewhere, then this method will not be recycled by GC if (!ispermanent) { delete this.queue[index] } } }; console.log ("QUEQE:" + this.queue); h.alert = h.alert = h.alert = h.delayjscallback = h.getimsi = h.getossdk = h.goback = h.overloadmethod = h.overloadmethod = h.passjson2java = h.passlongtype = h.retbackpassjson = h.retjavaobject = h.testlosstime = h.toast = h.toast = function () { //gets the array of all the arguments passed in when calling the method in the business var argArr = Array.prototype.slice.call (arguments, 0); // The first parameter defaults to the name of the method itself called //(although in business, there is no name for the method passed in this parameter, but in the following code, Using the Object.getownpropertynames method, //gets all the properties of Hostapp, ( When the property is a function type) and the property name as the first parameter, stitching to the front of the Business incoming parameter if ( ARGARR.LENGTH&NBSP;<&NBSP;1) { throw "hostapp call error, message:miss method Name " } // Array of types to be passed into the Java side var typeArr = []; //loops all the incoming parameters of the business, gets the type of each parameter, and deposits it into the type array. for (var h = 1; h < argarr.length; h++) { var arg = argarr[h]; var argType = typeof arg; typeArr[typeArr.length] = argType; //If an incoming parameter is found to be a callback function, the callback function is stored in Hostapp's callback function queue and the value of the parameter is replaced with the index of the callback function in the queue andnbsp; //(note that at this point, although the value of the parameter has only one index in the queue that is stored in the Argarr, However, the type of the parameter is still saved as a function type in Typearr if (argtype == "function") { var queueLength = H.queue.length; H.queue[queueLength] = arg; argarr[h] = queueLength } } //Pass in the method name, an array of parameter types, and all the arguments that are passed into the business. Perform a popup event to let the Java side capture. Gets the return value and translates to a JSON object. var promptresultobj = json.parse (Prompt (JSON.stringify ({ method : Argarr.shift (), types : typearr, args : argarr }))); //If the return code is not 200, Throw exception if (promptresultobj.code != 200) { throw "hostapp call Error, code: " + promptResultObj.code + ", message:" + promptresultobj.result } //returns the return value obtained from the Java side. //(note that when a callback method is passed in the business, in most cases, the return value is obtained through the callback method.) You can refer to the callback function queue and callback method on the Hostapp at this point) return promptresultobj.result }; // Use Getownpropertynames to get all the properties of the Hostapp object and loop. //Note that the first parameter of foreach is the array element (that is, the property name of Hostapp), the second argument is the array index, followed by all the elements in the array. object.getownpropertynames (H). ForEach ( function (attrname) { var attrObj = H[attrName]; if (typeof attrobj === " Function " && attrName !== " callback ") { //Here is an anonymous closure function, referenced by a property of Hostapp. //When this attribute persists in Hostapp, local variables attrname and attrobj can be permanently accessed by the closure function without being reclaimed by GC. //Obviously, the real function (all relevant properties on the Hostapp, such as Getimsi, GETOSSDK) has been put into a local variable attrobj that only the closure function can access. //in short, all the properties on the current Hostapp are just a closure function. However, this closure function can access the previous real property. h[attrname] = function () { return Attrobj.apply (h, [ attrname ] .concat (array.prototype.slice .call (arguments, 0))) } } }); win. Hostapp = h; console.log ("Hostapp initialization end")}) (window);
How to make JS and Java safe to call each other in WebView the core JS full parsing