Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02e281a1 authored by Nate Fischer's avatar Nate Fischer
Browse files

WebView: clarify docs for addJavascriptInterface

Docs change only, no change in logic.

We do not support calling #addJavascriptInterface until after JavaScript
is enabled via WebSettings#setJavaScriptEnabled. Calling these methods
in the wrong order is undefined behavior (and we've seen that it's buggy
under certain conditions, e.g. if the DOM includes an <img> element).

This clarifies the point in the docs and code example.

Bug: 64899039
Test: make -j40 docs (everything looks good)
Change-Id: I8ef9eec7f038037e6b898286e4dad8a57ecad472
(cherry picked from commit aaef6827)
parent 07168bf4
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -1926,13 +1926,14 @@ public class WebView extends AbsoluteLayout
     * For applications targeted to API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
     * all public methods (including the inherited ones) can be accessed, see the
     * important security note below for implications.
     * <p> Note that injected objects will not
     * appear in JavaScript until the page is next (re)loaded. For example:
     * <p> Note that injected objects will not appear in JavaScript until the page is next
     * (re)loaded. JavaScript should be enabled before injecting the object. For example:
     * <pre>
     * class JsObject {
     *    {@literal @}JavascriptInterface
     *    public String toString() { return "injectedObject"; }
     * }
     * webview.getSettings().setJavaScriptEnabled(true);
     * webView.addJavascriptInterface(new JsObject(), "injectedObject");
     * webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
     * webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>