Skip to content

Commit

Permalink
使用WebView获取源码、X5Rule动态解析
Browse files Browse the repository at this point in the history
  • Loading branch information
qiusunshine committed Apr 17, 2022
1 parent ae87505 commit 3957440
Show file tree
Hide file tree
Showing 8 changed files with 932 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.hikerview.service.http

import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
* Bridges from application code to network code. First it builds a network request from a user
* request. Then it proceeds to call the network. Finally it builds a user response from the network
* response.
*/
object ContentTypeAfterInterceptor : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val userRequest = chain.request()
val requestBuilder = userRequest.newBuilder()

val body = userRequest.body
if (body != null) {
if (userRequest.header("Content-Type-Temp") != null) {
requestBuilder.header("Content-Type", userRequest.header("Content-Type-Temp")!!)
requestBuilder.removeHeader("Content-Type-Temp")
}
}

return chain.proceed(requestBuilder.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.hikerview.service.http

import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
* Bridges from application code to network code. First it builds a network request from a user
* request. Then it proceeds to call the network. Finally it builds a user response from the network
* response.
*/
object ContentTypePreInterceptor : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val userRequest = chain.request()
val requestBuilder = userRequest.newBuilder()

val body = userRequest.body
if (body != null) {
val contentType = body.contentType()
//FormBody写死了contentType方法,BridgeInterceptor会覆盖Content-Type,这里反覆盖一下
if (contentType != null && "application/x-www-form-urlencoded" == contentType.toString() && userRequest.header("Content-Type") != null) {
requestBuilder.header("Content-Type-Temp", userRequest.header("Content-Type")!!)
}
}
try {
if(userRequest.url.host.contains("gitee.com") && userRequest.header("Referer") == null){
requestBuilder.header("Referer", "https://gitee.com")
}
} catch (e: Exception) {
e.printStackTrace()
}
return chain.proceed(requestBuilder.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.example.hikerview.service.parser

import android.webkit.WebView
import com.alibaba.fastjson.JSONArray
import com.annimon.stream.function.Consumer
import com.example.hikerview.ui.Application
import com.example.hikerview.ui.browser.util.UUIDUtil
import com.example.hikerview.ui.home.model.article.extra.X5Extra
import com.example.hikerview.ui.home.webview.ArticleWebkitHolder
import com.example.hikerview.utils.StringUtil
import com.example.hikerview.utils.ThreadTool
import org.apache.commons.lang3.StringUtils
import java.util.*

/**
* 作者:By 15968
* 日期:On 2021/9/20
* 时间:At 14:42
*/
class WebkitFetcher {
private var webViewHolder: ArticleWebkitHolder? = null
fun fetch(url0: String?, op: Map<*, *>?, codeListener: Consumer<String?>) {
ThreadTool.runOnUI {
val headers: Map<String, String?>? =
if (op == null) null else op["headers"] as Map<String, String?>?
val url = StringUtils.replaceOnceIgnoreCase(url0, "webview://", "")

val webView = WebView(Application.application.homeActivity)
webViewHolder = ArticleWebkitHolder(webView)
webViewHolder!!.initWebView(Application.application.homeActivity)
var ua: String? = null
if (headers != null) {
ua = headers["content-type"]
if (StringUtil.isEmpty(ua)) {
ua = headers["Content-Type"]
}
if (StringUtil.isEmpty(ua)) {
ua = headers["Content-type"]
}
if (StringUtil.isNotEmpty(ua)) {
webViewHolder!!.webView.settings.userAgentString = ua
}
}
val x5Extra = X5Extra()
if (ua != null) {
x5Extra.ua = ua
}
val blockRules =
if (op != null && op.containsKey("blockRules")) op["blockRules"] as JSONArray? else null
val js =
if (op != null && op.containsKey("js")) op["js"] as String? else null
if (!js.isNullOrEmpty()) {
x5Extra.js = js
}
if (blockRules != null && blockRules.size > 0) {
val rules: MutableList<String> = ArrayList()
for (i in blockRules.indices) {
rules.add(blockRules.getString(i))
}
x5Extra.blockRules = rules
}
webViewHolder!!.x5Extra = x5Extra
webViewHolder!!.finishPageConsumer = Consumer {
// JSEngine.getInstance().log("webview pageFinish: $url", null)
val sign = UUIDUtil.genUUID()
webViewHolder!!.webView.evaluateJavascript("(function(){fy_bridge_app.putVar('$sign', document.getElementsByTagName('html')[0].outerHTML);return 'ok'})()") { value: String? ->
val code = JSEngine.getInstance().getVar(sign, "")
JSEngine.getInstance().clearVar(sign)
codeListener.accept(code)
// JSEngine.getInstance().log("webview pageFinish2: $url", null)
destroy()
}
}
// JSEngine.getInstance().log("webview start: $url", null)
if (headers != null) {
webViewHolder!!.webView.loadUrl(url, headers)
} else {
webViewHolder!!.webView.loadUrl(url)
}
}
}

fun destroy() {
ThreadTool.runOnUI {
if (webViewHolder != null && webViewHolder!!.webView != null) {
webViewHolder!!.webView.stopLoading()
webViewHolder!!.webView.onPause()
webViewHolder!!.webView.destroy()
webViewHolder = null
}
}
}

companion object {
fun canParse(url: String): Boolean {
return StringUtil.isNotEmpty(url) && url.startsWith("webview://")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
Expand Down Expand Up @@ -96,7 +99,13 @@ public boolean parse(Activity activity, String url, String extra, Consumer<Strin
webViewHolder.getWebView().getSettings().setUserAgentString(x5Extra.getUa());
}
webViewHolder.setX5Extra(x5Extra);
webViewHolder.getWebView().loadUrl(loadUrl);
if (StringUtil.isNotEmpty(x5Extra.getReferer())) {
Map<String, String> headers = new HashMap<>();
headers.put("Referer", x5Extra.getReferer());
webViewHolder.getWebView().loadUrl(loadUrl, headers);
} else {
webViewHolder.getWebView().loadUrl(loadUrl);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
Expand Down Expand Up @@ -104,7 +107,13 @@ public boolean parse(Activity activity, String url, String extra, Consumer<Strin
webViewHolder.getWebView().getSettings().setUserAgentString(x5Extra.getUa());
}
webViewHolder.setX5Extra(x5Extra);
webViewHolder.getWebView().loadUrl(loadUrl);
if (StringUtil.isNotEmpty(x5Extra.getReferer())) {
Map<String, String> headers = new HashMap<>();
headers.put("Referer", x5Extra.getReferer());
webViewHolder.getWebView().loadUrl(loadUrl, headers);
} else {
webViewHolder.getWebView().loadUrl(loadUrl);
}
return true;
}

Expand Down
Loading

0 comments on commit 3957440

Please sign in to comment.