@capgo/inappbrowser 8.2.0 → 8.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,53 @@
1
+ import Foundation
2
+ import ObjectiveC
3
+ import WebKit
4
+
5
+ /// Enables registering a `WKURLSchemeHandler` for built-in schemes like `http` and `https`.
6
+ ///
7
+ /// Apple's public `setURLSchemeHandler(_:forURLScheme:)` API throws for http/https because
8
+ /// `WKWebView.handlesURLScheme(_:)` returns `true` for them. This extension swizzles that
9
+ /// class method to return `false` for specified schemes, allowing the public API to work.
10
+ ///
11
+ /// This is the industry-standard approach used by DuckDuckGo Browser, TON Proxy, and others.
12
+ extension WKWebView {
13
+
14
+ /// Tracks which schemes have been overridden to allow custom handling
15
+ private static var _overriddenSchemes = Set<String>()
16
+
17
+ /// One-time swizzle — matches the pattern used by DuckDuckGo and ton-proxy-swift
18
+ private static let _swizzleOnce: Void = {
19
+ let original = class_getClassMethod(WKWebView.self, #selector(WKWebView.handlesURLScheme(_:)))
20
+ let swizzled = class_getClassMethod(
21
+ WKWebView.self,
22
+ #selector(WKWebView._capgo_handlesURLScheme(_:))
23
+ )
24
+
25
+ guard let original, let swizzled else {
26
+ print("[InAppBrowser][Proxy] WARNING: Could not get methods for swizzle")
27
+ return
28
+ }
29
+
30
+ method_exchangeImplementations(original, swizzled)
31
+ }()
32
+
33
+ /// Swizzled replacement — returns false for overridden schemes, calls original for all others
34
+ @objc(capgo_handlesURLScheme:)
35
+ private static func _capgo_handlesURLScheme(_ urlScheme: String) -> Bool {
36
+ if _overriddenSchemes.contains(urlScheme.lowercased()) {
37
+ return false
38
+ }
39
+ // After swizzle, this actually calls the ORIGINAL handlesURLScheme
40
+ return _capgo_handlesURLScheme(urlScheme)
41
+ }
42
+
43
+ /// Call this before `setURLSchemeHandler(_:forURLScheme:)` to allow registering for http/https.
44
+ static func enableCustomSchemeHandling(for schemes: [String]) {
45
+ // Add schemes BEFORE triggering the swizzle
46
+ for scheme in schemes {
47
+ _overriddenSchemes.insert(scheme.lowercased())
48
+ }
49
+
50
+ // Trigger the one-time swizzle
51
+ _ = _swizzleOnce
52
+ }
53
+ }
@@ -101,11 +101,13 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
101
101
  self.initWebview(isInspectable: isInspectable)
102
102
  }
103
103
 
104
- public init(url: URL, headers: [String: String], isInspectable: Bool, credentials: WKWebViewCredentials? = nil, preventDeeplink: Bool, blankNavigationTab: Bool, enabledSafeBottomMargin: Bool, enabledSafeTopMargin: Bool = true, blockedHosts: [String], authorizedAppLinks: [String]) {
104
+ public init(url: URL, headers: [String: String], isInspectable: Bool, credentials: WKWebViewCredentials? = nil, preventDeeplink: Bool, blankNavigationTab: Bool, enabledSafeBottomMargin: Bool, enabledSafeTopMargin: Bool = true, blockedHosts: [String], authorizedAppLinks: [String], proxyRequests: Bool = false, proxySchemeHandler: ProxySchemeHandler? = nil) {
105
105
  super.init(nibName: nil, bundle: nil)
106
106
  self.blankNavigationTab = blankNavigationTab
107
107
  self.enabledSafeBottomMargin = enabledSafeBottomMargin
108
108
  self.enabledSafeTopMargin = enabledSafeTopMargin
109
+ self.proxyRequests = proxyRequests
110
+ self.proxySchemeHandler = proxySchemeHandler
109
111
  self.source = .remote(url)
110
112
  self.credentials = credentials
111
113
  self.setHeaders(headers: headers)
@@ -153,6 +155,8 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
153
155
  var authorizedAppLinks: [String] = []
154
156
  var activeNativeNavigationForWebview: Bool = true
155
157
  var disableOverscroll: Bool = false
158
+ var proxyRequests: Bool = false
159
+ var proxySchemeHandler: ProxySchemeHandler?
156
160
 
157
161
  // Dimension properties
158
162
  var customWidth: CGFloat?
@@ -669,6 +673,19 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
669
673
  // Enable background task processing
670
674
  webConfiguration.processPool = WKProcessPool()
671
675
 
676
+ // Register proxy scheme handler if enabled
677
+ // Swizzles WKWebView.handlesURLScheme to allow registering for http/https via the public API
678
+ // This is the industry-standard approach used by DuckDuckGo Browser and others
679
+ if proxyRequests, let handler = proxySchemeHandler {
680
+ WKWebView.enableCustomSchemeHandling(for: ["https", "http"])
681
+ if !WKWebView.handlesURLScheme("https") && !WKWebView.handlesURLScheme("http") {
682
+ webConfiguration.setURLSchemeHandler(handler, forURLScheme: "https")
683
+ webConfiguration.setURLSchemeHandler(handler, forURLScheme: "http")
684
+ } else {
685
+ print("[InAppBrowser][Proxy] WARNING: handlesURLScheme swizzle failed; proxy scheme handler not registered")
686
+ }
687
+ }
688
+
672
689
  // Enable JavaScript to run automatically (needed for preShowScript and Firebase polyfill)
673
690
  webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true
674
691
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/inappbrowser",
3
- "version": "8.2.0",
3
+ "version": "8.3.0-alpha.1",
4
4
  "description": "Capacitor plugin in app browser",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -41,7 +41,8 @@
41
41
  "prettier": "prettier-pretty-check \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
42
42
  "swiftlint": "node-swiftlint",
43
43
  "docgen": "docgen --api InAppBrowserPlugin --output-readme README.md --output-json dist/docs.json",
44
- "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
44
+ "build:proxy-bridge": "esbuild src/proxy-bridge.ts --bundle --outfile=android/src/main/assets/proxy-bridge.js --format=iife --target=es2015",
45
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs && npm run build:proxy-bridge",
45
46
  "clean": "rimraf ./dist",
46
47
  "watch": "tsc --watch",
47
48
  "prepublishOnly": "npm run build"
@@ -56,6 +57,7 @@
56
57
  "@ionic/prettier-config": "^4.0.0",
57
58
  "@ionic/swiftlint-config": "^2.0.0",
58
59
  "@types/node": "^24.10.1",
60
+ "esbuild": "^0.27.3",
59
61
  "eslint": "^8.57.1",
60
62
  "eslint-plugin-import": "^2.31.0",
61
63
  "husky": "^9.1.7",