牙叔教程 2021-08-15 13:37:15 阅读数:763
牙叔教程 簡單易懂
我一直覺得現在的autojs和webview交互不正經,
尤其是監聽彈框, 直接dismiss, 那那些需要彈框的網頁怎麼辦?
Autojs版本: 9.0.4
Android版本: 8.0.0
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(String callback);
}
}
複制代碼
<html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script language="javascript"> function onButtonClick() { function randomHexColor() { //隨機生成十六進制顏色 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()"> 改變安卓按鈕顏色 </button> </body>
</html>
複制代碼
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);
複制代碼
ui.layout(
<vertical> <text text="牙叔教程 簡單易懂" textSize="28sp" textColor="#fbfbfe" bg="#00afff" w="*" gravity="center"></text> <webview id="webview" /> <button id="button" text="改變網頁body顏色" /> </vertical>
);
複制代碼
let webView = ui.findById("webview");
webView.getSettings().setJavaScriptEnabled(true);
複制代碼
// 發出命令方: Android
// 執行命令方: Html
// Html返回值: body背景色
ui.button.click(function () {
function test() {
function randomHexColor() {
//隨機生成十六進制顏色
return "#" + ("00000" + ((Math.random() * 0x1000000) << 0).toString(16)).substr(-6);
}
document.body.bgColor = randomHexColor();
return "牙叔教程, " + document.body.bgColor;
}
let js = test.toString() + ";test();";
let valueCallback = new ValueCallback({
onReceiveValue: function (value) {
toastLog("網頁返回值: " + value);
},
});
webView.evaluateJavascript(js, valueCallback);
});
複制代碼
webView.setWebChromeClient(
new JavaAdapter(WebChromeClient, {
onConsoleMessage: function (message) {
message.message && log("h5: " + message.message());
},
})
);
複制代碼
// 重點, 這裏給html注册了一個全局對象: jsInterface
webView.addJavascriptInterface(new JSInterface().setJsCallback(new JSCallback()), "jsInterface");
複制代碼
html = files.path("./index.html");
webView.loadUrl("file://" + html);
複制代碼
思路是最重要的, 其他的百度, bing, stackoverflow, 安卓文檔, autojs文檔, 最後才是群裏問問 --- 牙叔教程
部分內容來自網絡 本教程僅用於學習, 禁止用於其他用途
版权声明:本文为[牙叔教程]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/08/20210815133652360m.html