Skip to content

Commit

Permalink
feat: make possible to configure userAgent (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Nov 6, 2019
1 parent 4b8fc3e commit 8952ed1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
11 changes: 11 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ private void initWebView() {
if (Config.getBoolean("android.allowMixedContent", false)) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

String appendUserAgent = Config.getString("android.appendUserAgent" , Config.getString("appendUserAgent", null));
if (appendUserAgent != null) {
String defaultUserAgent = settings.getUserAgentString();
settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
}
String overrideUserAgent = Config.getString("android.overrideUserAgent" , Config.getString("overrideUserAgent", null));
if (overrideUserAgent != null) {
settings.setUserAgentString(overrideUserAgent);
}

String backgroundColor = Config.getString("android.backgroundColor" , Config.getString("backgroundColor", null));
try {
if (backgroundColor != null) {
Expand Down
4 changes: 3 additions & 1 deletion electron-template/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { app, BrowserWindow, Menu } = require('electron');
const isDevMode = require('electron-is-dev');
const { CapacitorSplashScreen } = require('@capacitor/electron');
const { CapacitorSplashScreen, configCapacitor } = require('@capacitor/electron');

const path = require('path');

Expand Down Expand Up @@ -40,6 +40,8 @@ async function createWindow () {
}
});

configCapacitor(mainWindow);

if (isDevMode) {
// Set our above template to the Menu Object if we are in development mode, dont want users having the devtools.
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplateDev));
Expand Down
13 changes: 13 additions & 0 deletions electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const injectCapacitor = async function(url) {
}
};

const configCapacitor = async function(mainWindow) {
let capConfigJson = JSON.parse(fs.readFileSync(`./capacitor.config.json`, 'utf-8'));
const appendUserAgent = capConfigJson.electron && capConfigJson.electron.appendUserAgent ? capConfigJson.electron.appendUserAgent : capConfigJson.appendUserAgent;
if (appendUserAgent) {
mainWindow.webContents.setUserAgent(mainWindow.webContents.getUserAgent() + " " + appendUserAgent);
}
const overrideUserAgent = capConfigJson.electron && capConfigJson.electron.overrideUserAgent ? capConfigJson.electron.overrideUserAgent : capConfigJson.overrideUserAgent;
if (overrideUserAgent) {
mainWindow.webContents.setUserAgent(overrideUserAgent);
}
}

class CapacitorSplashScreen {

/**
Expand Down Expand Up @@ -153,5 +165,6 @@ class CapacitorSplashScreen {

module.exports = {
injectCapacitor,
configCapacitor,
CapacitorSplashScreen
};
9 changes: 8 additions & 1 deletion ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
webViewConfiguration.userContentController = o

configureWebView(configuration: webViewConfiguration)


if let appendUserAgent = (capConfig.getValue("ios.appendUserAgent") as? String) ?? (capConfig.getValue("appendUserAgent") as? String) {
webViewConfiguration.applicationNameForUserAgent = appendUserAgent
}

webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView?.scrollView.bounces = false

Expand All @@ -84,6 +88,9 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
webView?.backgroundColor = UIColor(fromHex: backgroundColor)
webView?.scrollView.backgroundColor = UIColor(fromHex: backgroundColor)
}
if let overrideUserAgent = (bridge!.config.getValue("ios.overrideUserAgent") as? String) ?? (bridge!.config.getValue("overrideUserAgent") as? String) {
webView?.customUserAgent = overrideUserAgent
}
}

private func getStartPath() -> String? {
Expand Down
19 changes: 19 additions & 0 deletions site/docs-md/basics/configuring-your-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,18 @@ The current ones you might configure are:
"192.0.2.1"
]
},
// User agent of Capacitor WebView for iOS, Android and Electron, unless also declared inside ios, android or electron objects
"overrideUserAgent": "my custom user agent",
// String to append to the original user agent of Capacitor WebView for iOS, Android and Electron,
// unless also declared inside ios, android or electron objects. Only if overrideUserAgent is not set.
"appendUserAgent": "string to append",
// Background color of Capacitor WebView for both iOS and Android unless also declared inside ios or android objects
"backgroundColor": "#ffffffff",
"android": {
// User agent of Capacitor WebView for Android
"overrideUserAgent": "my custom user agent for Android",
// String to append to the original user agent of Capacitor WebView for Android.
"appendUserAgent": "string to append for Android",
// Background color of Capacitor WebView for Android only
"backgroundColor": "#ffffffff",
// On Android, if you are loading the app from a remote/testing server from https
Expand All @@ -95,6 +104,10 @@ The current ones you might configure are:
"webContentsDebuggingEnabled": true
},
"ios": {
// User agent of Capacitor WebView for iOS
"overrideUserAgent": "my custom user agent for iOS",
// String to append to the original user agent of Capacitor WebView for iOS.
"appendUserAgent": "string to append for iOS",
// Background color of Capacitor WebView for iOS only
"backgroundColor": "#ffffffff",
// Configure the Swift version to be used for Cordova plugins.
Expand All @@ -105,6 +118,12 @@ The current ones you might configure are:
"minVersion": "11.3",
// Some Cordova plugins require to configure the linker flags
"cordovaLinkerFlags": ["-ObjC"]
},
"electron": {
// User agent of Capacitor WebView for Electron
"overrideUserAgent": "my custom user agent for Electron",
// String to append to the original user agent of Capacitor WebView for Electron.
"appendUserAgent": "string to append for Electron",
}
}
```
Expand Down

0 comments on commit 8952ed1

Please sign in to comment.