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 8fdb37c commit 2863a64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class WebKitWebViewActivity: AppCompatActivity() {
this.mWebView.settings.setSupportMultipleWindows(true)
this.mWebView.settings.domStorageEnabled = true
this.mWebView.settings.javaScriptEnabled = true

// Disable text selection
// NOTE: `evaluateJavascript` cannot be set to run on page load in WebChromeClient,
// need to disable long click instead.
// Selection in text fields should still work
this.mWebView.setOnLongClickListener { true }

this.setContentView(this.mWebView)
this.mWebView.setWebViewClient(MyWebViewClient(this))
this.mWebView.setWebChromeClient(MyWebChromeClient(this))
Expand Down
14 changes: 14 additions & 0 deletions ios/Classes/AGWKWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,26 @@ class AGWKWebViewController: UIViewController, WKNavigationDelegate {
private let webView: WKWebView
private var result: URL?

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

0 comments on commit 2863a64

Please sign in to comment.