Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView detect click in iframe

I have a WebView where I am loading a javascript that loads some content. That content is an html with an iframe.

It seems any click within iframe is not triggering calls to WebViewClient#shouldOverrideUrlLoading(WebView view, String url); The webview has set both WebViewClient and WebChromeClient.

A work-around I can see of is to call getSettings().setSupportMultipleWindows(true) on the WebView and then within onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) assume the call was made within iframe and use some inserted javascript in order to get the iframe data. But that seems ugly to me.

So, the question: how do you detect a click within iframe loaded within the webview?

Thanks!

like image 501
gunar Avatar asked Oct 05 '22 02:10

gunar


1 Answers

I think I have found what is the problem: I broke the Same Origin Policy. A colleague that is doing web development pointed me in the right direction.

The whole document (containing the iFrame) has a different origin (combination of scheme, host name and port number) than containing iframe. As long as those frames (containing document and inner iframe) have different origins, they cannot communicate. That's why I am not getting calls to shouldOverrideUrlLoading.

EDIT: It seems there is a way to overpass above security policy, but that comes with accepted security risk: Add the following header in the response that contains the iframe: "Access-Control-Allow-Origin:*"

like image 91
gunar Avatar answered Oct 13 '22 12:10

gunar