Support evaluateJavascript

pull/199/head
FongMi 2 years ago
parent 524106a1d8
commit f9b5183aa8
  1. 6
      app/src/main/java/com/fongmi/android/tv/bean/Rule.java
  2. 40
      app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWebView.java
  3. 7
      app/src/main/java/com/fongmi/android/tv/utils/Sniffer.java

@ -21,6 +21,8 @@ public class Rule {
private List<String> hosts;
@SerializedName("regex")
private List<String> regex;
@SerializedName("script")
private List<String> script;
public static Rule create(String name) {
return new Rule(name);
@ -48,6 +50,10 @@ public class Rule {
return regex == null ? Collections.emptyList() : regex;
}
public List<String> getScript() {
return script == null ? Collections.emptyList() : script;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) return true;

@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.text.TextUtils;
import android.view.ViewGroup;
@ -27,6 +28,7 @@ import com.github.catvod.crawler.Spider;
import com.google.common.net.HttpHeaders;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
public class CustomWebView extends WebView {
@ -87,9 +89,9 @@ public class CustomWebView extends WebView {
String url = request.getUrl().toString();
String host = request.getUrl().getHost();
if (TextUtils.isEmpty(host) || ApiConfig.get().getAds().contains(host)) return empty;
if (host.equals("challenges.cloudflare.com")) App.post(() -> showDialog());
if (url.contains("challenges.cloudflare.com/cdn-cgi/")) App.post(() -> showDialog());
Map<String, String> headers = request.getRequestHeaders();
if (isVideoFormat(headers, url)) post(headers, url);
if (isVideoFormat(headers, url)) interrupt(headers, url);
return super.shouldInterceptRequest(view, request);
}
@ -99,6 +101,12 @@ public class CustomWebView extends WebView {
if (dialog != null) hideDialog();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
evaluate(Sniffer.getScript(Uri.parse(url)));
}
@Override
@SuppressLint("WebViewClientOnReceivedSslError")
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
@ -112,6 +120,21 @@ public class CustomWebView extends WebView {
};
}
private void showDialog() {
if (dialog != null) return;
if (getParent() != null) ((ViewGroup) getParent()).removeView(this);
dialog = new AlertDialog.Builder(App.activity()).setView(this).show();
}
private void hideDialog() {
if (dialog != null) dialog.dismiss();
dialog = null;
}
private void evaluate(List<String> script) {
if (script.size() > 0) evaluateJavascript(script.get(0), value -> evaluate(script.subList(1, script.size())));
}
private boolean isVideoFormat(Map<String, String> headers, String url) {
try {
Site site = ApiConfig.get().getSite(key);
@ -123,23 +146,12 @@ public class CustomWebView extends WebView {
}
}
private void post(Map<String, String> headers, String url) {
private void interrupt(Map<String, String> headers, String url) {
String cookie = CookieManager.getInstance().getCookie(url);
if (cookie != null) headers.put(HttpHeaders.COOKIE, cookie);
onParseSuccess(headers, url);
}
private void showDialog() {
if (dialog != null) return;
if (getParent() != null) ((ViewGroup) getParent()).removeView(this);
dialog = new AlertDialog.Builder(App.activity()).setView(this).show();
}
private void hideDialog() {
if (dialog != null) dialog.dismiss();
dialog = null;
}
private void onParseSuccess(Map<String, String> headers, String url) {
if (callback != null) callback.onParseSuccess(headers, url, from);
App.post(() -> stop(false));

@ -67,4 +67,11 @@ public class Sniffer {
for (Rule rule : ApiConfig.get().getRules()) for (String host : rule.getHosts()) if (Util.containOrMatch(hosts, host)) return rule.getRegex();
return Collections.emptyList();
}
public static List<String> getScript(Uri uri) {
if (uri.getHost() == null) return Collections.emptyList();
String hosts = TextUtils.join(",", Arrays.asList(UrlUtil.host(uri), UrlUtil.host(uri.getQueryParameter("url"))));
for (Rule rule : ApiConfig.get().getRules()) for (String host : rule.getHosts()) if (Util.containOrMatch(hosts, host)) return rule.getScript();
return Collections.emptyList();
}
}

Loading…
Cancel
Save