From 292b83b5a543349375b4c138a3f6fbb56036a2e2 Mon Sep 17 00:00:00 2001 From: Alexis Deprez Date: Sat, 23 Mar 2024 02:32:52 +0100 Subject: [PATCH] fix(#1): prevent webview reload when ua unchanged --- README.md | 13 ++++++++----- .../com/adeprez/capacitor/ua/UserAgentPlugin.java | 15 ++++++++++----- ios/Plugin/UserAgentPlugin.swift | 15 +++++++++++++-- package.json | 4 ++-- src/definitions.ts | 7 +++++-- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 16cf980..2acb6ea 100644 --- a/README.md +++ b/README.md @@ -53,14 +53,17 @@ Get the Webview's user agent ### set(...) ```typescript -set(options: { userAgent: string; }) => Promise +set(options: { userAgent: string; reloadOnChange?: boolean; }) => Promise ``` -Update the Webview user agent (Android and iOS only) +Update the Webview user agent (Android and iOS only). Set reloadOnChange +to true to reload the page when the user agent changes (default is false). +Starting from KITKAT Android version, changing the user-agent while loading +a web page causes WebView to initiate loading once again. -| Param | Type | -| ------------- | ----------------------------------- | -| **`options`** | { userAgent: string; } | +| Param | Type | +| ------------- | ------------------------------------------------------------- | +| **`options`** | { userAgent: string; reloadOnChange?: boolean; } | -------------------- diff --git a/android/src/main/java/com/adeprez/capacitor/ua/UserAgentPlugin.java b/android/src/main/java/com/adeprez/capacitor/ua/UserAgentPlugin.java index fc7f876..973d75b 100644 --- a/android/src/main/java/com/adeprez/capacitor/ua/UserAgentPlugin.java +++ b/android/src/main/java/com/adeprez/capacitor/ua/UserAgentPlugin.java @@ -1,15 +1,14 @@ package com.adeprez.capacitor.ua; import android.webkit.WebSettings; -import com.getcapacitor.Bridge; -import com.getcapacitor.BridgeActivity; -import com.getcapacitor.BridgeWebViewClient; import com.getcapacitor.JSObject; import com.getcapacitor.Plugin; import com.getcapacitor.PluginCall; import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; +import java.util.Objects; + @CapacitorPlugin(name = "UserAgent") public class UserAgentPlugin extends Plugin { @@ -30,8 +29,14 @@ public void get(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void set(PluginCall call) { - String ua = call.getString("userAgent", null); - settings.setUserAgentString(ua); + String newUA = call.getString("userAgent", null); + String ua = settings.getUserAgentString(); + if (!Objects.equals(newUA, ua)) { + settings.setUserAgentString(ua); + if (Boolean.TRUE.equals(call.getBoolean("reloadOnChange", false))) { + bridge.getWebView().reload(); + } + } call.resolve(); } diff --git a/ios/Plugin/UserAgentPlugin.swift b/ios/Plugin/UserAgentPlugin.swift index c336be8..33eabc0 100644 --- a/ios/Plugin/UserAgentPlugin.swift +++ b/ios/Plugin/UserAgentPlugin.swift @@ -20,8 +20,19 @@ public class UserAgentPlugin: CAPPlugin { @objc func set(_ call: CAPPluginCall) { DispatchQueue.main.async { - let ua = call.getString("userAgent") ?? nil - self.bridge?.webView?.customUserAgent = ua + let newUA = call.getString("userAgent") ?? nil + + guard let userAgent = newUA else { + call.reject("User agent cannot be nil") + return + } + if self.bridge?.webView?.customUserAgent != userAgent { + self.bridge?.webView?.customUserAgent = userAgent + let reloadOnChange = call.getString("reloadOnChange") ?? false + if reloadOnChange { + self.bridge?.webView?.reload() + } + } call.resolve() } } diff --git a/package.json b/package.json index 306b607..6fd7253 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adeprez/capacitor-user-agent", - "version": "0.0.4", + "version": "0.1.1", "description": "User Agent get/set/reset plugin for Capacitor", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", @@ -61,7 +61,7 @@ "typescript": "~4.1.5" }, "peerDependencies": { - "@capacitor/core": "^4.0.0 || ^5.0.0" + "@capacitor/core": ">=4.0.0" }, "prettier": "@ionic/prettier-config", "swiftlint": "@ionic/swiftlint-config", diff --git a/src/definitions.ts b/src/definitions.ts index a8f5404..0279aab 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -5,9 +5,12 @@ export interface UserAgentPlugin { get(): Promise<{ userAgent: string }>; /** - * Update the Webview user agent (Android and iOS only) + * Update the Webview user agent (Android and iOS only). Set reloadOnChange + * to true to reload the page when the user agent changes (default is false). + * Starting from KITKAT Android version, changing the user-agent while loading + * a web page causes WebView to initiate loading once again. */ - set(options: { userAgent: string }): Promise; + set(options: { userAgent: string; reloadOnChange?: boolean }): Promise; /** * Reset the Webview user agent (Android and iOS only)