Where does WebView or WebViewClient make requests to <link>'s - android

I am loading a html file into a WebView and want to override the WebView's requests to stylesheet <link> and <image> elements in the <head> so that I can load a file from the machine
the links in the html are like so
<head>
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<svg width="100%" height="100%" viewBox="0 0 352 500>
<image width="352" height="500" xlink:href="image.jpeg"/>
</svg>
</body>
I have a WebViewClient with this code but none of these make a request to these links. shouldInterceptRequest and onLoadResource both just recieve the entire html file at request?.url
class EpubWebViewClient(): WebViewClient() {
override fun onLoadResource(view: WebView?, url: String?) {
Log.i("loadurl", "onLoadResource $url")
super.onLoadResource(view, url)
}
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
Log.i("loadurl", "shouldOverrideUrlLoading ${request?.url}")
return super.shouldOverrideUrlLoading(view, request)
}
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
Log.i("loadurl", "shouldInterceptRequest ${request?.url}")
return super.shouldInterceptRequest(view, request)
}
}
I would image there is a function like but perhaps there is not idk
override fun idealUrlOverloadFunction(url: String): String or something else {
return openLocalFile(url)
}
Does the WebView or WebViewClient even make requests for these?
If so how can I interupt them and load a resource from the machine instead of the web?

solved by me, consider this link
<link href="../stylesheet.css" rel="stylesheet" type="text/css"/>
apending some `string + :' combo such as
<link href="mystuff:../stylesheet.css" rel="stylesheet" type="text/css"/>
will cause it to show up in shouldInterceptRequest()
I am unsure as to weather this is caused by the link not firing or shouldInterceptRequest not listening though, but an easy fix with .replace

Related

How to detect a link to another section on the same page on an Android WebView

I know how to detect a user tap on a link inside a WebView:
binding.webview.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
println("shouldOverrideUrlLoading: ${request?.url?.toString()}")
//do whatever and return false if we don't want the link to be launched
return false
}
}
but this is not working for links to the same page, for example:
<a href="#tabpanel-size-guide-1">
Size guide
</a>
I guess this is because links to same page are not loading a new url, and then this event shouldOverrideUrlLoading is not triggered.
So, my question is: Are there a way to detect user taps on this kind of links?

Android WebView loadDataWithBaseURL shouldInterceptRequest

I am using a WebView to display an HTML string with img tags. These tags must display pictures stored in a ZIP archive, so I need to intercept the images requests to return a ZipInputStream.
I'm thus using loadDataWithBaseURL and shouldInterceptRequest, but shouldInterceptRequest is never called for my pictures requests. Here's my WebViewClient client:
webView.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
println("shouldInterceptRequest1 url: ${request?.url}")
return super.shouldInterceptRequest(view, request)
}
override fun shouldInterceptRequest(view: WebView?, url: String?): WebResourceResponse? {
println("shouldInterceptRequest2 url: $url")
#Suppress("DEPRECATION")
return super.shouldInterceptRequest(view, url)
}
}
And here's what I do to load the HTML:
webView.loadDataWithBaseURL(null, "<div><img src='file://path/to/my/image.png'/></div>", "text/html", "UTF-8", null)
All I get in the logcat is this:
shouldInterceptRequest1 url: data:text/html;charset=utf-8;base64,
shouldInterceptRequest2 url: data:text/html;charset=utf-8;base64,
The img request does not trigger shouldInterceptRequest.
What's wrong?
Despite the doc says this:
This callback is invoked for a variety of URL schemes (e.g., http(s):,
data:, file:, etc.), not only those schemes which send requests over
the network. This is not called for javascript: URLs, blob: URLs, or
for assets accessed via file:///android_asset/ or file:///android_res/
URLs.
In my very basic sample code shouldInterceptRequest isn't called for file:// URIs when using loadDataWithBaseURL... Using a http:// scheme works.

Pdf sometimes not loading with google embedded viewer on Android

I am using the google docs pdf embedded viewer in my android app to display my pdfs. Sometimes the viewer doesn't load my file even though most of the time it does and it's pretty random when it doesn't.
I generate the url with "https://docs.google.com/viewer?embedded=true&url=" + myUrl.
And after that I load the webview :
showLoader()
web_view.settings.javaScriptEnabled = true
web_view.clearCache(true)
web_view.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
hideLoader()
}
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
web_view.loadUrl(url)
return true
}
override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
println("before handler")
handler.proceed()
println("after handler")
}
}
web_view.loadUrl(url)
I tried to proceed if I encounter any sslError, to override urlLoading but none of this resolved my problem.
To test here is one of the url which doesn't always load :
http://docdif.fr.grpleg.com/general/MEDIAGRP/NP-FT-GT/LE10061AA.pdf
One thing I tried too is show the url when display on my app and when it doesn't load on my viewer it blocks too on my navigator. But if I reload the page it does charge.
Any idea what the bug might be?
Try this:
public void onPageFinished(WebView view, String url) {
if (view.getTitle().equals(""))
view.reload();
}
I hope that it can help you!

How to wait when page is loaded and then execute Javascript code in Android WebViewClient

I want processHTML to be triggered after the webpage is done loading. I think I need:
document.load =
for this. But I don't know the correct syntax for how to put it in loadUrl:
class JavaScriptTokenSubstractInterface {
#JavascriptInterface
#SuppressWarnings("unused")
fun processHTML(html: String) {
Log.d("","html shown is loading and not the result.")
}
}
val webView: WebView = findViewById(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.settings.useWideViewPort = true
webView.requestFocus(View.FOCUS_DOWN)
webView.addJavascriptInterface(JavaScriptTokenSubstractInterface(), "HTMLOUT")
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(webView: WebView?, url: String?) {
super.onPageFinished(webView, url)
Thread.sleep(3000)
webView?.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');")
}
}
webView.loadUrl("www.someWebPageThatIsLoading.nl")
What happens:
1. The loading of the page is shown
2. processHTML is triggered and it shows the HTML of the loading page
3. A few seconds go by and the page is done loading
processHTML should be triggered after the loading is done.

How to link multiple HTML pages in web view from assets folder in android?

Getting error **html exposed beyond app through Intent.getData()** when trying to call another HTML file through html link inside web view from local assets folder in android studio.
Below Code might resolve your issue
webView.settings.javaScriptEnabled = true
webView.webViewClient = object : WebViewClient()
{
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return true
}
// From api level 24
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
// Get the mailto url
val url = request.url.toString()
view.loadUrl(url)
// Return true means, leave the current web view and handle the url itself
return true
}
}
webView.loadUrl("file:///android_asset/sample.html")

Categories

Resources