Skip to content

Commit

Permalink
restrict deck option links
Browse files Browse the repository at this point in the history
  • Loading branch information
RobozinhoD committed Jul 13, 2024
1 parent 2c959dc commit 7f8c5e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/pages/DeckOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package com.ichi2.anki.pages
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebView
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.OnPageFinishedCallback
import com.ichi2.anki.R
import com.ichi2.anki.dialogs.DiscardChangesDialog
import com.ichi2.anki.utils.openUrl
import com.ichi2.anki.withProgress
import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.undoableOp
Expand Down Expand Up @@ -60,11 +63,27 @@ class DeckOptions : PageFragment() {
override fun onCreateWebViewClient(savedInstanceState: Bundle?): PageWebViewClient {
requireActivity().onBackPressedDispatcher.addCallback(this, onBackSaveCallback)
requireActivity().onBackPressedDispatcher.addCallback(this, onBackCallback)
return PageWebViewClient().apply {
onPageFinishedCallback = OnPageFinishedCallback { view ->

return object : PageWebViewClient() {
private val ankiManualHostRegex = Regex("^docs\\.ankiweb\\.net\$")

override var onPageFinishedCallback: OnPageFinishedCallback? = OnPageFinishedCallback { view ->
Timber.v("canGoBack: %b", view.canGoBack())
onBackCallback.isEnabled = view.canGoBack()
}

override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
val host = request?.url?.host ?: return shouldOverrideUrlLoading(view, request)
return if (ankiManualHostRegex.matches(host)) {
super.shouldOverrideUrlLoading(view, request)
} else {
openUrl(request.url)
true
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class PageWebViewClient : WebViewClient() {
/** Wait for the provided promise to complete before showing the WebView */
open val promiseToWaitFor: String? = null

var onPageFinishedCallback: OnPageFinishedCallback? = null
open var onPageFinishedCallback: OnPageFinishedCallback? = null

override fun shouldInterceptRequest(
view: WebView,
Expand Down

0 comments on commit 7f8c5e3

Please sign in to comment.