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

@@ -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");
}