diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostRenderer.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostRenderer.java index c7157a096ebe..d5ce0cea398c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostRenderer.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostRenderer.java @@ -48,6 +48,7 @@ */ public class ReaderPostRenderer { private static final String JAVASCRIPT_MESSAGE_HANDLER = "wvHandler"; + private static final String JS_OBJECT_ADDED_TAG = "jsObjectAdded"; private final ReaderResourceVars mResourceVars; private final ReaderPost mPost; private final int mMinFullSizeWidthDp; @@ -645,6 +646,11 @@ private String getContentTextProperties() { } private void setWebViewMessageHandler(@NonNull WebView webView) { + Object tag = webView.getTag(JS_OBJECT_ADDED_TAG.hashCode()); + if (tag != null && (boolean) tag) { + return; // Exit if the object has already been added + } + Set allowedOrigins = new HashSet<>(); allowedOrigins.add("*"); @@ -665,6 +671,9 @@ private void setWebViewMessageHandler(@NonNull WebView webView) { } return null; }); + + // Set the tag that the JS object has been added, so we can check before adding it again + webView.setTag(JS_OBJECT_ADDED_TAG.hashCode(), true); } void setPostMessageListener(@Nullable ReaderPostMessageListener listener) {