Non‑Intrusive JavaScript Injection in Android WebView for DoKit One‑Machine‑Multi‑Control
DoKit achieves non‑intrusive first‑line JavaScript injection for Android WebView by intercepting HTML requests, parsing with Jsoup to prepend a script tag, and employing ASM bytecode hooks to wrap WebViewClient, ensuring the OMMC feature reliably controls multiple devices on H5 pages.
Since its open‑source launch in 2018, DoKit has evolved into a mature ecosystem covering six platforms (Android, iOS, Web, Mini‑Program, Flutter, PC). Within Didi, DoKit is used in all business lines to improve development and testing efficiency. In 2023 the project focuses on test‑efficiency features such as UI automation and one‑machine‑multi‑control (OMMC).
The OMMC feature allows a single phone to control multiple devices simultaneously, which greatly improves test efficiency for quality teams. However, when H5 pages are involved, the OMMC code must run before the page’s own scripts, otherwise the control flow is broken. This article shares the technical challenges and solutions for achieving first‑line execution of injected JavaScript in Android WebView.
1. Problems Encountered by OMMC
• Need for temporary debugging tools during development. • Numerous entry points for development tools make it hard to locate required functions. • UI designers need to verify UI against design drafts, which requires cumbersome steps.
DoKit addresses these issues by providing over 30 built‑in tools in a single entry point and offering a highly customizable extension mechanism.
2. Common JavaScript Injection Methods
loadUrl injection
webView.getSettings().setJavaScriptEnabled(true);
public void onPageFinished(WebView webView, String url) {
webView.loadUrl("javascript:javacalljs()");
super.onPageFinished(webView, url);
}evaluateJavascript injection (Android 4.4+)
public void onPageFinished(WebView webView, String url) {
webView.evaluateJavascript("javacalljs()", new ValueCallback
() {
@Override
public void onReceiveValue(String value) {
// handle result
}
});
}HTML
Didi Tech
Official Didi technology account
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.