Android Question How disable alert message in Webview?

JohnC

Well-Known Member
Licensed User
I would do a google search to see if there is any javascript that will disable an alert window in browsers.

Then use Webviewextras to inject that javascript into the page after it has loaded into webview:
B4X:
wve.executeJavascript(wv, Javascript)
 

drgottjr

Well-Known Member
Licensed User
the javascript to disable alert() is:
window.alert = "";
follow the instructions provided above. it may or may not work after a page has been loaded. also, don't forget: a webview is a subset of a browser. not everything supported in a browser is supported in a webview.
 

JohnC

Well-Known Member
Licensed User
also, don't forget: a webview is a subset of a browser. not everything supported in a browser is supported in a webview.
Good point, so I would also add the ChromeClient to the webview to increase it's compatibility with normal browsers:
B4X:
wve.addWebChromeClient(wv,"WVE")
 

miguelcleman

Member
Licensed User
I did

window.alert = ""; in wve.executeJavascript(wv, Javascript)

wve.addWebChromeClient(wv,"WVE")

but it didn't work.

There is another way?

Thanks.
 

JohnC

Well-Known Member
Licensed User
Please post your exact code so we can make sure everything is in the correct order.
 

drgottjr

Well-Known Member
Licensed User
what, exactly, did you do? what, exactly, happened when you did it? what did you do it to? assuming you did everything correctly, i believe the issue has to do with trying to modify a default behavior after the fact (ie, after the page has been loaded)
 

miguelcleman

Member
Licensed User
My code is :

WebViewExtras1.ExecuteJavascript("window.alert = "&Chr(34)&Chr(34)&";")

I can run another javascript, like open a alert dialog, and all is fine, but disable a alert from page loaded i can not.

Thanks.
 

JohnC

Well-Known Member
Licensed User
Some things to check:

1) Make sure you are adding in the chromeclient before you load in the webpage
2) Try this line of code instead:

B4X:
WebViewExtras1.ExecuteJavascript("window.alert = """";")
3) Also, make sure the code in above item #2 is in the wv_pagefinished event sub

Also, does the alert window pop up as soon as you load in the page or do you need to tap on something on the page to display the alert? I ask because if the alert gets displayed as soon as the page load, then a different solution is needed because the above solution can only intercept alerts that a user initiates AFTER the page is fully loaded and not during a page_load event.
 

drgottjr

Well-Known Member
Licensed User
so, it does work. at least on a simple test. here's the deal with alert() and webview: by default, webview does not show alerts. in order to enable alerts, you have to add the webviewextras.addWebChromeClient() method. of course, by not adding webviewextras.addWebChromeClient(), you may miss out on other things, but at least you won't see alerts.

second, if you do use webviewextras.addWebChromeClient(), then the javascript interface is necessary. when you inject the necessary javascript to disable alerts, it will disable them, but there will be a small error thrown to your b4a log. the user will not see this, nor will she see the alert.

please add the attached "alert.txt" sample to your Files folder (and inform the IDE). it's test html page, but due to security reasosns, we're not allowed to upload it as such; we have to change the name. you can leave it as ".txt"; webview doesn't care or know. also had the following snippets to your project:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim webview As WebView
    Dim wvx As WebViewExtras
    Dim btn As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    webview.Initialize("wv")
    Activity.AddView(webview, 0%x,0%y,100%x,100%y)
    wvx.addJavascriptInterface(webview, "jsi")
'    wvx.addWebChromeClient(webview, "cc")
   
    btn.Initialize("btn")
    Activity.AddView(btn, 80%x, 80%y, 15%x, 10%y)
    btn.Text = "tap me"
   
    webview.LoadHtml( File.ReadString( File.DirAssets, "alert.html"))
    btn.BringToFront   
End Sub

Sub btn_Click
    wvx.executeJavascript( webview, "window.alert=''" )
End Sub
what should happen (at least it happens when i run it on my device) is the follow: a webview will appear with a tiny button telling you to click it. at the bottom of the webview there will be another bottom that says "tap me". the button at the top is in the html document loaded by the webview. the bottom at the bottom is a b4a button view.

tap the button at the top. there should be no alert. tap it again if you like. there should be no alert. no need to tap the button at the bottom, since there is no alert in any case.

now, go to the line in the b4a project that has commented out the addition of chromeclient:
B4X:
'    wvx.addWebChromeClient(webview, "cc")
uncomment the line, thus activing alert

recompile and run the project. tap on the top button. you should now see an alert. tap again. alert.
now tap on the bottom button to inject the disabling javascript into the webview page.
now tap the button at the top again. alert gone. BUT there will be an error in the log.
note: pay close attention to the injected javascript. it's 2 single quotes inside a pair of double quotes:
wvx.executeJavascript( webview, "window.alert=''" )
 

Attachments

miguelcleman

Member
Licensed User
Hi,

The alert gets displayed as soon as the page load (I do not click in nothing).

My code :

WebViewExtras1.Initialize(WEBVIEW1)
WebViewExtras1.JavaScriptEnabled = True
JavascriptInterface1.Initialize
WebViewExtras1.addJavascriptInterface(JavascriptInterface1, "B4A")
WebChromeClient1.Initialize("WebChromeClient1")
WebViewExtras1.SetWebChromeClient(WebChromeClient1)

My WebViewExtras library do not have the addWebChromeClient option

I use the library WebViewExtras2 (Version: 2.20)

Thanks
 

JohnC

Well-Known Member
Licensed User
OK, since the alert happens when you load the page, trying to insert/run javascript code AFTER the page loads won't work.

So, you probably need to modify the page's HTML BEFORE is loads into the webview, and you can try this very theoretical method:

1) Instead of letting the webview control load the webpage (that pops up the immediate alert window), use okHTTPUtils2 to download the webpage into a string variable.

Then do one of the two below methods:

2) Search in the string for the places were the javascript pops up the alert and remove it from the string using something like "HTML.Replace("windows.alert(""Pop up"")","") or
3) Add the javascript code in post #3 as inline code in the HTML so it will run as the page loads.

4) Then load the modified HTML code inside the webview with .LoadHTML

Its a long shot, but the above might allow you to load and display the page in webview without the alert popping up.
 

drgottjr

Well-Known Member
Licensed User
my example works. not sure how you implemented it. good luck.
 

Brandsum

Well-Known Member
Licensed User
Adding window.alert="" could cause some JavaScript error in the webpage. So any other code after that alert function might not work as expected.

As alert is a function, you should override it with a blank function instead of blank string. On PageFinished event run this.
B4X:
wvx.executeJavascript( webview, "window.alert=function(e){};" )
 

miguelcleman

Member
Licensed User
The code below works to code from drgottjr (alert.txt) :

wvx.executeJavascript( webview, "window.alert=function(e){};" )

But, in my case, where I do not click nothing, the alert message just appears (code above not works).

I will try the ideia from JohnC.

Thanks
 

Brandsum

Well-Known Member
Licensed User
There is a way to suppress the alert dialog natively. I will post an example here tonight.
 

miguelcleman

Member
Licensed User
Finally,

This code works to me :

WebViewClient1.Initialize("WebViewClient1")
WebViewExtras1.SetWebViewClient(WebViewClient1)
WebViewExtras1.ExecuteJavascript("window.alert = function() {};")

p.s. : changeded "WebChromeClient1" to "WebViewClient1"

Thanks
 

Brandsum

Well-Known Member
Licensed User
Or you can do it like this.
B4X:
Dim jo As JavaObject
wvx.SetWebChromeClient(jo.InitializeContext.RunMethod("createChromeClient",Null))
   
   
   
#if java
import android.webkit.WebView;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;

public WebChromeClient createChromeClient(){
    WebChromeClient wbc = new WebChromeClient(){
        public boolean onJsAlert(WebView view, final String url, String message, JsResult result) {
            result.cancel();
            return true;
        }
    };
   
    return wbc;
}
#End If
 
Top