diff --git a/doric-android/app/build.gradle b/doric-android/app/build.gradle index 85e0701d..178be178 100644 --- a/doric-android/app/build.gradle +++ b/doric-android/app/build.gradle @@ -25,7 +25,7 @@ android { } afterEvaluate { - buildJSBundle.exec() + //buildJSBundle.exec() } task buildJSBundle(type: Exec) { diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 9539e5ed..143b4878 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -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 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); diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java index 2a6dcf50..53faaa95 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricWebViewJSExecutor.java @@ -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"); } diff --git a/doric-js/bundle/doric-web.js b/doric-js/bundle/doric-web.js new file mode 100644 index 00000000..a37ac372 --- /dev/null +++ b/doric-js/bundle/doric-web.js @@ -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"); diff --git a/doric-js/index.web.ts b/doric-js/index.web.ts new file mode 100644 index 00000000..b1ae8460 --- /dev/null +++ b/doric-js/index.web.ts @@ -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") \ No newline at end of file diff --git a/doric-js/lib/index.web.d.ts b/doric-js/lib/index.web.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/doric-js/lib/index.web.js b/doric-js/lib/index.web.js new file mode 100644 index 00000000..79827c64 --- /dev/null +++ b/doric-js/lib/index.web.js @@ -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"); diff --git a/doric-js/rollup.config.js b/doric-js/rollup.config.js index 7affb998..6a580f4e 100644 --- a/doric-js/rollup.config.js +++ b/doric-js/rollup.config.js @@ -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", }, -] \ No newline at end of file + 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); + }, + }, +];