react native webview check the url before load - android

I know the method onShouldStartLoadWithRequest exists for only ios but is there anything equivalent for android? I need to check the url before the webview loads. I know onNavigationStateChange and onLoadStart gets called initially but i need something even before this.

You can set a webViewClient tpo your webView object and override shouldOverrideUrlLoading(WebView view, WebResourceRequest request) and get the url like request.getUrl()

Related

Detect URL changes in Webview android

I am using android webview and loading an url. Inside the webview, there are certain click events and on clicking , the UI inside webview is changed. I am not getting any callback in shouldOverrideUrlLoading(...). Is there a way to detect the changes inside webview in android?
The shouldOverrideUrlLoading() is called only when baseUrl changes.
The onPageFinished(WebView view, String url) method is called when every url changes and loads. The Url can be parsed to find out the change.

Is shouldOverrideUrlLoading called for links within the same domain?

I have a hybrid application where I have a WebView which is implementing the shouldOverrideUrlLoading method (both the deprecated and the newest version). This should take over control before loading any external links or certain links within my domain. Without going into specifics, the code looks roughtly like this:
private WebView mWebView;
mWebView.setWebViewClient(new myWebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.isExternal() || url.contains("#specialCase")) {
// Do actions
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
I have noticed that all external links work properly, however shouldOverrideUrlLoading is not being called at all when the link is within my domain, so there is no way for me to detect those cases where I want to take over control.
The android documentation states
Give the host application a chance to take over the control when a new
url is about to be loaded in the current WebView.
Does that new mean different domain? Is there anything I am missing or doing wrong? Any ideas on how to detect the user has clicked a link pointing to the same domain?
Thank you in advance.
Finally found the reason why shouldOverrideUrlLoading was never been called.
Apparently the method is only called when the actual loading is about to start. Our web application is a single-page application, hence even though the URL changes, no new page is loaded and shouldOverrideUrlLoading is not called.

android webview form post

I want to write a form proxy in my app. The html file is stored locally and the form code is
<form action="custom" method="POST" />
My custom WebView should handle the action tag differently. So custom should be redirected to a different URL, depending on user settings in my app.
I wrote a custom WebView and where I override postUrl, but it is never fired. I also tried a custom WebViewClient and override shouldOverrideUrlLoading, but this is also not fired.
Which method should I override to change my post url and the CONTENT-TYPE for my formular?
Edit: I found https://code.google.com/p/android/issues/detail?id=9122 and I thing nobody found a solution for this problem. That's really bad.
Inject javascript into WebView right after the page is loaded and change action of the form using it. You can simply inject javascript by calling webView.loadUrl("javascript:...");

How do I know the loaded URL has method type POST in Android?

I have a webview in Android app and load my webapplication. I am listening the page navigation using ShouldOverrideURL method and do some operation. I want to do specific operation when the URL method type is GET or POST. I cannot use PostURL method here as I load only one home URL.
It looks like shouldOverrideUrl is only called during GET requests: Android - how to intercept a form POST in android WebViewClient on API level 4
If you want to do a specific operation during a POST operation for a particular URL, you want to extend WebView and override WebView#postUrl(String url, byte[] postData).

Inject Content into WebView Before Loading Url

I have an app with a previously-existing, web-based registration process that I am trying to use inside a WebView. I need to add some style tags to the html in order to hide some elements for better displaying the content inside my app. I can get it to work on initial load, but I cannot figure out how to do it from one page to the next inside the WebView. Here is what I have working:
On initial load of the site, I am getting the raw html and appending "<style>MY STYLES HERE</style>" to the string before calling
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
This works perfectly, but if a user clicks a link on the page and it loads another page into the WebView, then this code does not get called and the style tag is lost.
I assume I need to override "shouldOverrideUrlLoading" in the WebViewClient, but I don't know how to intercept the html from here. I thought I would try something like:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String rawHtml = getRawHtml(url) + "<style>...</style>";
wv.loadDataWithBaseURL(url, rawHtml, null, "UTF-8", url);
}
But this obviously sends it into an endless loop of intercepting the load to start a new load.
I have also tried overriding onPageFinished and doing:
wv.loadUrl("javascript:(function() { ... })()");
which works, except that it waits until the entire page is loaded before executing. This causes the page to appear loaded with all of the UI elements in tact, and then all of the ones I am trying to hide suddenly disappear. My ultimate goal is to enhance the look and feel of the site on a mobile device, so this is not an option.
Is there something else I can do in "shouldOverrideUrlLoading" to inject style tags? Or if not, what else can I try?
I've run into this problem, and depending on the number of redirects, etc, we have not been able to make the injected JavaScript available all the time.
At minimum, you should use the wv.loadUrl("javascript:(function() { ... })()"); approach, but call it in both onPageStarted() and onPageFinished().
Depending on the complexity of your pages, you might need to inject the JavaScript in onLoadResource() as well.

Categories

Resources