android:webview add some api

This commit is contained in:
pengfei.zhou 2021-11-05 11:59:54 +08:00 committed by osborn
parent 780188e145
commit 196497f3bd
8 changed files with 369 additions and 57 deletions

View File

@ -25,10 +25,18 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSRuntimeException;
import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import pub.doric.utils.DoricLog;
@ -39,11 +47,72 @@ import pub.doric.utils.DoricLog;
*/
public class DoricWebViewJSExecutor implements IDoricJSE {
private final WebView webView;
private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
private static Object unwrapJSObject(JSONObject jsonObject) {
String type = jsonObject.optString("type");
switch (type) {
case "number":
return jsonObject.optDouble("value");
case "string":
return jsonObject.optString("value");
case "boolean":
return jsonObject.optBoolean("value");
case "object":
return jsonObject.optJSONObject("value");
case "array":
return jsonObject.optJSONArray("value");
default:
return JSONObject.NULL;
}
}
private static final String WRAPPED_NULL = new JSONBuilder().put("type", "null").toString();
public class WebViewCallback {
@JavascriptInterface
public void callNative(int command, String arguments) {
public String callNative(String name, String arguments) {
JavaFunction javaFunction = globalFunctions.get(name);
if (javaFunction == null) {
return WRAPPED_NULL;
}
try {
JSONArray jsonArray = new JSONArray(arguments);
int length = jsonArray.length();
JSDecoder[] decoders = new JSDecoder[length];
for (int i = 0; i < length; i++) {
JSONObject jsonObject = jsonArray.optJSONObject(i);
Object object = unwrapJSObject(jsonObject);
decoders[i] = new JavaJSDecoder(object);
}
JavaValue javaValue = javaFunction.exec(decoders);
if (javaValue.getType() == 0) {
return WRAPPED_NULL;
}
if (javaValue.getType() == 1) {
Double value = Double.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "number").put("value", value).toString();
}
if (javaValue.getType() == 2) {
Boolean value = Boolean.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "boolean").put("value", value).toString();
}
if (javaValue.getType() == 3) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "string").put("value", value).toString();
}
if (javaValue.getType() == 4) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "object").put("value", value).toString();
}
if (javaValue.getType() == 5) {
String value = String.valueOf(javaValue.getValue());
return new JSONBuilder().put("type", "array").put("value", value).toString();
}
} catch (JSONException e) {
e.printStackTrace();
}
return WRAPPED_NULL;
}
}
@ -73,16 +142,16 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
WebSettings webSettings = this.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
this.webView.setWebChromeClient(new DoricWebChromeClient());
this.webView.loadUrl("https://m.baidu.com");
this.webView.loadUrl("javascript:alert(\"11111\")");
this.webView.loadUrl("about:blank");
WebViewCallback webViewCallback = new WebViewCallback();
this.webView.addJavascriptInterface(webViewCallback, "callNative");
this.webView.addJavascriptInterface(webViewCallback, "NativeClient");
WebView.setWebContentsDebuggingEnabled(true);
}
@Override
public String loadJS(String script, String source) {
this.webView.evaluateJavascript(script, null);
return script;
return null;
}
@Override
@ -93,7 +162,7 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
@Override
public void injectGlobalJSFunction(String name, JavaFunction javaFunction) {
globalFunctions.put(name, javaFunction);
}
@Override

View File

@ -0,0 +1,132 @@
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.engine;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSBoolean;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSNumber;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSString;
import com.github.pengfeizhou.jscore.JSValue;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Iterator;
/**
* @Description: This is for java runtime.
* @Author: pengfei.zhou
* @CreateDate: 2021/11/5
*/
public class JavaJSDecoder extends JSDecoder {
private final Object value;
private static final JSNull JS_NULL = new JSNull();
public JavaJSDecoder(Object object) {
super(null);
this.value = object;
}
@Override
public boolean readBool() throws ArchiveException {
return (boolean) this.value;
}
@Override
public String readString() throws ArchiveException {
return (String) this.value;
}
@Override
public Double readNumber() {
return (Double) this.value;
}
@Override
public boolean isBool() {
return this.value instanceof Boolean;
}
@Override
public boolean isNULL() {
return this.value == JSONObject.NULL;
}
@Override
public boolean isString() {
return this.value instanceof String;
}
@Override
public boolean isNumber() {
return this.value instanceof Number;
}
@Override
public boolean isArray() {
return this.value instanceof JSONArray;
}
@Override
public boolean isObject() {
return this.value instanceof JSONObject;
}
@Override
public JSValue decode() throws ArchiveException {
if (isBool()) {
return new JSBoolean((Boolean) this.value);
}
if (isString()) {
return new JSString((String) this.value);
}
if (isNumber()) {
return new JSNumber(((Number) this.value).doubleValue());
}
if (isObject()) {
JSONObject jsonObject = (JSONObject) this.value;
Iterator<String> it = jsonObject.keys();
JSObject jsObject = new JSObject();
while (it.hasNext()) {
String key = it.next();
Object o = jsonObject.opt(key);
JavaJSDecoder jsDecoder = new JavaJSDecoder(o);
JSValue jsValue = jsDecoder.decode();
jsObject.setProperty(key, jsValue);
}
return jsObject;
}
if (isArray()) {
JSONArray jsonArray = (JSONArray) this.value;
int length = jsonArray.length();
JSArray jsArray = new JSArray(length);
for (int i = 0; i < length; i++) {
Object o = jsonArray.opt(i);
JavaJSDecoder jsDecoder = new JavaJSDecoder(o);
JSValue jsValue = jsDecoder.decode();
jsArray.put(i, jsValue);
}
return jsArray;
}
return JS_NULL;
}
}

View File

@ -1,18 +1,2 @@
'use strict';
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
console.log("Hello,WebView");

View File

@ -1,16 +1,78 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
console.log("Hello,WebView")
declare module NativeClient {
function callNative(name: string, args: string): string
}
type RawValue = number | string | boolean | object | undefined
type WrappedValue = {
type: "number" | "string" | "boolean" | "object" | "array" | "null",
value: RawValue,
}
function _wrappedValue(v: RawValue): WrappedValue {
switch (typeof v) {
case "number":
return {
type: "number",
value: v
};
case "string":
return {
type: "string",
value: v
};
case "boolean":
return {
type: "boolean",
value: v
};
case "object":
if (v instanceof Array) {
return {
type: "array",
value: JSON.stringify(v)
};
} else {
return {
type: "object",
value: JSON.stringify(v)
};
}
default:
return {
type: "null",
value: undefined
};
}
}
function _rawValue(v: WrappedValue): RawValue {
switch (v.type) {
case "number":
return v.value
case "string":
return v.value
case "boolean":
return v.value
case "object":
case "array":
return JSON.stringify(v.value)
default:
return undefined
}
}
function __injectGlobalObject(name: string, args: string) {
Reflect.set(window, name, JSON.parse(args));
}
function __injectGlobalFunction(name: string) {
Reflect.set(window, name, function () {
const args: any[] = [];
for (let i = 0; i < arguments.length; i++) {
args.push(_wrappedValue(arguments[i]));
}
const ret = NativeClient.callNative(name, JSON.stringify(args));
return _rawValue(JSON.parse(ret))
});
}

View File

@ -0,0 +1,12 @@
declare module NativeClient {
function callNative(name: string, args: string): string;
}
declare type RawValue = number | string | boolean | object | undefined;
declare type WrappedValue = {
type: "number" | "string" | "boolean" | "object" | "array" | "null";
value: RawValue;
};
declare function _wrappedValue(v: RawValue): WrappedValue;
declare function _rawValue(v: WrappedValue): RawValue;
declare function __injectGlobalObject(name: string, args: string): void;
declare function __injectGlobalFunction(name: string): void;

View File

@ -1,17 +1,66 @@
"use strict";
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
console.log("Hello,WebView");
function _wrappedValue(v) {
switch (typeof v) {
case "number":
return {
type: "number",
value: v
};
case "string":
return {
type: "string",
value: v
};
case "boolean":
return {
type: "boolean",
value: v
};
case "object":
if (v instanceof Array) {
return {
type: "array",
value: JSON.stringify(v)
};
}
else {
return {
type: "object",
value: JSON.stringify(v)
};
}
default:
return {
type: "null",
value: undefined
};
}
}
function _rawValue(v) {
switch (v.type) {
case "number":
return v.value;
case "string":
return v.value;
case "boolean":
return v.value;
case "object":
case "array":
return JSON.stringify(v.value);
default:
return undefined;
}
}
function __injectGlobalObject(name, args) {
Reflect.set(window, name, JSON.parse(args));
}
function __injectGlobalFunction(name) {
Reflect.set(window, name, function () {
const args = [];
for (let i = 0; i < arguments.length; i++) {
args.push(_wrappedValue(arguments[i]));
}
const ret = NativeClient.callNative(name, JSON.stringify(args));
return _rawValue(JSON.parse(ret));
});
}

View File

@ -5,7 +5,9 @@
// "incremental": true, /* Enable incremental compilation */
"target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [], /* Specify library files to be included in the compilation. */
"lib": [
"DOM"
], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */

View File

@ -5,7 +5,9 @@
// "incremental": true, /* Enable incremental compilation */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [], /* Specify library files to be included in the compilation. */
"lib": [
"DOM"
], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */