Skip to content

Commit

Permalink
Disable text selection in wkwebview authgear/authgear-server#3846
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Mar 7, 2024
1 parent 5e34306 commit 60565d4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
this.mWebView = new WebView(this);
this.mWebView.getSettings().setSupportMultipleWindows(true);
this.mWebView.getSettings().setDomStorageEnabled(true);

// Disable text selection
// NOTE: `evaluateJavascript` cannot be set to run on per navigation in WebChromeClient,
// need to disable long click instead.
// Selection in text fields should still work
this.mWebView.setOnLongClickListener(v -> true);

this.setContentView(this.mWebView);
this.mWebView.setWebViewClient(new MyWebViewClient(this));
this.mWebView.setWebChromeClient(new MyWebChromeClient(this));
Expand Down
14 changes: 14 additions & 0 deletions packages/authgear-capacitor/ios/Plugin/AGWKWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@ class AGWKWebViewController: UIViewController, WKNavigationDelegate {
}
}

private let disableUserSelectSource: String = """
document.documentElement.style.webkitUserSelect = 'none';
document.documentElement.style.userSelect = 'none';
"""

init(url: URL, redirectURI: URL, completionHandler: @escaping CompletionHandler) {
self.url = url
self.redirectURI = redirectURI
self.completionHandler = completionHandler

let configuration = WKWebViewConfiguration()

// Inject `user-select: none` style
let disableUserSelectScript = WKUserScript(
source: self.disableUserSelectSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false
)
configuration.userContentController.addUserScript(disableUserSelectScript)

self.webView = WKWebView(frame: .zero, configuration: configuration)
self.webView.translatesAutoresizingMaskIntoConstraints = false
self.webView.allowsBackForwardNavigationGestures = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
this.mWebView = new WebView(this);
this.mWebView.getSettings().setSupportMultipleWindows(true);
this.mWebView.getSettings().setDomStorageEnabled(true);

// Disable text selection
// NOTE: `evaluateJavascript` cannot be set to run per navigation in WebChromeClient,
// need to disable long click instead.
// Selection in text fields should still work
this.mWebView.setOnLongClickListener(v -> true);

this.setContentView(this.mWebView);
this.mWebView.setWebViewClient(new MyWebViewClient(this));
this.mWebView.setWebChromeClient(new MyWebChromeClient(this));
Expand Down
10 changes: 10 additions & 0 deletions packages/authgear-react-native/ios/AGWKWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ - (instancetype)initWithURL:(NSURL *)url redirectURI:(NSURL *)redirectURI comple
self.completionHandler = completionHandler;

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

// Inject `user-select: none` style
WKUserContentController *controller = [[WKUserContentController alloc] init];
NSString *disableUserSelectScript =
@"document.documentElement.style.webkitUserSelect = 'none';"
"document.documentElement.style.userSelect = 'none';";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:disableUserSelectScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
[controller addUserScript:userScript];
configuration.userContentController = controller;

self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
self.webView.translatesAutoresizingMaskIntoConstraints = false;
self.webView.allowsBackForwardNavigationGestures = YES;
Expand Down

0 comments on commit 60565d4

Please sign in to comment.