Skip to content

Commit

Permalink
feat:Use shouldOverrideUrlLoading to intercept request
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinnZou committed Apr 21, 2024
1 parent fa6653c commit 8b802c5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal fun BasicWebViewSample() {
request.let {
Logger.i { "Sample onInterceptRequest: $it" }
}
return if (request.isForMainFrame && request.url.contains("github")) {
return if (request.url.contains("github")) {
WebRequestInterceptResult.Modify(
WebRequest(
url = "https://kotlinlang.org/docs/multiplatform.html",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
Expand Down Expand Up @@ -311,90 +310,97 @@ open class AccompanistWebViewClient : WebViewClient() {
}
}

// override fun shouldOverrideUrlLoading(
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?,
): Boolean {
KLogger.d {
"shouldOverrideUrlLoading: ${request?.url} ${request?.isForMainFrame} ${request?.isRedirect} ${request?.method}"
}
if (isRedirect || request == null || navigator.requestInterceptor == null) {
isRedirect = false
return super.shouldOverrideUrlLoading(view, request)
}
val webRequest =
WebRequest(
request.url.toString(),
request.requestHeaders?.toMutableMap() ?: mutableMapOf(),
request.isForMainFrame,
request.isRedirect,
request.method ?: "GET",
)
val interceptResult =
navigator.requestInterceptor!!.onInterceptUrlRequest(
webRequest,
navigator,
)
return when (interceptResult) {
is WebRequestInterceptResult.Allow -> {
false
}

is WebRequestInterceptResult.Reject -> {
navigator.stopLoading()
true
}

is WebRequestInterceptResult.Modify -> {
isRedirect = true
interceptResult.request.apply {
navigator.stopLoading()
navigator.loadUrl(this.url, this.headers)
}
true
}
}
}

// override fun shouldInterceptRequest(
// view: WebView?,
// request: WebResourceRequest?,
// ): Boolean {
// KLogger.d {
// "shouldOverrideUrlLoading: ${request?.url}"
// ): WebResourceResponse? {
// KLogger.d { "shouldInterceptRequest: ${request?.url} ${request?.isForMainFrame} ${request?.isRedirect} ${request?.method}" }
// if (isRedirect || request == null || navigator.requestInterceptor == null) {
// isRedirect = false
// return super.shouldInterceptRequest(view, request)
// }
// navigator.requestInterceptor?.apply {
// if (request.isForMainFrame) {
// val webRequest =
// WebRequest(
// request?.url.toString(),
// request?.requestHeaders?.toMutableMap() ?: mutableMapOf(),
// request.url.toString(),
// request.requestHeaders.toMutableMap(),
// request.isForMainFrame,
// request.isRedirect,
// request.method ?: "GET",
// )
// val interceptResult =
// this.beforeRequest(
// navigator.requestInterceptor!!.onInterceptUrlRequest(
// webRequest,
// navigator,
// )
// return when (interceptResult) {
// is WebRequestInterceptResult.Allow -> {
// super.shouldOverrideUrlLoading(view, request)
// super.shouldInterceptRequest(view, request)
// }
//
// is WebRequestInterceptResult.Reject -> {
// true
// navigator.stopLoading()
// super.shouldInterceptRequest(view, request)
// }
//
// is WebRequestInterceptResult.Redirect -> {
// is WebRequestInterceptResult.Modify -> {
// isRedirect = true
// interceptResult.request.apply {
// navigator.stopLoading()
// navigator.loadUrl(this.url, this.headers)
// }
// true
// null
// }
// }
// }
// return super.shouldOverrideUrlLoading(view, request)
// isRedirect = false
// return super.shouldInterceptRequest(view, request)
// }

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?,
): WebResourceResponse? {
KLogger.d { "shouldInterceptRequest: ${request?.url} ${request?.isForMainFrame} ${request?.isRedirect} ${request?.method}" }
if (isRedirect) {
isRedirect = false
return super.shouldInterceptRequest(view, request)
}
navigator.requestInterceptor?.apply {
val webRequest =
WebRequest(
request?.url.toString(),
request?.requestHeaders?.toMutableMap() ?: mutableMapOf(),
request?.isForMainFrame ?: false,
request?.isRedirect ?: false,
request?.method ?: "GET",
)
val interceptResult =
this.onInterceptUrlRequest(
webRequest,
navigator,
)
return when (interceptResult) {
is WebRequestInterceptResult.Allow -> {
super.shouldInterceptRequest(view, request)
}

is WebRequestInterceptResult.Reject -> {
navigator.stopLoading()
super.shouldInterceptRequest(view, request)
}

is WebRequestInterceptResult.Modify -> {
isRedirect = true
interceptResult.request.apply {
navigator.stopLoading()
navigator.loadUrl(this.url, this.headers)
}
null
}
}
}
isRedirect = false
return super.shouldInterceptRequest(view, request)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal object KLogger : Logger(

// For iOS, it will not print out the log if the severity is upper than Debug in AS.
fun info(msg: () -> String) {
d { msg() }
i { msg() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class WKNavigationDelegate(
"Outer decidePolicyForNavigationAction: $url $isRedirect $decidePolicyForNavigationAction ${decidePolicyForNavigationAction.request.allHTTPHeaderFields}"
}
if (url != null && !isRedirect &&
navigator.requestInterceptor != null
navigator.requestInterceptor != null &&
decidePolicyForNavigationAction.targetFrame?.mainFrame == true
) {
navigator.requestInterceptor.apply {
val request = decidePolicyForNavigationAction.request
Expand All @@ -128,7 +129,7 @@ class WKNavigationDelegate(
webRequest,
navigator,
)
return when (interceptResult) {
when (interceptResult) {
is WebRequestInterceptResult.Allow -> {
decisionHandler(WKNavigationActionPolicy.WKNavigationActionPolicyAllow)
}
Expand Down

0 comments on commit 8b802c5

Please sign in to comment.