Android Question ShouldInterceptRequest: ignore URLs in UltimateWebView2

peacemaker

Expert
Licensed User
Longtime User
HI, All and @Ivica Golubovic

How to prevent loading the URLs that are intercepted by ShouldInterceptRequest ?

How to prevent AJAX calls ?
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
How to prevent loading the URLs that are intercepted by ShouldInterceptRequest ?

you serve your own message to the caller in place of the requested resource. think of it as a kind of 404 error.

How to prevent AJAX calls ?

the only way you can prevent requests to your server is to turn the server off.
you can deny service, not prevent it. ajax calls are simply http requests
made by a browser using javascript. you can deny them just as you can deny
any request to your server (eg, by ip address, by user, by token, etc)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
AJAX calls from a web-page in the WebView are being done to external servers. It needs to prevent them.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
ajax is built into webview. the only way to stop it would be to turn off javascript. or to turn off
internet connection.

who wrote the webview page that contains the ajax calls? why did he do that if he doesn't
want to allow ajax?

intercepting outgoing calls with webviewclient (ie, UltimateWebView2 or similar) might be
an option. eg, any calls not local or not going to a specific external server would be
intercepted and a message returned to caller indicating that such calls are not allowed.

i still don't understand how users are able to make their own ajax calls with the webview page
unless the page is specifically enabling it. that makes it difficult to stop them from doing what
they want.

in any case, a webviewclient handles a webview's heavy jobs behind the scenes. it
checks to see it it's supposed to intercept loading the request page. if it has been given no
reasons to intercept the loading, it passes the loading request on to its next task. otherwise,
it loads a custom page and passes it back to the caller instead of the requested url.
i don't know how ultimatewebview2 does that, but that's how intercept works. basically you
need to define the conditions for interception and the custom message.to be loaded.
(by the way, the author has self-deprecated ultimatewebview2 in favor of his webkitlibrary,
so you might need to look at that in any case).
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
shouldoverrideurlloading might also work, but it depends on how users are able to make ajax calls. shouldoverrideurlloading works with "<a>" tags that the user clicks on. ajax triggers shouldintercept (i've tested it).

you've provided a description of what you're trying to do, but you've posted no code. i don't understand the flow of your app. i don't understand the purpose of the webpage.

shouldoverride and shouldintercept are similar but they are triggered differently. technically, your webviewclient can invoke both, or one, or neither. a full webviewclient will, of course, be configured to handle both methods.

what does your webpage do?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Just trying to show a web-page without ads.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i believe the only way to come close would be to intercept calls to any domain other than the webpage's. (the site might use 3rd party content that wasn't an ad; that content would be blocked as well.)

update: seems to work. find your library of choice and implement its shouldintercept method. shouldoverride would apply only if you could see the ad and clicked on it. in my test, 3rd party sites were intercepted, but it wasn't clear from their url's that they were all actually ads. sites can use cdn's for their own resources, and those cdn's are blocked.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4X:
 Dim Empty_Stream As InputStream
 Dim Empty_array (2) As Byte
 Empty_array(0) = 32
 Empty_array(1) = 32
 Empty_Stream.InitializeFromBytesArray(Empty_array, 0, 2)


Private Sub wv_ShouldInterceptRequest (Request As WebResourceRequest) As WebResourceResponse 'Works from API level 11 and above. From Api level 11 to 20, passed WebResourceRequest contains only Url.
    'Log("ShouldInterceptRequestUrl = " & Request.Url)
    If Request.Url.ToLowerCase.Contains(root_url) = False Then
        Dim Response As WebResourceResponse
        Response.Initialize2("text/html", "utf-8", 404, "Not found", Null, Empty_Stream)
        Return Response
    End If
    Return Null
End Sub

Such filtering does not help with AJAX, as i can understand.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i don't know what your understanding of ajax is.
i don't know that ajax is being used.
i don't know that "root_url" or "wv" are correctly assigned. i can't see the code.
the sub you post looks correct. are you saying that it is never called?
i need to load the webpage you load and follow what it does and how it does it.
in my tests, i was able to intercept ajax calls. in any case, there are other ways to do what ajax does these days without using ajax.
i ran a couple of tests loading real websites with ads. first, my shouldintercept checked all loading and passed the url's on to b4a where i decided to pass or deny (just as you did). if your shouldintercept is never called, then something is wrong with it. it never intercepts anything? what is the site's url?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks for the discussion. I think, the topic may be just stopped, as no super need of it.
I'm not an expert in JS at all, and it was a request and just curious for me to try to filter. But actually it's not an ethical thing to filter the ads of 3rd party web-sites by a request. IMHO.
Web-site authors are trying to compensate money for free web-sites via ads.
 
Last edited:
Upvote 0
Top