The tutorial is easy to understand

Results show

origin

I’ve always thought that the current autoJS and WebView interaction is not serious,

  • Listening bounced
  • Listening to console Logs
  • Monitor page title
  • Listening to the url

In particular, listen for pop-ups, direct dismiss, what about those pages that need pop-ups?

The environment

Autojs version: 9.0.4

Android version: 8.0.0

Train of thought

  • Autojs as orders, the webview as an executive order, method of use: the webview. EvaluateJavascript
  • The webview as orders, autojs as an executive command, method of use: the webview. AddJavascriptInterface

Here’s what you’ll learn

  • Get random colors
  • Change the background color of the page
  • Change the button background color
  • EvaluateJavascript callback function
  • The addJavascriptInterface callback function
  • Use of the @javascriptInterface annotation
  • Internal interface of a Java class

The code on

1. Create class JSInterface and package it as dex to call autoJS
package com.yashu.simple;

import android.webkit.JavascriptInterface;

public class JSInterface {
    private JSCallback jsCallback;

    public JSInterface setJsCallback(JSCallback jsCallback) {
        this.jsCallback = jsCallback;
        return this;
    }


    @JavascriptInterface
    public void share(String callback) {
        if(jsCallback ! =null) {
            jsCallback.jsShare(callback);
        }
    }


    public interface JSCallback {
        void jsShare(Stringcallback); }}Copy the code
2. The web page
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script language="javascript">
      function onButtonClick() {
        function randomHexColor() {
          // Randomly generate hexadecimal colors
          return "#" + ("00000" + ((Math.random() * 0x1000000) < <0).toString(16)).substr(-6);
        }
        let color = randomHexColor();
        jsInterface.share(color);
      }
    </script>
  </head>

  <body>
    <img
      id="image"
      width="328"
      height="185"
      src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
    />
    <button type="button" style="width: 328px; height: 185px; font-size: 40px" onclick="onButtonClick()">Change the android button color</button>
  </body>
</html>
Copy the code
3. Load dex and import JSInterface
let dexPath = files.path("./classes2.dex");
runtime.loadDex(dexPath);
importClass(android.webkit.JavascriptInterface);
importClass(android.webkit.WebViewClient);
importClass(android.webkit.ValueCallback);
importClass(android.webkit.WebChromeClient);
importClass(com.yashu.simple.JSInterface);
Copy the code
4. The UI interface
ui.layout(
  <vertical>
    <text text="Uncle Tooth tutorial is easy to understand." textSize="28sp" textColor="#fbfbfe" bg="#00afff" w="*" gravity="center"></text>
    <webview id="webview" />
    <button id="button" text="Change the page body color" />
  </vertical>
);
Copy the code
5. Set webView properties
let webView = ui.findById("webview");
webView.getSettings().setJavaScriptEnabled(true);
Copy the code
6. Set the button click event
// Send the command to Android
// Execute the command: Html
// Html return value: body background color
ui.button.click(function () {
  function test() {
    function randomHexColor() {
      // Randomly generate hexadecimal colors
      return "#" + ("00000" + ((Math.random() * 0x1000000) < <0).toString(16)).substr(-6);
    }
    document.body.bgColor = randomHexColor();
    return "Uncle Tooth Course," + document.body.bgColor;
  }
  let js = test.toString() + "; test();";
  let valueCallback = new ValueCallback({
    onReceiveValue: function (value) {
      toastLog("Page return value:"+ value); }}); webView.evaluateJavascript(js, valueCallback); });Copy the code
7. Web page logs Logs are displayed on the console to check errors
webView.setWebChromeClient(
  new JavaAdapter(WebChromeClient, {
    onConsoleMessage: function (message) {
      message.message && log("h5: "+ message.message()); }}));Copy the code
8. How to use JSInterface
// The main point here is to register a global object for HTML: jsInterface
webView.addJavascriptInterface(new JSInterface().setJsCallback(new JSCallback()), "jsInterface");
Copy the code
9. Load the web page
html = files.path("./index.html");
webView.loadUrl("file://" + html);
Copy the code

Quotes.

The idea is the most important, other Baidu, Bing, StackOverflow, Android documents, autoJS documents, and finally the group to ask – tooth tutorial

The statement

This tutorial is for study only and is prohibited for other purposes