addJavascriptInterface() does not inject java object
Reported by
ed.bake...@gmail.com,
Jul 19 2017
|
||||
Issue descriptionTHIS TEMPLATE IS FOR FILING BUGS ON THE ANDROID SYSTEM WEBVIEW. GENERAL WEB BUGS SHOULD BE FILED USING A DIFFERENT TEMPLATE! Device name: BlackBerry Priv Android version: 6.0.1 WebView version (from system settings -> Apps -> Android System WebView): 59.0.3071.125 Application: n/a Application version: n/a URLs (if applicable): Steps to reproduce: (1) Create a WebView (2) Use addJavascriptInterface() to inject a Java object (3) Load data into WebView with <img> element in the html (4) When the page has loaded enable Javascript (5) Attempt to access the injected Java object in the DOM Expected result: (6) The injected Java object should be accessible in the DOM Actual result: (6) The injected Java object is not accessible in the DOM. Consistent behaviour expected, the same html data without an <img> element will have the Java object accessible in the DOM. Notes: A reduced test case apk and source code is attached, the test case shows the difference in behaviour with html content with and without an <img> element in two WebViews
,
Jul 20 2017
This is indeed weird but I think this is just a timing issue in an unsupported order of operations. You're waiting for onPageFinished, and loading different content is going to change the timing of how long that takes. The order of operations with JS interface injection has always been significant (since it doesn't take effect until after a navigation) - I don't think it's particularly surprising that trying to enable JS after the fact doesn't work reliably, and I think it's just a fluke of timing that it works in the case without the <img> tag. Enable JS first, before calling addJavascriptInterface or navigating.
,
Jul 20 2017
I agree it's really an edge case, but I have been able to reproduce this with all sorts of long and short html content. The key difference was just adding a single <img> would cause the Java object to not get injected into the DOM.
,
Jul 20 2017
<img> typically involves an external resource (even though yours doesn't), and so I can believe that is specifically going to delay onPageFinished as that waits for external resource loads to complete. As I said, the surprising thing here is that it works in *any* case, not that it doesn't work when you have an <img> :)
,
Jul 21 2017
torne@, would a documentation change be in order for addJavascriptInterface? These orders *seem* ok: * setJavaScriptEnabled, addJavascriptInterface, loadData * addJavascriptInterface, setJavaScriptEnabled, loadData While this order is bad: * addJavascriptInterface, loadData, setJavaScriptEnabled For simplicity, we could just say that setJavaScriptEnabled should always be called before addJavascriptInterface, and not promise anything for other orders.
,
Aug 23 2017
I resolved this by adding a note in the docs for addJavascriptInterface. I changed the code example to specifically call WebSettings#enableJavaScript() as well as mentioning this in the text of the docs. Nothing to verify here, we just have to wait a long time (best guess is ~1 year) until the public docs get the update.
,
Aug 24
Bulk edit: marking stale 'fixed' bugs as 'verified' since they don't need verification at this point. |
||||
►
Sign in to add a comment |
||||
Comment 1 by ntfschr@chromium.org
, Jul 20 2017Owner: ntfschr@chromium.org
Status: Assigned (was: Unconfirmed)