Skip to content

Commit

Permalink
Add spoofWebview option to hide webview from UA
Browse files Browse the repository at this point in the history
  • Loading branch information
DuBistKomisch authored and deanylev committed Nov 18, 2024
1 parent e2d8429 commit 10996f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public class InAppBrowser extends CordovaPlugin {
private static final String FOOTER_COLOR = "footercolor";
private static final String BEFORELOAD = "beforeload";
private static final String FULLSCREEN = "fullscreen";
private static final String SPOOF_WEBVIEW = "spoofWebview";

private static final int TOOLBAR_HEIGHT = 48;

Expand Down Expand Up @@ -149,6 +150,7 @@ public class InAppBrowser extends CordovaPlugin {
private String footerColor = "";
private String beforeload = "";
private boolean fullscreen = true;
private boolean spoofWebview = false;
private String[] allowedSchemes;
private InAppBrowserClient currentClient;

Expand Down Expand Up @@ -715,6 +717,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
if (fullscreenSet != null) {
fullscreen = fullscreenSet.equals("yes") ? true : false;
}
String sw = features.get(SPOOF_WEBVIEW);
if (sw != null) {
spoofWebview = sw.equals("yes") ? true : false;
}
}

final CordovaWebView thatWebView = this.webView;
Expand Down Expand Up @@ -1001,6 +1007,9 @@ public void postMessage(String data) {
settings.setUserAgentString(settings.getUserAgentString() + " " + appendUserAgent);
}

if (spoofWebview) {
settings.setUserAgentString(settings.getUserAgentString().replaceAll("; wv", ""));
}
//Toggle whether this is enabled or not!
Bundle appSettings = cordova.getActivity().getIntent().getExtras();
boolean enableDatabase = appSettings == null ? true : appSettings.getBoolean("InAppBrowserStorageEnabled", true);
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVInAppBrowserOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
@property (nonatomic, assign) BOOL hidden;
@property (nonatomic, assign) BOOL disallowoverscroll;
@property (nonatomic, copy) NSString* beforeload;
@property (nonatomic, assign) BOOL spoofwebview;

+ (CDVInAppBrowserOptions*)parseOptions:(NSString*)options;

Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVInAppBrowserOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (id)init
self.toolbarcolor = nil;
self.toolbartranslucent = YES;
self.beforeload = @"";
self.spoofwebview = NO;
}

return self;
Expand Down
4 changes: 4 additions & 0 deletions src/ios/CDVWKInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@ - (void)createViews
) {
userAgent = [NSString stringWithFormat:@"%@ %@", userAgent, [self settingForKey:@"AppendUserAgent"]];
}
if (_browserOptions.spoofwebview) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(Mobile/[^ ]+)" options:0 error:nil];
userAgent = [regex stringByReplacingMatchesInString:userAgent options:0 range:NSMakeRange(0, userAgent.length) withTemplate:@" Version/11.0 $1 Safari/604.1"];
}
configuration.applicationNameForUserAgent = userAgent;
configuration.userContentController = userContentController;
#if __has_include(<Cordova/CDVWebViewProcessPoolFactory.h>)
Expand Down

0 comments on commit 10996f8

Please sign in to comment.