forked from qiusunshine/hikerView
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae87505
commit 3957440
Showing
8 changed files
with
932 additions
and
117 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/example/hikerview/service/http/ContentTypeAfterInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/example/hikerview/service/http/ContentTypePreInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/src/main/java/com/example/hikerview/service/parser/WebkitFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.