Add webview init

This commit is contained in:
pengfei.zhou 2021-11-04 18:50:32 +08:00 committed by osborn
parent e8089e98a8
commit 780188e145
8 changed files with 186 additions and 84 deletions

View File

@ -25,7 +25,7 @@ android {
}
afterEvaluate {
buildJSBundle.exec()
//buildJSBundle.exec()
}
task buildJSBundle(type: Exec) {

View File

@ -96,7 +96,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
}
protected void initJSEngine() {
mDoricJSE = new DoricNativeJSExecutor();
mDoricJSE = new DoricWebViewJSExecutor(Doric.application());
}
public void setEnvironmentValue(Map<String, Object> values) {
@ -239,6 +239,9 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
private void initDoricRuntime() {
try {
if (mDoricJSE instanceof DoricWebViewJSExecutor) {
loadBuiltinJS("doric-web.js");
}
loadBuiltinJS(DoricConstant.DORIC_BUNDLE_SANDBOX);
String libName = DoricConstant.DORIC_MODULE_LIB;
String libJS = DoricUtils.readAssetFile(DoricConstant.DORIC_BUNDLE_LIB);

View File

@ -17,7 +17,11 @@ package pub.doric.engine;
import android.annotation.SuppressLint;
import android.content.Context;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.github.pengfeizhou.jscore.JSDecoder;
@ -25,6 +29,8 @@ import com.github.pengfeizhou.jscore.JSRuntimeException;
import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue;
import pub.doric.utils.DoricLog;
/**
* @Description: This contains a webView which is used for executing JavaScript
@ -41,10 +47,34 @@ public class DoricWebViewJSExecutor implements IDoricJSE {
}
}
@SuppressLint("JavascriptInterface")
private static class DoricWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
ConsoleMessage.MessageLevel messageLevel = consoleMessage.messageLevel();
if (messageLevel == ConsoleMessage.MessageLevel.ERROR) {
DoricLog.e(consoleMessage.message());
} else if (messageLevel == ConsoleMessage.MessageLevel.WARNING) {
DoricLog.w(consoleMessage.message());
} else {
DoricLog.d(consoleMessage.message());
}
return true;
}
}
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
public DoricWebViewJSExecutor(Context context) {
this.webView = new WebView(context.getApplicationContext());
this.webView.loadUrl("about:blank");
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\")");
WebViewCallback webViewCallback = new WebViewCallback();
this.webView.addJavascriptInterface(webViewCallback, "callNative");
}

View File

@ -0,0 +1,18 @@
'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");

16
doric-js/index.web.ts Normal file
View File

@ -0,0 +1,16 @@
/*
* 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")

0
doric-js/lib/index.web.d.ts vendored Normal file
View File

17
doric-js/lib/index.web.js Normal file
View File

@ -0,0 +1,17 @@
"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,87 +1,105 @@
import resolve from '@rollup/plugin-node-resolve'
import buble from '@rollup/plugin-buble';
import commonjs from '@rollup/plugin-commonjs'
import resolve from "@rollup/plugin-node-resolve";
import buble from "@rollup/plugin-buble";
import commonjs from "@rollup/plugin-commonjs";
export default [
{
input: "lib/index.runtime.js",
output: {
name: "doric",
format: "iife",
file: "bundle/doric-sandbox.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
console.warn(warning.message);
}
{
input: "lib/index.runtime.js",
output: {
name: "doric",
format: "iife",
file: "bundle/doric-sandbox.js",
},
{
input: "lib/index.js",
output: {
format: "cjs",
file: "bundle/doric-lib.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
console.warn(warning.message);
}
plugins: [resolve({ mainFields: ["jsnext"] })],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
{
input: "lib/index.debug.js",
output: {
format: "cjs",
file: "bundle/doric-vm.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
],
external: ['ws'],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
console.warn(warning.message);
}
},
{
input: "lib/index.js",
output: {
format: "cjs",
file: "bundle/doric-lib.js",
},
{
input: "lib-es5/index.runtime.es5.js",
output: {
name: "doric",
format: "iife",
file: "bundle/doric-sandbox.es5.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
commonjs(),
buble({
transforms: { dangerousForOf: true }
}),
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
console.warn(warning.message);
}
plugins: [resolve({ mainFields: ["jsnext"] })],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
{
input: "lib-es5/index.js",
output: {
format: "cjs",
file: "bundle/doric-lib.es5.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
buble({
transforms: { dangerousForOf: true }
}),
],
onwarn: function (warning) {
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
console.warn(warning.message);
}
},
{
input: "lib/index.debug.js",
output: {
format: "cjs",
file: "bundle/doric-vm.js",
},
]
plugins: [resolve({ mainFields: ["jsnext"] })],
external: ["ws"],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
},
{
input: "lib/index.web.js",
output: {
format: "cjs",
file: "bundle/doric-web.js",
},
plugins: [resolve({ mainFields: ["jsnext"] })],
external: ["ws"],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
},
{
input: "lib-es5/index.runtime.es5.js",
output: {
name: "doric",
format: "iife",
file: "bundle/doric-sandbox.es5.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
commonjs(),
buble({
transforms: { dangerousForOf: true },
}),
],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
},
{
input: "lib-es5/index.js",
output: {
format: "cjs",
file: "bundle/doric-lib.es5.js",
},
plugins: [
resolve({ mainFields: ["jsnext"] }),
buble({
transforms: { dangerousForOf: true },
}),
],
onwarn: function (warning) {
if (warning.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(warning.message);
},
},
];