Skip to content

Commit

Permalink
fix(#1): prevent webview reload when ua unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Deprez committed Mar 23, 2024
1 parent bc20a28 commit 292b83b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ Get the Webview's user agent
### set(...)

```typescript
set(options: { userAgent: string; }) => Promise<void>
set(options: { userAgent: string; reloadOnChange?: boolean; }) => Promise<void>
```

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`** | <code>{ userAgent: string; }</code> |
| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** | <code>{ userAgent: string; reloadOnChange?: boolean; }</code> |

--------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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();
}

Expand Down
15 changes: 13 additions & 2 deletions ios/Plugin/UserAgentPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
set(options: { userAgent: string; reloadOnChange?: boolean }): Promise<void>;

/**
* Reset the Webview user agent (Android and iOS only)
Expand Down

0 comments on commit 292b83b

Please sign in to comment.