From e6ecf16f7cd055bf970f6a4ee13ecc82175850fd Mon Sep 17 00:00:00 2001 From: aiselp Date: Mon, 14 Aug 2023 22:19:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BFaxios=E6=94=AF=E6=8C=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AEtimeout=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../axios/browser-libs/XMLHttpRequest.js | 19 ++++++++++++------- autojs/src/main/assets/modules/npm/process.js | 15 +++++++-------- .../autojs/core/ui/widget/JsWebView.kt | 4 ++-- .../com/stardust/autojs/core/web/JsBridge.kt | 11 +---------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/autojs/src/main/assets/modules/axios/browser-libs/XMLHttpRequest.js b/autojs/src/main/assets/modules/axios/browser-libs/XMLHttpRequest.js index c65a7fad0..5d65a8ff3 100644 --- a/autojs/src/main/assets/modules/axios/browser-libs/XMLHttpRequest.js +++ b/autojs/src/main/assets/modules/axios/browser-libs/XMLHttpRequest.js @@ -7,6 +7,7 @@ let HttpUrl = Packages.okhttp3.HttpUrl; let MediaType = Packages.okhttp3.MediaType; let Headers = Packages.okhttp3.Headers; + let InterruptedIOException = java.io.InterruptedIOException const stream = require("stream"); let EventTarget = require("./EventTarget.js"); @@ -24,6 +25,7 @@ ///////XMLHttpRequest对象 let XMLHttpRequest = function () { this.responseType = 'text'; + this.timeout = 0; setReadonlyAttribute(this, 'upload', {}) setReadonlyAttribute(this, 'readyState', 0); setReadonlyAttribute(this, 'status', 0); @@ -105,12 +107,7 @@ XMLHttpRequest.prototype.send = function (body) { atl.addTask(); const xhr = this; - const { - url, - method, - ac, - headers, - } = this._requestData + const { url, method, ac, headers, } = this._requestData const responseType = xhr.responseType; const builder = new Request.Builder(); @@ -120,12 +117,20 @@ let reqBody = parserReqBody(xhr, body); let request = builder.url(url.build()).method(method, reqBody).build(); - let call = XMLHttpRequest._okHttpClient.newCall(request); + let client = XMLHttpRequest._okHttpClient; + if (xhr.timeout > 0) { + client = new OkHttpClient.Builder().callTimeout(xhr.timeout, java.util.concurrent.TimeUnit.MILLISECONDS) + .followRedirects(true).build(); + } + let call = client.newCall(request); setReadonlyAttribute(xhr, '_call', call, false); call.enqueue({ onFailure(call, e) { xhr._setReadyState(4); setReadonlyAttribute(xhr, 'statusText', e.message) + if (e instanceof InterruptedIOException) { + xhr.dispatchEvent(new Event('timeout')); + } xhr.dispatchEvent(new Event('error')) xhr.dispatchEvent(new Event('loadend')) atl.removeTask() diff --git a/autojs/src/main/assets/modules/npm/process.js b/autojs/src/main/assets/modules/npm/process.js index 73bad8e9f..a6591a094 100644 --- a/autojs/src/main/assets/modules/npm/process.js +++ b/autojs/src/main/assets/modules/npm/process.js @@ -1,25 +1,24 @@ -const { - EventEmitter -} = require("events"); -const es = require("event-stream"); + +const { EventEmitter } = require("events"); +// const es = require("event-stream"); const process = new EventEmitter(); //events.on('exit', process.emit.bind(process, 'exit')) - +/* process.stdout = es.map(function(data, callback) { console.log(data); return callback(null, data) }) -/* + process.stdout.end = function() {}; process.stdout.destroy = function(){}; -*/ + process.stderr = es.map(function(data, callback) { console.error(data); return callback(null, data) }) - +*/ process.nextTick = setImmediate; process.env = {} //log(process); diff --git a/autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsWebView.kt b/autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsWebView.kt index 7b4bacee9..641dc9c8e 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsWebView.kt +++ b/autojs/src/main/java/com/stardust/autojs/core/ui/widget/JsWebView.kt @@ -9,7 +9,7 @@ import com.stardust.autojs.core.web.JsBridge open class JsWebView : WebView { //val events = EventEmitter() - + @RequiresApi(Build.VERSION_CODES.M) val jsBridge = JsBridge(this) init { @@ -21,7 +21,7 @@ open class JsWebView : WebView { settings.javaScriptCanOpenWindowsAutomatically = true settings.domStorageEnabled = true settings.displayZoomControls = false - webViewClient = JsBridge.SuperWebViewClient() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) webViewClient = JsBridge.SuperWebViewClient() } constructor(context: Context) : super(context) diff --git a/autojs/src/main/java/com/stardust/autojs/core/web/JsBridge.kt b/autojs/src/main/java/com/stardust/autojs/core/web/JsBridge.kt index 8488bc8be..2ea254bae 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/web/JsBridge.kt +++ b/autojs/src/main/java/com/stardust/autojs/core/web/JsBridge.kt @@ -14,14 +14,13 @@ import org.mozilla.javascript.Undefined import java.io.ByteArrayOutputStream import kotlin.random.Random - +@RequiresApi(Build.VERSION_CODES.M) class JsBridge(private val webView: WebView) { companion object { const val WEBOBJECTNAME = "\$autox" const val JAVABRIDGE = "AutoxJavaBridge" const val sdkPath = "web/autox.sdk.v1.js" - @RequiresApi(Build.VERSION_CODES.M) fun evaluateJavascript(js: String, webView: WebView) { Looper.getMainLooper().queue.addIdleHandler { webView.evaluateJavascript(js, null) @@ -29,7 +28,6 @@ class JsBridge(private val webView: WebView) { } } - @RequiresApi(Build.VERSION_CODES.M) fun injectionJsBridge(webView: WebView) { val js: String = try { val inputStream = webView.context.assets.open(sdkPath) @@ -67,7 +65,6 @@ class JsBridge(private val webView: WebView) { return this } - @RequiresApi(Build.VERSION_CODES.M) fun callHandler(event: String, data: String?, callBack: BaseFunction?) { val fn = if (callBack is Handle) callBack else callBack?.let { Handle(it) } val pos = Pos(getId(), event, data) @@ -81,12 +78,10 @@ class JsBridge(private val webView: WebView) { evaluateJavascript(js, this.webView) } - @RequiresApi(Build.VERSION_CODES.M) fun callHandler(event: String, data: String?) { callHandler(event, data, null) } - @RequiresApi(Build.VERSION_CODES.M) fun callHandler(event: String) { callHandler(event, null, null) } @@ -127,7 +122,6 @@ class JsBridge(private val webView: WebView) { } } - @RequiresApi(Build.VERSION_CODES.M) fun invokeToMainThread(p1: String?, p2: Handle?) { Looper.getMainLooper().queue.addIdleHandler { invoke(p1, p2) @@ -156,7 +150,6 @@ class JsBridge(private val webView: WebView) { inner class JsObject { private val callBackData = HashMap() - @RequiresApi(Build.VERSION_CODES.M) @JavascriptInterface //web调用安卓 fun callHandle(reqData: String) { @@ -184,7 +177,6 @@ class JsBridge(private val webView: WebView) { return Gson().toJson(data) } - @RequiresApi(Build.VERSION_CODES.M) @JavascriptInterface fun callBack(callBackId: Int, data: String?) { val callBack = callHandlerData[callBackId]?.callBack @@ -233,7 +225,6 @@ class JsBridge(private val webView: WebView) { return super.shouldInterceptRequest(view, request) } - @RequiresApi(Build.VERSION_CODES.M) override fun onPageFinished(view: WebView?, url: String?) { super.onPageFinished(view, url) if (view != null) {