Android:use webview to execute js

This commit is contained in:
pengfei.zhou
2021-11-05 15:14:32 +08:00
committed by osborn
parent 196497f3bd
commit bb17b74b99
7 changed files with 367 additions and 42 deletions

View File

@@ -1,2 +1,128 @@
'use strict';
"use strict";
function _binaryValue(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 _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 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));
});
}
function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try {
const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs);
args.forEach(e => {
NativeClient.log(`Arg:${e},${typeof e}`);
});
const rawArgs = args.map(e => _rawValue(e));
rawArgs.forEach(e => {
NativeClient.log(`RawArg:${e},${typeof e}`);
});
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret));
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal);
}
catch (e) {
NativeClient.log(`error:${e},${e.stack}`);
NativeClient.returnNative("");
}
}

View File

@@ -1,4 +1,6 @@
declare module NativeClient {
function log(message: string): void
function returnNative(ret: string): void
function callNative(name: string, args: string): string
}
@@ -8,7 +10,43 @@ type WrappedValue = {
type: "number" | "string" | "boolean" | "object" | "array" | "null",
value: RawValue,
}
function _binaryValue(v: RawValue) {
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 _wrappedValue(v: RawValue): WrappedValue {
switch (typeof v) {
case "number":
@@ -49,20 +87,23 @@ function _wrappedValue(v: RawValue): WrappedValue {
function _rawValue(v: WrappedValue): RawValue {
switch (v.type) {
case "number":
return v.value
return v.value;
case "string":
return v.value
return v.value;
case "boolean":
return v.value
return v.value;
case "object":
case "array":
return JSON.stringify(v.value)
if (typeof v.value === 'string') {
return JSON.parse(v.value)
}
return v.value;
default:
return undefined
return undefined;
}
}
function __injectGlobalObject(name: string, args: string) {
function _injectGlobalObject(name: string, args: string) {
Reflect.set(window, name, JSON.parse(args));
}
@@ -73,6 +114,23 @@ function __injectGlobalFunction(name: string) {
args.push(_wrappedValue(arguments[i]));
}
const ret = NativeClient.callNative(name, JSON.stringify(args));
return _rawValue(JSON.parse(ret))
return _rawValue(JSON.parse(ret));
});
}
function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`)
try {
const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs) as WrappedValue[];
const rawArgs = args.map(e => _rawValue(e));
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret))
NativeClient.log(`return:${returnVal}`)
NativeClient.returnNative(returnVal)
} catch (e) {
NativeClient.log(`error:${e},${(e as any).stack}`)
NativeClient.returnNative("")
}
}

View File

@@ -1,4 +1,6 @@
declare module NativeClient {
function log(message: string): void;
function returnNative(ret: string): void;
function callNative(name: string, args: string): string;
}
declare type RawValue = number | string | boolean | object | undefined;
@@ -6,7 +8,21 @@ declare type WrappedValue = {
type: "number" | "string" | "boolean" | "object" | "array" | "null";
value: RawValue;
};
declare function _binaryValue(v: RawValue): {
type: string;
value: number;
} | {
type: string;
value: string;
} | {
type: string;
value: boolean;
} | {
type: string;
value: undefined;
};
declare function _wrappedValue(v: RawValue): WrappedValue;
declare function _rawValue(v: WrappedValue): RawValue;
declare function __injectGlobalObject(name: string, args: string): void;
declare function _injectGlobalObject(name: string, args: string): void;
declare function __injectGlobalFunction(name: string): void;
declare function __invokeMethod(objectName: string, functionName: string, stringifiedArgs: string): void;

View File

@@ -1,4 +1,41 @@
"use strict";
function _binaryValue(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 _wrappedValue(v) {
switch (typeof v) {
case "number":
@@ -46,12 +83,12 @@ function _rawValue(v) {
return v.value;
case "object":
case "array":
return JSON.stringify(v.value);
return v.value;
default:
return undefined;
}
}
function __injectGlobalObject(name, args) {
function _injectGlobalObject(name, args) {
Reflect.set(window, name, JSON.parse(args));
}
function __injectGlobalFunction(name) {
@@ -64,3 +101,26 @@ function __injectGlobalFunction(name) {
return _rawValue(JSON.parse(ret));
});
}
function __invokeMethod(objectName, functionName, stringifiedArgs) {
NativeClient.log(`invoke:${objectName}.${functionName}(${stringifiedArgs})`);
try {
const thisObject = Reflect.get(window, objectName);
const thisFunction = Reflect.get(thisObject, functionName);
const args = JSON.parse(stringifiedArgs);
args.forEach(e => {
NativeClient.log(`Arg:${e},${typeof e}`);
});
const rawArgs = args.map(e => _rawValue(e));
rawArgs.forEach(e => {
NativeClient.log(`RawArg:${e},${typeof e}`);
});
const ret = Reflect.apply(thisFunction, thisObject, rawArgs);
const returnVal = JSON.stringify(_wrappedValue(ret));
NativeClient.log(`return:${returnVal}`);
NativeClient.returnNative(returnVal);
}
catch (e) {
NativeClient.log(`error:${e},${e.stack}`);
NativeClient.returnNative("");
}
}

View File

@@ -61,6 +61,7 @@ export default [
}
console.warn(warning.message);
},
treeshake: false,
},
{
input: "lib-es5/index.runtime.es5.js",