diff --git a/quickjs/src/main/java/com/fongmi/quickjs/bean/Req.java b/quickjs/src/main/java/com/fongmi/quickjs/bean/Req.java index 9b7f2046f..efb1d8583 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/bean/Req.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/bean/Req.java @@ -17,6 +17,8 @@ public class Req { private Integer redirect; @SerializedName("timeout") private Integer timeout; + @SerializedName("postType") + private String postType; @SerializedName("method") private String method; @SerializedName("body") @@ -42,6 +44,10 @@ public class Req { return timeout == null ? 10000 : timeout; } + public String getPostType() { + return TextUtils.isEmpty(postType) ? "" : postType; + } + public String getMethod() { return TextUtils.isEmpty(method) ? "get" : method; } diff --git a/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java b/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java index 0d3587363..f8407fc24 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/crawler/Spider.java @@ -28,8 +28,6 @@ import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import dalvik.system.DexClassLoader; @@ -184,22 +182,17 @@ public class Spider extends com.github.catvod.crawler.Spider { } private String getContent() { + String global = "globalThis." + key; String content = Module.get().load(api); if (content.contains("__jsEvalReturn")) { - return catvod(content); + return content.concat(global).concat(" = __jsEvalReturn()"); } else if (content.contains("__JS_SPIDER__")) { - return content.replace("__JS_SPIDER__", "globalThis." + key); + return content.replace("__JS_SPIDER__", global); } else { - return content.replaceAll("export default.*?[{]", "globalThis." + key + " = {"); + return content.replaceAll("export default.*?[{]", global + " = {"); } } - private String catvod(String content) { - String[] split = content.split("export\\s+function\\s+__jsEvalReturn.*?[{]"); - Matcher matcher = Pattern.compile("\\s?return\\s?([\\s+\\S]*)\\s?\\}").matcher(split[1]); - return matcher.find() ? split[0].concat("globalThis." + key + " = ").concat(matcher.group(1)) : content; - } - private JSObject convert(HashMap map) { JSObject obj = ctx.createNewJSObject(); if (map == null || map.isEmpty()) return obj; diff --git a/quickjs/src/main/java/com/fongmi/quickjs/method/Function.java b/quickjs/src/main/java/com/fongmi/quickjs/method/Function.java index 0f3ad5f90..da6f8d8dd 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/method/Function.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/method/Function.java @@ -26,15 +26,11 @@ public class Function implements Callable { @Override public Object[] call() throws Exception { JSFunction func = jsObject.getJSFunction(name); - boolean async = func.getJSFunction("toString").call().toString().startsWith("async"); - return new Object[]{async ? async(func) : func.call(args)}; - } - - private Object async(JSFunction func) { - JSObject promise = (JSObject) func.call(args); - JSFunction then = promise.getJSFunction("then"); + JSObject object = (JSObject) func.call(args); + JSFunction then = object.getJSFunction("then"); + if (then == null) return new Object[]{object}; then.call(jsCallFunction); - return result; + return new Object[]{result}; } private final JSCallFunction jsCallFunction = new JSCallFunction() { diff --git a/quickjs/src/main/java/com/fongmi/quickjs/method/Global.java b/quickjs/src/main/java/com/fongmi/quickjs/method/Global.java index 0da19efc3..18557f87b 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/method/Global.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/method/Global.java @@ -9,6 +9,7 @@ import com.fongmi.quickjs.bean.Req; import com.fongmi.quickjs.utils.Parser; import com.fongmi.quickjs.utils.Proxy; import com.github.catvod.net.OkHttp; +import com.github.catvod.utils.Json; import com.google.gson.Gson; import com.whl.quickjs.wrapper.JSArray; import com.whl.quickjs.wrapper.JSFunction; @@ -19,12 +20,14 @@ import com.whl.quickjs.wrapper.QuickJSContext; import java.io.UnsupportedEncodingException; import java.lang.reflect.Method; import java.net.URLEncoder; +import java.util.Map; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import okhttp3.Call; +import okhttp3.FormBody; import okhttp3.Headers; import okhttp3.MediaType; import okhttp3.OkHttpClient; @@ -161,7 +164,7 @@ public class Global { timer.schedule(new TimerTask() { @Override public void run() { - if (!executor.isShutdown()) executor.submit(() -> {func.call();}); + if (!executor.isShutdown()) executor.submit(() -> { func.call(); }); } }, delay); } @@ -183,11 +186,19 @@ public class Global { } private RequestBody getPostBody(Req req, String contentType) { + if (req.getData() != null && req.getPostType().equals("form")) return getFormBody(req); if (req.getData() != null) return RequestBody.create(gson.toJson(req.getData()), MediaType.get("application/json")); if (req.getBody() != null && contentType != null) return RequestBody.create(gson.toJson(req.getBody()), MediaType.get(contentType)); return RequestBody.create("", null); } + private RequestBody getFormBody(Req req) { + FormBody.Builder formBody = new FormBody.Builder(); + Map params = Json.toMap(req.getData()); + for (String key : params.keySet()) formBody.add(key, params.get(key)); + return formBody.build(); + } + private String getCharset(Headers headers) { String contentType = headers.get("Content-Type"); if (TextUtils.isEmpty(contentType)) return "UTF-8";