- js相关bug修复 (by okjack)

- drpy js模块引入支持相对路径 (by okjack)
pull/82/head^2
okjackcaptain 3 years ago
parent 0eb8e57684
commit 31f940523f
  1. 2
      app/src/main/java/com/github/tvbox/osc/js/DrpyMethods.java
  2. 40
      app/src/main/java/com/github/tvbox/osc/js/ES6Module.java
  3. 3
      app/src/main/java/com/github/tvbox/osc/js/JSEngine.java

@ -44,7 +44,7 @@ public class DrpyMethods {
try {
JSONObject jsonObject = obj.toJSONObject();
String method = jsonObject.optString("method", "get");
JSONObject headersJSONObject = jsonObject.getJSONObject("headers");
JSONObject headersJSONObject = jsonObject.optJSONObject("headers");
Map<String, String> headers = Json.toMap(headersJSONObject);
String contentType = null;
for(String headerKey : headers.keySet()) {

@ -1,26 +1,38 @@
package com.github.tvbox.osc.js;
import android.net.Uri;
import android.text.TextUtils;
import com.github.tvbox.osc.api.ApiConfig;
import com.github.tvbox.osc.util.FileUtils;
import com.github.tvbox.osc.util.OkGoHelper;
import com.quickjs.QuickJS;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import okhttp3.Headers;
import okhttp3.Request;
public class ES6Module extends com.quickjs.ES6Module {
private Map<String, String> moduleCache = new ConcurrentHashMap<>();
protected String[] modules = {"cheerio.min.js", "crypto-js.js", "dayjs.min.js", "uri.min.js", "underscore-esm-min.js"};
private String moduleUrl = "";
public ES6Module(QuickJS quickJS) {
super(quickJS);
}
public void setModuleUrl(String url) {
if (url.startsWith("clan://")) url = ApiConfig.get().clanToAddress(url);
moduleUrl = url;
}
@Override
public String getModuleScript(String name) {
if (moduleCache.containsKey(name)) return moduleCache.get(name);
try {
for (String one : modules) {
if (name.contains(one)) {
@ -29,16 +41,26 @@ public class ES6Module extends com.quickjs.ES6Module {
}
}
String content = "";
if (name.startsWith("http://") || name.startsWith("https://")) {
Map<String, String> header = new HashMap<>();
header.put("User-Agent", "Mozilla/5.0");
Request request = new Request.Builder().url(name).headers(Headers.of(header)).build();
content = OkGoHelper.getDefaultClient().newCall(request).execute().body().string();
if (name.startsWith("http:") || name.startsWith("https:")) {
content = getModuleContent(name);
} else if (name.startsWith("assets://")) {
content = FileUtils.getAssetFile(name.substring(9));
} else if (FileUtils.isAssetFile(name, "js/lib")) {
content = FileUtils.getAssetFile("js/lib/" + name);
} else {
String curModuleUrl = "";
if (name.startsWith(".")) name = name.substring(1);
if (name.startsWith("/")) name = name.substring(1);
Uri uri = Uri.parse(moduleUrl);
if (uri.getLastPathSegment() == null) {
curModuleUrl = uri.getScheme() + "://" + name;
} else {
curModuleUrl = uri.toString().replace(uri.getLastPathSegment(), name);
}
content = getModuleContent(curModuleUrl);
}
if (TextUtils.isEmpty(content)) return "";
moduleCache.put(name, content);
return content;
} catch (Exception e) {
e.printStackTrace();
@ -46,6 +68,14 @@ public class ES6Module extends com.quickjs.ES6Module {
}
}
private String getModuleContent(String url) throws Exception {
if (url.startsWith("file://")) return FileUtils.read(url);
Map<String, String> header = new HashMap<>();
header.put("User-Agent", "Mozilla/5.0");
Request request = new Request.Builder().url(url).headers(Headers.of(header)).build();
return OkGoHelper.getDefaultClient().newCall(request).execute().body().string();
}
@Override
public String convertModuleName(String moduleBaseName, String moduleName) {
return super.convertModuleName(moduleBaseName, moduleName);

@ -92,6 +92,7 @@ public class JSEngine {
} else {
moduleJsStr = moduleJsStr.replace("__JS_SPIDER__", "globalThis." + key);
}
module.setModuleUrl(sourceBean.getApi());
module.executeModuleScript(moduleJsStr, moduleName);
JSSpider spider = new JSSpider(module, key);
String extJs = sourceBean.getExt().startsWith("http") ? sourceBean.getExt() : loadExt(sourceBean.getExt());
@ -134,6 +135,8 @@ public class JSEngine {
content = FileUtils.getAssetFile(name.substring(9));
} else if (name.startsWith("file://")) {
content = FileUtils.read(name);
} else {
content = name;
}
if (TextUtils.isEmpty(content)) return "";
jsCache.put(name, content);

Loading…
Cancel
Save