-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from anilibria/fix/new-new-crashes
Fix/new new crashes
- Loading branch information
Showing
6 changed files
with
248 additions
and
205 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
.../src/main/java/ru/radiationx/anilibria/ui/fragments/comments/webview/VkWebChromeClient.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,48 @@ | ||
package ru.radiationx.anilibria.ui.fragments.comments.webview | ||
|
||
import android.app.AlertDialog | ||
import android.os.Message | ||
import android.webkit.ConsoleMessage | ||
import android.webkit.WebChromeClient | ||
import android.webkit.WebView | ||
import ru.radiationx.anilibria.ui.fragments.comments.VkCommentsViewModel | ||
|
||
class VkWebChromeClient( | ||
private val viewModel: VkCommentsViewModel, | ||
) : WebChromeClient() { | ||
|
||
private val jsErrorRegex = Regex("Uncaught (?:\\w+)Error:") | ||
private val sourceRegex = Regex("https?://vk\\.com/") | ||
|
||
override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean { | ||
return super.onConsoleMessage(consoleMessage) | ||
} | ||
|
||
override fun onConsoleMessage(message: String?, lineNumber: Int, sourceID: String?) { | ||
super.onConsoleMessage(message, lineNumber, sourceID) | ||
val hasJsError = jsErrorRegex.containsMatchIn(message.orEmpty()) | ||
val isVkSource = sourceRegex.containsMatchIn(sourceID.orEmpty()) | ||
if (hasJsError && isVkSource) { | ||
viewModel.notifyNewJsError() | ||
} | ||
} | ||
|
||
override fun onCreateWindow( | ||
view: WebView?, | ||
isDialog: Boolean, | ||
isUserGesture: Boolean, | ||
resultMsg: Message, | ||
): Boolean { | ||
if (view == null) { | ||
return false | ||
} | ||
val newWebView = WebView(view.context) | ||
AlertDialog.Builder(view.context) | ||
.setView(newWebView) | ||
.show() | ||
val transport = resultMsg.obj as WebView.WebViewTransport | ||
transport.webView = newWebView | ||
resultMsg.sendToTarget() | ||
return true | ||
} | ||
} |
Oops, something went wrong.