react-native-minimal-webview 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +130 -2
- package/android/build.gradle +26 -0
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +44 -0
- package/android/src/main/java/expo/modules/minimalwebview/DocumentEnd.kt +31 -0
- package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +496 -0
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +510 -0
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +359 -0
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +245 -0
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +199 -0
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +191 -0
- package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +113 -0
- package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +306 -0
- package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +76 -0
- package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +258 -0
- package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +44 -0
- package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +87 -0
- package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +77 -0
- package/build/WebView.d.ts +99 -0
- package/build/WebView.d.ts.map +1 -0
- package/build/WebView.js +448 -0
- package/build/WebView.js.map +1 -0
- package/build/bridge/createBridge.d.ts +32 -0
- package/build/bridge/createBridge.d.ts.map +1 -0
- package/build/bridge/createBridge.js +135 -0
- package/build/bridge/createBridge.js.map +1 -0
- package/build/bridge/endpoint.d.ts +35 -0
- package/build/bridge/endpoint.d.ts.map +1 -0
- package/build/bridge/endpoint.js +100 -0
- package/build/bridge/endpoint.js.map +1 -0
- package/build/bridge/origins.d.ts +25 -0
- package/build/bridge/origins.d.ts.map +1 -0
- package/build/bridge/origins.js +60 -0
- package/build/bridge/origins.js.map +1 -0
- package/build/bridge/pageClient.generated.d.ts +3 -0
- package/build/bridge/pageClient.generated.d.ts.map +1 -0
- package/build/bridge/pageClient.generated.js +4 -0
- package/build/bridge/pageClient.generated.js.map +1 -0
- package/build/bridge/protocol.d.ts +50 -0
- package/build/bridge/protocol.d.ts.map +1 -0
- package/build/bridge/protocol.js +52 -0
- package/build/bridge/protocol.js.map +1 -0
- package/build/defaultNavigationRules.d.ts +16 -0
- package/build/defaultNavigationRules.d.ts.map +1 -0
- package/build/defaultNavigationRules.js +23 -0
- package/build/defaultNavigationRules.js.map +1 -0
- package/build/index.d.ts +31 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +30 -0
- package/build/index.js.map +1 -0
- package/build/platform.generated.d.ts +702 -0
- package/build/platform.generated.d.ts.map +1 -0
- package/build/platform.generated.js +199 -0
- package/build/platform.generated.js.map +1 -0
- package/build/reactNativeWebViewDefaults.d.ts +21 -0
- package/build/reactNativeWebViewDefaults.d.ts.map +1 -0
- package/build/reactNativeWebViewDefaults.js +12 -0
- package/build/reactNativeWebViewDefaults.js.map +1 -0
- package/build/reactNativeWebViewMessages.d.ts +22 -0
- package/build/reactNativeWebViewMessages.d.ts.map +1 -0
- package/build/reactNativeWebViewMessages.js +30 -0
- package/build/reactNativeWebViewMessages.js.map +1 -0
- package/build/refStore.d.ts +31 -0
- package/build/refStore.d.ts.map +1 -0
- package/build/refStore.js +113 -0
- package/build/refStore.js.map +1 -0
- package/build/types.d.ts +1127 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.defaults.d.ts +8 -0
- package/build/types.defaults.d.ts.map +1 -0
- package/build/types.defaults.js +17 -0
- package/build/types.defaults.js.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/Asks.swift +45 -0
- package/ios/DelegateProxy.swift +162 -0
- package/ios/DocumentEnd.swift +23 -0
- package/ios/DownloadName.swift +12 -0
- package/ios/Downloads.swift +51 -0
- package/ios/Hooks.swift +308 -0
- package/ios/Loads.swift +44 -0
- package/ios/MRWExceptions.h +9 -0
- package/ios/MRWExceptions.m +10 -0
- package/ios/MRWInvoke.h +23 -0
- package/ios/MRWInvoke.m +187 -0
- package/ios/MinimalWebView+Dialogs.swift +99 -0
- package/ios/MinimalWebView+Navigation.swift +244 -0
- package/ios/MinimalWebView+Scripts.swift +92 -0
- package/ios/MinimalWebView.docc/MinimalWebView.md +13 -0
- package/ios/MinimalWebView.podspec +32 -0
- package/ios/MinimalWebView.swift +512 -0
- package/ios/MinimalWebViewModule.swift +110 -0
- package/ios/NavigationRules.swift +123 -0
- package/ios/OriginRules.swift +35 -0
- package/ios/PlatformCompletions.generated.swift +39 -0
- package/ios/PlatformSettings.swift +247 -0
- package/ios/ReadAccess.swift +19 -0
- package/ios/Recovery.swift +25 -0
- package/ios/SharedCookies.swift +59 -0
- package/package.json +94 -4
- package/src/WebView.tsx +623 -0
- package/src/bridge/createBridge.ts +176 -0
- package/src/bridge/endpoint.ts +119 -0
- package/src/bridge/origins.ts +62 -0
- package/src/bridge/pageClient.generated.ts +5 -0
- package/src/bridge/pageClient.ts +68 -0
- package/src/bridge/protocol.ts +78 -0
- package/src/defaultNavigationRules.ts +25 -0
- package/src/index.ts +37 -0
- package/src/platform.generated.ts +694 -0
- package/src/reactNativeWebViewDefaults.ts +13 -0
- package/src/reactNativeWebViewMessages.ts +32 -0
- package/src/refStore.ts +164 -0
- package/src/types.defaults.ts +24 -0
- package/src/types.ts +1194 -0
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,1127 @@
|
|
|
1
|
+
import type { Ref } from 'react';
|
|
2
|
+
import type { ViewProps } from 'react-native';
|
|
3
|
+
import type { BridgeConnection, CONNECTION } from './bridge/createBridge';
|
|
4
|
+
import type { AndroidSettingKeys, CGPoint, CGRect, CGSize, IOSSettingKeys, UIEdgeInsets } from './platform.generated';
|
|
5
|
+
/**
|
|
6
|
+
* What the WebView loads: a URL, or an HTML string.
|
|
7
|
+
* @group WebView
|
|
8
|
+
*/
|
|
9
|
+
export type WebViewSource = {
|
|
10
|
+
/**
|
|
11
|
+
* The URL to load.
|
|
12
|
+
*
|
|
13
|
+
* A `file://` URL reads only within `readAccessRoot` on iOS; Android needs `android.settings.allowFileAccess`,
|
|
14
|
+
* which lets a `file:` page read any file the app can.
|
|
15
|
+
*/
|
|
16
|
+
uri: string;
|
|
17
|
+
/** Sent with the first request only, not on later navigations. */
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
/**
|
|
20
|
+
* Makes the request a POST with this form-urlencoded body.
|
|
21
|
+
*
|
|
22
|
+
* Its `Content-Type` is `application/x-www-form-urlencoded` unless iOS `headers` set one; Android sends no
|
|
23
|
+
* `headers`.
|
|
24
|
+
* `reload()` sends the POST again; {@link ProcessGoneEvent.recovered | crash recovery} doesn't.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const source: WebViewSource = { uri: 'https://example.com/form', body: 'a=1&b=2' };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
body?: string;
|
|
31
|
+
/**
|
|
32
|
+
* For a `file://` `uri`, the `file://` directory the page may read, which must contain the file. Without it, or
|
|
33
|
+
* when it isn't a `file:` URL or doesn't contain the file (a warning says so), the file's directory.
|
|
34
|
+
* @ios
|
|
35
|
+
*/
|
|
36
|
+
readAccessRoot?: string;
|
|
37
|
+
} | {
|
|
38
|
+
/** The page's HTML. */
|
|
39
|
+
html: string;
|
|
40
|
+
/** The page's URL: its relative URLs resolve against it, and it has this origin. Without it, `about:blank`. */
|
|
41
|
+
baseUrl?: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* What happens to a navigation.
|
|
45
|
+
*
|
|
46
|
+
* - `allow`: it loads in place, on iOS a tapped universal link too (instead of opening the app that claims it).
|
|
47
|
+
* - `block`: it is cancelled.
|
|
48
|
+
* - `external`: `onOpenExternal`, or else the system, opens it.
|
|
49
|
+
* @group Navigation
|
|
50
|
+
*/
|
|
51
|
+
export type NavigationAction = 'allow' | 'block' | 'external';
|
|
52
|
+
/**
|
|
53
|
+
* A pattern over a navigation's URL and kind, and what the navigations it matches do.
|
|
54
|
+
*
|
|
55
|
+
* Every field it sets must match; for a list, one matching entry is enough. A field left out matches anything; a flag
|
|
56
|
+
* set to `false` matches only the requests without it.
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const navigationRules: NavigationRule[] = [
|
|
60
|
+
* { query: ['gad_source', 'utm_term=google'], action: 'block' }, // gad_source with any value, utm_term only =google
|
|
61
|
+
* { scheme: ['tel', 'mailto'], action: 'external' },
|
|
62
|
+
* { host: ['*.example.com'], path: ['/checkout'], action: 'external' }, // shop.example.com/checkouts too
|
|
63
|
+
* { host: ['example.com', '*.example.com'], action: 'allow' }, // *.example.com alone skips example.com
|
|
64
|
+
* ];
|
|
65
|
+
* ```
|
|
66
|
+
* @group Navigation
|
|
67
|
+
*/
|
|
68
|
+
export type NavigationRule = {
|
|
69
|
+
/** URL schemes without the `:` (`tel`), exact and case-insensitive. */
|
|
70
|
+
scheme?: string[];
|
|
71
|
+
/**
|
|
72
|
+
* Hosts: exact, or a `*.` suffix wildcard that matches subdomains only, not the domain itself.
|
|
73
|
+
*
|
|
74
|
+
* Case, port, userinfo and a trailing dot are ignored.
|
|
75
|
+
*/
|
|
76
|
+
host?: string[];
|
|
77
|
+
/**
|
|
78
|
+
* Path prefixes: plain prefixes of the percent-encoded URL path, not whole segments.
|
|
79
|
+
*
|
|
80
|
+
* A URL without an authority (`myapp:/open`) has the path `/`.
|
|
81
|
+
*/
|
|
82
|
+
path?: string[];
|
|
83
|
+
/**
|
|
84
|
+
* Query parameters, percent-encoded as in the URL and case-sensitive: a name matches that parameter whatever its
|
|
85
|
+
* value, and `name=value` only that exact value.
|
|
86
|
+
*/
|
|
87
|
+
query?: string[];
|
|
88
|
+
/**
|
|
89
|
+
* The main frame's navigation (true) or an iframe's (false).
|
|
90
|
+
*
|
|
91
|
+
* Android decides only an iframe's non-http(s) navigations, so a `mainFrame: false` rule for http(s) is iOS only.
|
|
92
|
+
*/
|
|
93
|
+
mainFrame?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* A `target=_blank` or `window.open` navigation, always a main-frame one.
|
|
96
|
+
*
|
|
97
|
+
* An allowed one loads in place: there is never a second WebView, so no `window.opener` (a native hook that takes
|
|
98
|
+
* `createWebViewWith` on iOS or `onCreateWindow` on Android owns real popups).
|
|
99
|
+
*
|
|
100
|
+
* One to `data:`, `blob:`, `javascript:`, `about:` or `file:` is always blocked (on Android, WebView may still run a
|
|
101
|
+
* `javascript:` one's script, with the page's origin), and one opened with no URL (`window.open('')`) is decided by
|
|
102
|
+
* the URL the page sends it to within 5 s, and dropped otherwise.
|
|
103
|
+
*
|
|
104
|
+
* One opened without a user gesture reaches the rules only with `javaScriptCanOpenWindowsAutomatically` on
|
|
105
|
+
* (`ios.preferences`, `android.settings`; off by default); otherwise WebKit or WebView drops it unreported.
|
|
106
|
+
*/
|
|
107
|
+
newWindow?: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* A user gesture started it: the user's tap, click or key press.
|
|
110
|
+
*
|
|
111
|
+
* - Android: WebView's flag (`hasGesture()`, `onCreateWindow`'s `isUserGesture`), Chromium's user activation, which
|
|
112
|
+
* a tap that doesn't scroll, a click or a key press gives the page for up to 5 s. Any navigation the page starts in
|
|
113
|
+
* that time is true: a `location.href` change or `window.open` from a click handler, a timer's, a link a script
|
|
114
|
+
* clicks.
|
|
115
|
+
* - iOS: only a link activation (`<a>`, `<area>`, `target=_blank` too) within 1 s after a tap on the WebView that
|
|
116
|
+
* scrolls and zooms nothing. WebKit reports a link a script clicks as it does a tapped one, so the core times the
|
|
117
|
+
* tap itself. A `location.href` change or `window.open` is false, from a click handler too, as is a link activated
|
|
118
|
+
* from a hardware keyboard; a tapped link's redirect is true only within that second. VoiceOver and Switch Control
|
|
119
|
+
* activate a link without a touch: while either runs, every link activation is true, a script's too.
|
|
120
|
+
*
|
|
121
|
+
* A redirect of a load the core starts itself (an allowed new window, and on Android an allowed `ask`) is never
|
|
122
|
+
* user-initiated.
|
|
123
|
+
*/
|
|
124
|
+
userInitiated?: boolean;
|
|
125
|
+
/**
|
|
126
|
+
* What the navigations it matches do; `ask` puts each to `onNavigationRequest`.
|
|
127
|
+
*
|
|
128
|
+
* On Android, which can't hold a navigation until JS answers, it works only for the main frame (elsewhere it is
|
|
129
|
+
* `block`): the navigation is cancelled, and an allowed one loads again after the answer, without the page's referrer
|
|
130
|
+
* policy or the gesture flag.
|
|
131
|
+
*/
|
|
132
|
+
action: NavigationAction | 'ask';
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* A navigation an `ask` rule puts to `onNavigationRequest`, or an `external` one to `onOpenExternal`.
|
|
136
|
+
* @group Navigation
|
|
137
|
+
*/
|
|
138
|
+
export type NavigationRequest = {
|
|
139
|
+
/** The URL it goes to. */
|
|
140
|
+
url: string;
|
|
141
|
+
/** Whether it's the main frame's, not an iframe's. */
|
|
142
|
+
mainFrame: boolean;
|
|
143
|
+
/** Whether it's a new-window navigation ({@link NavigationRule.newWindow}). */
|
|
144
|
+
newWindow: boolean;
|
|
145
|
+
/** Whether a user gesture started it ({@link NavigationRule.userInitiated}). */
|
|
146
|
+
userInitiated: boolean;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* A navigation decision, for `onNavigation`.
|
|
150
|
+
* @group Navigation
|
|
151
|
+
*/
|
|
152
|
+
export type NavigationEvent = NavigationRequest & {
|
|
153
|
+
/** Without `onOpenExternal`, or when it returns false, an `external` URL that no app opens is reported as `block`. */
|
|
154
|
+
action: NavigationAction;
|
|
155
|
+
/**
|
|
156
|
+
* Index of the matching rule in `navigationRules`, or in {@link defaultNavigationRules} when that is unset; null when
|
|
157
|
+
* no rule decided (none matched, a scheme that always loads in place or is always blocked, or a download opened
|
|
158
|
+
* externally).
|
|
159
|
+
*/
|
|
160
|
+
rule: number | null;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* A WebView's navigation state, as native reports it: from `useWebViewState` and `onNavigationStateChange`.
|
|
164
|
+
*
|
|
165
|
+
* While no WebView is mounted it is the initial state: `url` and `title` empty, `pendingUrl` and `outcome` null,
|
|
166
|
+
* `loading`, `canGoBack` and `canGoForward` false, `progress` 0.
|
|
167
|
+
* @group Navigation
|
|
168
|
+
*/
|
|
169
|
+
export type NavigationState = {
|
|
170
|
+
/**
|
|
171
|
+
* The page on screen: changes on commit and same-document navigation (pushState, replaceState, hash), and stays the
|
|
172
|
+
* old page while a load is in progress.
|
|
173
|
+
*
|
|
174
|
+
* After a failed load, the failed URL. Iframes never change it.
|
|
175
|
+
*/
|
|
176
|
+
url: string;
|
|
177
|
+
/**
|
|
178
|
+
* A main-frame load in progress, which may still fail or be blocked.
|
|
179
|
+
*
|
|
180
|
+
* Iframes never change it. Always null on Android with a WebView older than 146.
|
|
181
|
+
*/
|
|
182
|
+
pendingUrl: string | null;
|
|
183
|
+
title: string;
|
|
184
|
+
loading: boolean;
|
|
185
|
+
/** From 0 to 1. */
|
|
186
|
+
progress: number;
|
|
187
|
+
canGoBack: boolean;
|
|
188
|
+
canGoForward: boolean;
|
|
189
|
+
/**
|
|
190
|
+
* How the last main-frame load ended; null while one is loading, and before the first.
|
|
191
|
+
*
|
|
192
|
+
* - `failed`: a network or SSL error, or a renderer crash, cut it off.
|
|
193
|
+
* - `cancelled`: it never showed a page (a stop before the response, a download, a 204, a redirect the rules blocked,
|
|
194
|
+
* a load a newer one replaced); a stop after the page arrived is `cancelled` on iOS but `finished` on Android, and
|
|
195
|
+
* a 204 is `finished` on iOS 17.
|
|
196
|
+
*
|
|
197
|
+
* An HTTP error status (404) is `finished`: the site's page shows, and `onError` gets an `http` error. On Android
|
|
198
|
+
* with a WebView older than 146, a load that never commits changes nothing here.
|
|
199
|
+
*/
|
|
200
|
+
outcome: 'finished' | 'failed' | 'cancelled' | null;
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* A main-frame error, for `onError`.
|
|
204
|
+
*
|
|
205
|
+
* - `network` and `ssl`: the load failed, no page arrived; an SSL error always cancels the load. `code` is the
|
|
206
|
+
* platform's own: on iOS the `NSError` code (an `NSURLError*` constant, usually), on Android `WebViewClient.ERROR_*`
|
|
207
|
+
* for `network` and `SslError.SSL_*` for `ssl`.
|
|
208
|
+
* - `http`: the response had an HTTP error status (400+), and the site's own error page shows.
|
|
209
|
+
* @group Events
|
|
210
|
+
*/
|
|
211
|
+
export type LoadErrorEvent = {
|
|
212
|
+
kind: 'network' | 'ssl';
|
|
213
|
+
url: string;
|
|
214
|
+
code: number;
|
|
215
|
+
description: string;
|
|
216
|
+
} | {
|
|
217
|
+
kind: 'http';
|
|
218
|
+
url: string;
|
|
219
|
+
statusCode: number;
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* The renderer process died, for `onProcessGone`.
|
|
223
|
+
* @group Events
|
|
224
|
+
*/
|
|
225
|
+
export type ProcessGoneEvent = {
|
|
226
|
+
/**
|
|
227
|
+
* Why it died: `crash` when the renderer crashed, `memory` when the system killed it to reclaim memory, and `unknown`
|
|
228
|
+
* on iOS, where WebKit doesn't say.
|
|
229
|
+
*/
|
|
230
|
+
cause: 'crash' | 'memory' | 'unknown';
|
|
231
|
+
/**
|
|
232
|
+
* Whether the core reloads the page: on iOS in place; on Android in a new WebView, once the view's window is visible,
|
|
233
|
+
* with the history restored unless the `source` page was on screen, and with the props applied but not what `call`
|
|
234
|
+
* set.
|
|
235
|
+
*
|
|
236
|
+
* False while a POST `source` is on screen, which, as in a browser, isn't sent again unasked (`reload()` sends it),
|
|
237
|
+
* and after more than 3 crashes in 60 s, counted for the whole app on Android, until the next
|
|
238
|
+
* {@link WebViewRef.reload | reload()} (on iOS also a `source` change); pull-to-refresh doesn't lift it.
|
|
239
|
+
*/
|
|
240
|
+
recovered: boolean;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Runs at document start, before any page script and before the DOM is parsed.
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const script: InjectedScript = {
|
|
247
|
+
* code: "addEventListener('DOMContentLoaded', () => document.body.classList.add('in-app'));",
|
|
248
|
+
* origins: ['https://example.com'],
|
|
249
|
+
* };
|
|
250
|
+
* ```
|
|
251
|
+
* @group Scripts
|
|
252
|
+
*/
|
|
253
|
+
export type InjectedScript = {
|
|
254
|
+
code: string;
|
|
255
|
+
/**
|
|
256
|
+
* Runs it in every frame, iframes included.
|
|
257
|
+
* @default `false`
|
|
258
|
+
*/
|
|
259
|
+
allFrames?: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* The origins whose frames run it, in the syntax of {@link BridgeOptions.allowedOrigins}. Each frame is matched by
|
|
262
|
+
* its own `location.origin`, so a listed page's iframes from other origins don't run it; about:blank and srcdoc
|
|
263
|
+
* frames have their parent's origin. `'*'` matches every origin, opaque ones (`data:`, sandboxed frames) and
|
|
264
|
+
* `file:` included; `[]` matches none.
|
|
265
|
+
*
|
|
266
|
+
* Without `'*'` in the list, the code runs inside a function: its top-level `let`, `const`, `class`, `function` and
|
|
267
|
+
* `var` stay local to it, so put what the page should see on `window`. An invalid rule throws when the WebView
|
|
268
|
+
* renders.
|
|
269
|
+
*/
|
|
270
|
+
origins: string[];
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* Who sent a call or event: the page's origin as `location.origin` writes it (`https://example.com:8443`), checked by
|
|
274
|
+
* native code, not taken from the page.
|
|
275
|
+
*
|
|
276
|
+
* Every allowed origin gets the same trust; a handler scopes itself with this.
|
|
277
|
+
* @group Bridge
|
|
278
|
+
*/
|
|
279
|
+
export type BridgeSender = {
|
|
280
|
+
origin: string;
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* Answers a bridge call: the return value (a promise is awaited) is the result.
|
|
284
|
+
*
|
|
285
|
+
* A throw or rejection rejects the call with the {@link ErrorCode} `handler_error` and the error's message.
|
|
286
|
+
* @group Bridge
|
|
287
|
+
*/
|
|
288
|
+
export type BridgeHandler<Params = unknown, Result = unknown> = (params: Params, sender: BridgeSender) => Result | Promise<Result>;
|
|
289
|
+
/**
|
|
290
|
+
* What crosses a bridge, for `createBridge<Schema>`: the methods each side handles, which the other side calls, and the
|
|
291
|
+
* events.
|
|
292
|
+
*
|
|
293
|
+
* With a schema, `handle` takes only the `reactNative` methods and `call` only the `page` ones, `emit` and `on` only its
|
|
294
|
+
* events (another name doesn't compile), and `params` or a payload is required where its type doesn't allow
|
|
295
|
+
* `undefined`. A method's `params` and `result` are left out when it has none. Every key is required: `{}` for a side
|
|
296
|
+
* that handles nothing. The method and event maps are `type`s or written inline, not `interfaces` (an interface has no
|
|
297
|
+
* index signature, so it doesn't satisfy `Record`).
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* type ShopBridge = {
|
|
301
|
+
* reactNative: { share: { params: { url: string }; result: boolean } };
|
|
302
|
+
* page: { getCartCount: { result: number } };
|
|
303
|
+
* events: { themeChanged: 'light' | 'dark' };
|
|
304
|
+
* };
|
|
305
|
+
* const bridge = createBridge<ShopBridge>({ allowedOrigins: ['https://example.com'] });
|
|
306
|
+
* ```
|
|
307
|
+
* @group Bridge
|
|
308
|
+
*/
|
|
309
|
+
export type BridgeSchema = {
|
|
310
|
+
/** The methods React Native answers with `bridge.handle`, and the page calls with `nativeBridge.call`. */
|
|
311
|
+
reactNative: Record<string, {
|
|
312
|
+
params?: unknown;
|
|
313
|
+
result?: unknown;
|
|
314
|
+
}>;
|
|
315
|
+
/** The methods the page answers with `nativeBridge.handle`, and React Native calls with `bridge.call`. */
|
|
316
|
+
page: Record<string, {
|
|
317
|
+
params?: unknown;
|
|
318
|
+
result?: unknown;
|
|
319
|
+
}>;
|
|
320
|
+
/** The events, by name and payload, that either side emits and the other listens to. */
|
|
321
|
+
events: Record<string, unknown>;
|
|
322
|
+
};
|
|
323
|
+
type ParamsOf<Method> = 'params' extends keyof Method ? Method['params' & keyof Method] : undefined;
|
|
324
|
+
type ResultOf<Method> = 'result' extends keyof Method ? Method['result' & keyof Method] : unknown;
|
|
325
|
+
/** `call`'s arguments after the method: `params`, required unless they can be `undefined`, then the options. */
|
|
326
|
+
type CallArgs<Method> = undefined extends ParamsOf<Method> ? [params?: ParamsOf<Method>, options?: {
|
|
327
|
+
timeoutMs?: number;
|
|
328
|
+
}] : [params: ParamsOf<Method>, options?: {
|
|
329
|
+
timeoutMs?: number;
|
|
330
|
+
}];
|
|
331
|
+
/** `emit`'s argument after the event: the payload, required unless it can be `undefined`. */
|
|
332
|
+
type PayloadArg<Payload> = undefined extends Payload ? [payload?: Payload] : [payload: Payload];
|
|
333
|
+
/**
|
|
334
|
+
* A promise-based RPC channel to `window.nativeBridge` in the page, typed by a {@link BridgeSchema}.
|
|
335
|
+
*
|
|
336
|
+
* Attach it with the `bridge` prop. Handlers and listeners belong to the bridge, not to a WebView: they stay across
|
|
337
|
+
* unmounts. Messages have no size limit.
|
|
338
|
+
*
|
|
339
|
+
* What it sends waits for the page's `DOMContentLoaded`, so the handlers and listeners of the page's synchronous and
|
|
340
|
+
* deferred scripts are in place, and never reaches a later page, even one on the same origin. The page's own calls
|
|
341
|
+
* don't wait: one made before React Native has registered its method rejects with `not_found`.
|
|
342
|
+
*
|
|
343
|
+
* A call or event made while no WebView is mounted with the bridge waits the same way. A call's timeout runs
|
|
344
|
+
* meanwhile, and only the latest 1000 are kept: an older call rejects with `unavailable`.
|
|
345
|
+
* Unmounting rejects the calls in flight with `navigated`.
|
|
346
|
+
*
|
|
347
|
+
* The page's `window.nativeBridge` has the same four methods, from its side: {@link PageBridge}.
|
|
348
|
+
* @see [Bridge example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#the-bridge)
|
|
349
|
+
* @group Bridge
|
|
350
|
+
*/
|
|
351
|
+
export type Bridge<Schema extends BridgeSchema = BridgeSchema> = {
|
|
352
|
+
/**
|
|
353
|
+
* Answers `nativeBridge.call(method, params)` from the page, for a method of the schema's `reactNative`.
|
|
354
|
+
*
|
|
355
|
+
* One handler per method: a later one replaces it. Returns an unsubscribe function, which removes only this handler.
|
|
356
|
+
*/
|
|
357
|
+
handle<M extends keyof Schema['reactNative'] & string>(method: M, handler: BridgeHandler<ParamsOf<Schema['reactNative'][M]>, ResultOf<Schema['reactNative'][M]>>): () => void;
|
|
358
|
+
/**
|
|
359
|
+
* Calls a method of the schema's `page`, which the page registered with `nativeBridge.handle`.
|
|
360
|
+
*
|
|
361
|
+
* Rejects with a {@link CodedError}: `timeout`, `not_found`, `handler_error`, `navigated` or `unavailable`.
|
|
362
|
+
* `timeoutMs` overrides the bridge's for this call, and one that isn't 0 or more rejects with a plain `RangeError`.
|
|
363
|
+
*/
|
|
364
|
+
call<M extends keyof Schema['page'] & string>(method: M, ...args: CallArgs<Schema['page'][M]>): Promise<ResultOf<Schema['page'][M]>>;
|
|
365
|
+
/**
|
|
366
|
+
* Fires an event the page listens to with `nativeBridge.on`.
|
|
367
|
+
*
|
|
368
|
+
* Dropped if the page goes away before it arrives, or if the page has no listener for it.
|
|
369
|
+
*/
|
|
370
|
+
emit<E extends keyof Schema['events'] & string>(event: E, ...payload: PayloadArg<Schema['events'][E]>): void;
|
|
371
|
+
/**
|
|
372
|
+
* Listens to `nativeBridge.emit(event, payload)` from the page. Returns an unsubscribe function.
|
|
373
|
+
*
|
|
374
|
+
* A listener that throws is reported as an unhandled promise rejection; the event's other listeners still run.
|
|
375
|
+
*/
|
|
376
|
+
on<E extends keyof Schema['events'] & string>(event: E, listener: (payload: Schema['events'][E], sender: BridgeSender) => void): () => void;
|
|
377
|
+
/** What `<WebView>` attaches to. Only `createBridge` sets it, so a hand-made or mock `Bridge` doesn't type-check. */
|
|
378
|
+
readonly [CONNECTION]: BridgeConnection;
|
|
379
|
+
};
|
|
380
|
+
/**
|
|
381
|
+
* The page's `window.nativeBridge`, for the page's own TypeScript: the same {@link BridgeSchema} as {@link Bridge}, read
|
|
382
|
+
* from the page's side.
|
|
383
|
+
*
|
|
384
|
+
* `handle` takes the schema's `page` methods, and its handler gets the params only (no sender), `call` takes the
|
|
385
|
+
* `reactNative` ones, and `emit` and `on` take the `events`. `call` rejects with a {@link CodedError}: `timeout`,
|
|
386
|
+
* `not_found`, `handler_error`, or `navigated` when the page comes back from the back-forward cache. Its timeout
|
|
387
|
+
* defaults to the bridge's `timeoutMs`, and a `timeoutMs` that isn't 0 or more rejects with a plain `RangeError`.
|
|
388
|
+
*
|
|
389
|
+
* `window.nativeBridge` exists only in the main frame of a page on one of the bridge's `allowedOrigins`, so a page
|
|
390
|
+
* declares it optional. The page's project needs this package as a dev dependency for the type, which is a type-only
|
|
391
|
+
* import and costs nothing at runtime, and must share or copy the schema type.
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* import type { PageBridge } from 'react-native-minimal-webview';
|
|
395
|
+
*
|
|
396
|
+
* declare global {
|
|
397
|
+
* interface Window {
|
|
398
|
+
* nativeBridge?: PageBridge<ShopBridge>;
|
|
399
|
+
* }
|
|
400
|
+
* }
|
|
401
|
+
* ```
|
|
402
|
+
* @see [Bridge example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#in-the-page)
|
|
403
|
+
* @group Bridge
|
|
404
|
+
*/
|
|
405
|
+
export type PageBridge<Schema extends BridgeSchema = BridgeSchema> = {
|
|
406
|
+
/** Answers `bridge.call(method, params)` from React Native. A later handler replaces it. Returns an unsubscribe function. */
|
|
407
|
+
handle<M extends keyof Schema['page'] & string>(method: M, handler: (params: ParamsOf<Schema['page'][M]>) => ResultOf<Schema['page'][M]> | Promise<ResultOf<Schema['page'][M]>>): () => void;
|
|
408
|
+
/** Calls a method React Native registered with `bridge.handle`: `call(method, params, { timeoutMs })`. */
|
|
409
|
+
call<M extends keyof Schema['reactNative'] & string>(method: M, ...args: CallArgs<Schema['reactNative'][M]>): Promise<ResultOf<Schema['reactNative'][M]>>;
|
|
410
|
+
/** Fires an event React Native listens to with `bridge.on`. */
|
|
411
|
+
emit<E extends keyof Schema['events'] & string>(event: E, ...payload: PayloadArg<Schema['events'][E]>): void;
|
|
412
|
+
/** Listens to `bridge.emit(event, payload)` from React Native. Returns an unsubscribe function. */
|
|
413
|
+
on<E extends keyof Schema['events'] & string>(event: E, listener: (payload: Schema['events'][E]) => void): () => void;
|
|
414
|
+
};
|
|
415
|
+
/**
|
|
416
|
+
* The options of `createBridge`.
|
|
417
|
+
* @group Bridge
|
|
418
|
+
*/
|
|
419
|
+
export type BridgeOptions = {
|
|
420
|
+
/**
|
|
421
|
+
* The origins whose pages get `window.nativeBridge`, main frame only: an origin, a `*.` wildcard that matches
|
|
422
|
+
* subdomains only (not the domain itself), an IP literal, or `'*'` for any origin, opaque ones too.
|
|
423
|
+
*
|
|
424
|
+
* Each takes an optional port. http or https, no path, ASCII (punycode) hosts; case is ignored, and a missing port
|
|
425
|
+
* means 80 or 443.
|
|
426
|
+
* @example
|
|
427
|
+
* ```ts
|
|
428
|
+
* createBridge({ allowedOrigins: ['https://example.com', 'https://*.example.com', 'http://[::1]:8080'] });
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
allowedOrigins: string[];
|
|
432
|
+
/**
|
|
433
|
+
* Per-call timeout in milliseconds, also the default for the page's `nativeBridge.call`; `0` or `Infinity` means
|
|
434
|
+
* none.
|
|
435
|
+
* @default `10000`
|
|
436
|
+
*/
|
|
437
|
+
timeoutMs?: number;
|
|
438
|
+
};
|
|
439
|
+
/**
|
|
440
|
+
* An `<input type="file">`.
|
|
441
|
+
* @android
|
|
442
|
+
* @group Page requests
|
|
443
|
+
*/
|
|
444
|
+
export type FileRequest = {
|
|
445
|
+
/** Its `accept` list as written: MIME types and extensions (`.pdf`). */
|
|
446
|
+
accept: string[];
|
|
447
|
+
/** Its `multiple` attribute. */
|
|
448
|
+
multiple: boolean;
|
|
449
|
+
/** Its `capture` attribute: the page wants the camera. */
|
|
450
|
+
capture: boolean;
|
|
451
|
+
};
|
|
452
|
+
/**
|
|
453
|
+
* A device resource a page can ask for.
|
|
454
|
+
*
|
|
455
|
+
* `geolocation` and `protectedMedia` (the device id DRM playback needs, Widevine) are Android only: on iOS, location
|
|
456
|
+
* always shows WebKit's own prompt, and FairPlay asks nothing.
|
|
457
|
+
* @group Page requests
|
|
458
|
+
*/
|
|
459
|
+
export type PermissionResource = 'camera' | 'microphone' | 'geolocation' | 'protectedMedia';
|
|
460
|
+
/**
|
|
461
|
+
* A page's permission request, for `onPermissionRequest`.
|
|
462
|
+
*
|
|
463
|
+
* Only the main frame asks (on Android, which doesn't name the frame, any frame of the top page's origin); other
|
|
464
|
+
* frames, and a page with an opaque origin (`data:`, a sandboxed frame), are denied without asking.
|
|
465
|
+
* @group Page requests
|
|
466
|
+
*/
|
|
467
|
+
export type PermissionRequest = {
|
|
468
|
+
/** The requesting page's origin, as `location.origin` writes it; every `file:` page is `file://`. */
|
|
469
|
+
origin: string;
|
|
470
|
+
resources: PermissionResource[];
|
|
471
|
+
};
|
|
472
|
+
/**
|
|
473
|
+
* A finished download.
|
|
474
|
+
* @group Page requests
|
|
475
|
+
*/
|
|
476
|
+
export type Download = {
|
|
477
|
+
ok: true;
|
|
478
|
+
/** A `file://` in the app's cache (iOS: the temporary directory), which the core never deletes. */
|
|
479
|
+
uri: string;
|
|
480
|
+
/**
|
|
481
|
+
* A plain file name, no path, but the page chose it, like the content and `mimeType`: don't open the file as
|
|
482
|
+
* trusted.
|
|
483
|
+
*/
|
|
484
|
+
filename: string;
|
|
485
|
+
mimeType: string;
|
|
486
|
+
/** The server URL the file came from; `null` when the page generated it (`data:` or `blob:` URL). */
|
|
487
|
+
url: string | null;
|
|
488
|
+
};
|
|
489
|
+
/**
|
|
490
|
+
* A download that could not be saved; `kind` says why.
|
|
491
|
+
*
|
|
492
|
+
* - `network`: connecting or reading the response failed, or a redirect was refused.
|
|
493
|
+
* - `http`: the response had an error status (Android only: iOS saves what the server sent).
|
|
494
|
+
* - `data`: the content: a bad `data:` or `blob:` URL, the page not reading its blob, an unsupported scheme, or
|
|
495
|
+
* (Android) a file above 100 MB or a `blob:` URL the main frame can't read (a cross-origin iframe's, or one already
|
|
496
|
+
* revoked).
|
|
497
|
+
* @group Page requests
|
|
498
|
+
*/
|
|
499
|
+
export type DownloadFailure = {
|
|
500
|
+
/** Tells it from a {@link Download}. */
|
|
501
|
+
ok: false;
|
|
502
|
+
kind: 'network' | 'data';
|
|
503
|
+
filename: string;
|
|
504
|
+
/** As in {@link Download}. */
|
|
505
|
+
url: string | null;
|
|
506
|
+
description: string;
|
|
507
|
+
} | {
|
|
508
|
+
ok: false;
|
|
509
|
+
kind: 'http';
|
|
510
|
+
filename: string;
|
|
511
|
+
url: string;
|
|
512
|
+
statusCode: number;
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* What `onDownload` gets.
|
|
516
|
+
* @group Page requests
|
|
517
|
+
*/
|
|
518
|
+
export type DownloadResult = Download | DownloadFailure;
|
|
519
|
+
/**
|
|
520
|
+
* What a platform setting can take: what its property or setter accepts. An object is one of the iOS structs.
|
|
521
|
+
* @group Platform settings
|
|
522
|
+
*/
|
|
523
|
+
export type SettingValue = boolean | number | string | null | UIEdgeInsets | CGPoint | CGSize | CGRect;
|
|
524
|
+
/**
|
|
525
|
+
* A settings root: its generated keys ({@link IOSSettingKeys}, {@link AndroidSettingKeys}, from the SDK headers), so a
|
|
526
|
+
* misspelled key fails to compile, and `other` for the keys they lack.
|
|
527
|
+
* @group Platform settings
|
|
528
|
+
*/
|
|
529
|
+
export type SettingsRoot<Keys> = Keys & {
|
|
530
|
+
/**
|
|
531
|
+
* Any other key the platform has (a newer SDK's, one the headers don't list), untyped. It is set as if it were at
|
|
532
|
+
* the root; a generated key can't go here.
|
|
533
|
+
*/
|
|
534
|
+
other?: Record<string, SettingValue | undefined> & {
|
|
535
|
+
[K in keyof Keys]?: never;
|
|
536
|
+
};
|
|
537
|
+
};
|
|
538
|
+
/**
|
|
539
|
+
* The private WebKit settings {@link IOSSettings} takes besides {@link IOSSettingKeys}, both off by default.
|
|
540
|
+
* @ios
|
|
541
|
+
* @group Platform settings
|
|
542
|
+
*/
|
|
543
|
+
export type IOSPrivateSettingKeys = {
|
|
544
|
+
/** `WKWebViewConfiguration` */
|
|
545
|
+
configuration: {
|
|
546
|
+
/** Lets a `file:` page read any URL. */
|
|
547
|
+
allowUniversalAccessFromFileURLs?: boolean;
|
|
548
|
+
};
|
|
549
|
+
/** `WKPreferences` */
|
|
550
|
+
preferences: {
|
|
551
|
+
/** Lets a `file:` page read other `file:` URLs. */
|
|
552
|
+
allowFileAccessFromFileURLs?: boolean;
|
|
553
|
+
};
|
|
554
|
+
};
|
|
555
|
+
/**
|
|
556
|
+
* WebKit settings by Objective-C property name.
|
|
557
|
+
*
|
|
558
|
+
* Any public property works, including ones this package has never heard of (under a root's `other`). The keys the SDK
|
|
559
|
+
* headers list autocomplete, with their `@since`, as do the {@link IOSPrivateSettingKeys}. A value must fit the
|
|
560
|
+
* property's type: enums and option sets by raw value (from {@link IOSConstants}), structs as objects, a `UIColor` as
|
|
561
|
+
* `#rrggbb` or `#rrggbbaa`, an `NSURL` as a string, `websiteDataStore` as `'default'` or `'nonPersistent'`.
|
|
562
|
+
*
|
|
563
|
+
* A key that doesn't fit, a private (`_`) one, or one the device's iOS lacks (its `@since` is newer) is skipped with a
|
|
564
|
+
* warning; nothing here can crash the app. So is a property UIKit declares without runtime metadata, such as
|
|
565
|
+
* `webView.opaque`: set it with `call('webView.setOpaque:', [false])`.
|
|
566
|
+
*
|
|
567
|
+
* A root's default keys are the package's, and yours go over them; a key the package doesn't set keeps WebKit's
|
|
568
|
+
* default, and a key you remove goes back to the value it had before you first set it. The ref's
|
|
569
|
+
* {@link WebViewRef.get | get} and {@link WebViewRef.call | call} take these roots, plus `websiteDataStore` and
|
|
570
|
+
* `httpCookieStore`.
|
|
571
|
+
*
|
|
572
|
+
* WebKit has no mixed-content setting: it applies its own policy.
|
|
573
|
+
* @example
|
|
574
|
+
* ```tsx
|
|
575
|
+
* <WebView
|
|
576
|
+
* source={{ uri: 'https://example.com' }}
|
|
577
|
+
* ios={{
|
|
578
|
+
* configuration: { dataDetectorTypes: IOSConstants.DataDetectorTypes.link },
|
|
579
|
+
* scrollView: { bounces: false, contentInset: { top: 0, left: 0, bottom: 80, right: 0 } },
|
|
580
|
+
* }}
|
|
581
|
+
* />
|
|
582
|
+
* ```
|
|
583
|
+
* @ios
|
|
584
|
+
* @see [Platform settings example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#platform-settings)
|
|
585
|
+
* @group Platform settings
|
|
586
|
+
*/
|
|
587
|
+
export type IOSSettings = {
|
|
588
|
+
/**
|
|
589
|
+
* `WKWebViewConfiguration`: read once, when the WKWebView is created; a change recreates the WebView (the page
|
|
590
|
+
* reloads and the navigation state starts over). It is a copy, so `call` on it changes nothing.
|
|
591
|
+
* @default `{ allowsInlineMediaPlayback: true }`
|
|
592
|
+
*/
|
|
593
|
+
configuration?: SettingsRoot<IOSSettingKeys['configuration'] & IOSPrivateSettingKeys['configuration']>;
|
|
594
|
+
/** `WKPreferences` (`configuration.preferences`), live. */
|
|
595
|
+
preferences?: SettingsRoot<IOSSettingKeys['preferences'] & IOSPrivateSettingKeys['preferences']>;
|
|
596
|
+
/** `WKWebpagePreferences` (`configuration.defaultWebpagePreferences`), live from the next navigation. */
|
|
597
|
+
webpagePreferences?: SettingsRoot<IOSSettingKeys['webpagePreferences']>;
|
|
598
|
+
/**
|
|
599
|
+
* The `WKWebView`, live.
|
|
600
|
+
* @default `{ inspectable: __DEV__ }`
|
|
601
|
+
*/
|
|
602
|
+
webView?: SettingsRoot<IOSSettingKeys['webView']>;
|
|
603
|
+
/**
|
|
604
|
+
* Its `UIScrollView`, live.
|
|
605
|
+
*
|
|
606
|
+
* `directionalLockEnabled` is on by default, as react-native-webview has it.
|
|
607
|
+
*/
|
|
608
|
+
scrollView?: SettingsRoot<IOSSettingKeys['scrollView']>;
|
|
609
|
+
};
|
|
610
|
+
/**
|
|
611
|
+
* Android settings by setter name without `set`.
|
|
612
|
+
*
|
|
613
|
+
* Any public setter works, including ones a newer SDK or androidx.webkit adds (under a root's `other`). The keys the
|
|
614
|
+
* SDK lists autocomplete, with their `@since`; the constants are in {@link AndroidConstants}. An `int` setter takes a
|
|
615
|
+
* number up to `0xFFFFFFFF`, truncated, so an ARGB color such as `0xFF202020` works.
|
|
616
|
+
*
|
|
617
|
+
* A key with no public setter for the value's type, or one this device's WebView lacks, is skipped with a warning;
|
|
618
|
+
* nothing here can crash the app. A root's default keys are the package's, and yours go over them; a key the package
|
|
619
|
+
* doesn't set, such as `overScrollMode`, keeps the platform's default. A key you remove goes back to the value it had
|
|
620
|
+
* before you first set it; one with no getter, such as `geolocationEnabled`, keeps its value until the WebView is
|
|
621
|
+
* recreated, with a warning.
|
|
622
|
+
*
|
|
623
|
+
* The ref's {@link WebViewRef.get | get} and {@link WebViewRef.call | call} take these roots, plus `cookieManager` and
|
|
624
|
+
* `webStorage`, the WebView profile's (`webView.profile`, the default one unless set); a key is the getter's name
|
|
625
|
+
* without `get` or `is`.
|
|
626
|
+
* @example
|
|
627
|
+
* ```tsx
|
|
628
|
+
* <WebView
|
|
629
|
+
* source={{ uri: 'https://example.com' }}
|
|
630
|
+
* android={{
|
|
631
|
+
* settings: { textZoom: 120, cacheMode: AndroidConstants.WebSettings.LOAD_NO_CACHE },
|
|
632
|
+
* webView: { overScrollMode: 2 },
|
|
633
|
+
* }}
|
|
634
|
+
* />
|
|
635
|
+
* ```
|
|
636
|
+
* @android
|
|
637
|
+
* @see [Platform settings example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#platform-settings)
|
|
638
|
+
* @group Platform settings
|
|
639
|
+
*/
|
|
640
|
+
export type AndroidSettings = {
|
|
641
|
+
/**
|
|
642
|
+
* `WebSettings` setters, then static `WebSettingsCompat` ones.
|
|
643
|
+
*
|
|
644
|
+
* Each new WebView starts with `javaScriptEnabled`, `domStorageEnabled`, `supportMultipleWindows` and
|
|
645
|
+
* `builtInZoomControls` on; `allowFileAccess`, `allowContentAccess`, `displayZoomControls` and
|
|
646
|
+
* `downloadFaviconsEnabled` off; `mixedContentMode` `MIXED_CONTENT_NEVER_ALLOW`; and, with WebView 146 and newer,
|
|
647
|
+
* `backForwardCacheEnabled` on. `useWideViewPort` and `loadWithOverviewMode` are on by default, as
|
|
648
|
+
* react-native-webview has them. `userAgentString: null` is WebView's default user agent.
|
|
649
|
+
*/
|
|
650
|
+
settings?: SettingsRoot<AndroidSettingKeys['settings']>;
|
|
651
|
+
/**
|
|
652
|
+
* `WebView` setters (static and inherited `View` ones too), then `WebViewCompat` ones, then `CookieManager`'s
|
|
653
|
+
* `acceptThirdPartyCookies`.
|
|
654
|
+
*
|
|
655
|
+
* `acceptThirdPartyCookies` is on by default, as react-native-webview has it. `webContentsDebuggingEnabled` is
|
|
656
|
+
* refused: it is process-wide, set with {@link AndroidWebView.setWebContentsDebuggingEnabled}.
|
|
657
|
+
*/
|
|
658
|
+
webView?: SettingsRoot<AndroidSettingKeys['webView']>;
|
|
659
|
+
};
|
|
660
|
+
/**
|
|
661
|
+
* An event a native hook sent with `emit` or `ask`.
|
|
662
|
+
* @typeParam N - The event's name, to type one hook's events.
|
|
663
|
+
* @typeParam P - Its payload.
|
|
664
|
+
* @example
|
|
665
|
+
* ```ts
|
|
666
|
+
* type PageLoaded = HookEvent<'pageLoaded', { count: number }>;
|
|
667
|
+
* ```
|
|
668
|
+
* @group Native hooks
|
|
669
|
+
*/
|
|
670
|
+
export type HookEvent<N extends string = string, P = Record<string, unknown>> = {
|
|
671
|
+
/** The name the hook sent it with, such as `pageLoaded`. */
|
|
672
|
+
name: N;
|
|
673
|
+
/** What the hook sent with it. */
|
|
674
|
+
payload: P;
|
|
675
|
+
};
|
|
676
|
+
/**
|
|
677
|
+
* A native hook's JS side, for {@link WebViewProps.extensions}: its key, its options, and what takes its events.
|
|
678
|
+
*
|
|
679
|
+
* A package with a native hook exports a function that returns one, so that an app turns the hook on by putting it in
|
|
680
|
+
* `extensions`.
|
|
681
|
+
* @typeParam E - The hook's events, to type `onEvent`: a union of {@link HookEvent}s, which nothing checks at runtime.
|
|
682
|
+
* @see [Native hooks guide](https://rasmuskard.github.io/react-native-minimal-webview/guides/native-hooks/)
|
|
683
|
+
* @group Native hooks
|
|
684
|
+
*/
|
|
685
|
+
export type HookExtension<E extends HookEvent = HookEvent> = {
|
|
686
|
+
/**
|
|
687
|
+
* The key the hook is registered under.
|
|
688
|
+
*
|
|
689
|
+
* A key no hook is registered under warns: pass a one-platform hook's extension on that platform only. Two extensions
|
|
690
|
+
* with one key warn in development, and the later one wins.
|
|
691
|
+
*/
|
|
692
|
+
key: string;
|
|
693
|
+
/**
|
|
694
|
+
* What the hook gets as its options. Null or false switches the hook off, as does leaving its extension out of
|
|
695
|
+
* `extensions`: it stays attached and gets null, but the core routes none of its delegates, clients or listeners,
|
|
696
|
+
* drops its events, answers its asks with null, and rejects `ref.hook` calls to its handlers with `not_found`.
|
|
697
|
+
* Undefined, the default, means `true`. Turning it on again gives it the new options.
|
|
698
|
+
*/
|
|
699
|
+
options?: unknown;
|
|
700
|
+
/**
|
|
701
|
+
* Each of the hook's events: it goes to the extension with the hook's key (the later of two with one key), and one no
|
|
702
|
+
* extension owns is dropped. For an `ask`, the return value (a promise is awaited) is the answer, and `undefined`, or
|
|
703
|
+
* a value JSON can't encode, answers null. `signal` is set only for an `ask`, and aborts when native stops waiting for
|
|
704
|
+
* the answer: an ask has no timeout, but one still open when the page goes away is answered null.
|
|
705
|
+
*
|
|
706
|
+
* The latest `onEvent` always takes the events: a render with new handlers sends native nothing.
|
|
707
|
+
*/
|
|
708
|
+
onEvent?(event: E, signal?: AbortSignal): unknown;
|
|
709
|
+
};
|
|
710
|
+
/**
|
|
711
|
+
* A fullscreen change, for `onFullscreenChange`.
|
|
712
|
+
* @android
|
|
713
|
+
* @group Events
|
|
714
|
+
*/
|
|
715
|
+
export type FullscreenChangeEvent = {
|
|
716
|
+
active: boolean;
|
|
717
|
+
};
|
|
718
|
+
/**
|
|
719
|
+
* Why a ref method or a bridge call rejected. Each code means the same on every method that rejects with it.
|
|
720
|
+
*
|
|
721
|
+
* - `timeout`: a bridge call got no answer in time.
|
|
722
|
+
* - `not_found`: nothing by that name answers: no bridge handler for the method, no attached hook `name` that is on
|
|
723
|
+
* and handles `method`, or no such root, key, method (on Android, one that takes these arguments) or selector (on
|
|
724
|
+
* this OS or WebView version too; a private `_` key of an iOS `get` too), or a malformed path.
|
|
725
|
+
* - `handler_error`: the handler of a bridge call, on either side, or of a `hook` call failed; the `message` is its
|
|
726
|
+
* error's.
|
|
727
|
+
* - `script_error`: `evaluate`'s script threw, rejected or didn't parse.
|
|
728
|
+
* - `native_error`: the platform refused or failed the call: a member the core owns (or a private selector for an iOS
|
|
729
|
+
* `call`), a key `get` can't read, wrong arguments (iOS), a completion handler `call` can't await, a native
|
|
730
|
+
* exception or `NSError`, or a result JSON can't carry (`NaN`).
|
|
731
|
+
* - `navigated`: the page or the WebView went away while the call was pending (a navigation, a crash, an unmount), so
|
|
732
|
+
* it may have run: retry only what is safe to repeat.
|
|
733
|
+
* - `unavailable`: the call didn't run: the WebView is gone or not mounted yet, is being rebuilt after its renderer
|
|
734
|
+
* died (Android), the bridge's queue was full and dropped it, or lacks the feature (the method's TSDoc names the
|
|
735
|
+
* Android System WebView it needs, or a compat method this WebView doesn't support).
|
|
736
|
+
*
|
|
737
|
+
* A programming error, such as an argument of the wrong type, can reject with a plain `TypeError` instead.
|
|
738
|
+
* @group WebView
|
|
739
|
+
*/
|
|
740
|
+
export type ErrorCode = 'timeout' | 'not_found' | 'handler_error' | 'script_error' | 'native_error' | 'navigated' | 'unavailable';
|
|
741
|
+
/**
|
|
742
|
+
* What a ref method or a bridge call rejects with: an `Error` whose `code` says why.
|
|
743
|
+
* @group WebView
|
|
744
|
+
*/
|
|
745
|
+
export type CodedError = Error & {
|
|
746
|
+
code: ErrorCode;
|
|
747
|
+
};
|
|
748
|
+
/**
|
|
749
|
+
* What the WebView loads, its settings, and the callbacks for its events and the page's requests.
|
|
750
|
+
*
|
|
751
|
+
* The ones from React Native's `ViewProps` (`style`, `testID`, `nativeID`, the accessibility props, layout and touch
|
|
752
|
+
* events) go to the native view.
|
|
753
|
+
*
|
|
754
|
+
* Every `on…` callback is read when its event or request comes, so adding, changing or removing one applies to the
|
|
755
|
+
* next, and a request without one gets the default described on its prop. One that answers a request and throws (or
|
|
756
|
+
* rejects) is logged with `console.error` and gives the refusing answer: `onNavigationRequest` `block`,
|
|
757
|
+
* `onOpenExternal` null (the core opens nothing), `onFileRequest` cancel, `onPermissionRequest` deny, an extension's
|
|
758
|
+
* `onEvent` null.
|
|
759
|
+
*
|
|
760
|
+
* The `signal` such a callback gets aborts when native stops waiting for its answer (the page moved on, crashed or
|
|
761
|
+
* unmounted), and a late answer is dropped.
|
|
762
|
+
*
|
|
763
|
+
* JS dialogs (`alert`, `confirm`, `prompt`) and HTTP auth have no prop. iOS shows a `UIAlertController` titled with the
|
|
764
|
+
* frame's host, or `JavaScript` for a frame without one (`data:`, sandboxed, `file:`), one at a time (one that comes
|
|
765
|
+
* while another shows is cancelled), and Android WebView's own dialogs. An auth challenge is cancelled on Android, and
|
|
766
|
+
* gets WebKit's default handling on iOS: without a stored credential, the site's 401 page shows. `dialogs` and
|
|
767
|
+
* `basicAuthCredential` of `@react-native-minimal-webview/extensions` take them over, or a native hook does.
|
|
768
|
+
* @group WebView
|
|
769
|
+
*/
|
|
770
|
+
export interface WebViewProps extends Omit<ViewProps, 'children'> {
|
|
771
|
+
/**
|
|
772
|
+
* What to load.
|
|
773
|
+
*
|
|
774
|
+
* A new `source` loads at once, unless it equals the loaded one field by field: that loads nothing, even after the
|
|
775
|
+
* page navigated away.
|
|
776
|
+
*/
|
|
777
|
+
source: WebViewSource;
|
|
778
|
+
/**
|
|
779
|
+
* WebKit settings by property name; see {@link IOSSettings}.
|
|
780
|
+
* @ios
|
|
781
|
+
*/
|
|
782
|
+
ios?: IOSSettings;
|
|
783
|
+
/**
|
|
784
|
+
* Android settings by setter name; see {@link AndroidSettings}.
|
|
785
|
+
* @android
|
|
786
|
+
*/
|
|
787
|
+
android?: AndroidSettings;
|
|
788
|
+
/**
|
|
789
|
+
* Replaces the whole user agent, over `ios.webView.customUserAgent` and `android.settings.userAgentString`. Without
|
|
790
|
+
* it, those keys apply; unset, they are the platform's own user agent.
|
|
791
|
+
*
|
|
792
|
+
* To append to the default one instead, set `ios.configuration.applicationNameForUserAgent` and, on Android,
|
|
793
|
+
* `android.settings.userAgentString` to `AndroidWebView.defaultUserAgent` plus your suffix.
|
|
794
|
+
*
|
|
795
|
+
* A change applies from the next navigation.
|
|
796
|
+
*/
|
|
797
|
+
userAgent?: string;
|
|
798
|
+
/**
|
|
799
|
+
* Scripts to run at document start, before the page's own, in the frames picked by each script's `allFrames` and
|
|
800
|
+
* `origins`.
|
|
801
|
+
*
|
|
802
|
+
* A change applies from the next navigation. On Android with a WebView older than 101 they don't run (a warning says
|
|
803
|
+
* so, once per process).
|
|
804
|
+
*/
|
|
805
|
+
injectedScripts?: InjectedScript[];
|
|
806
|
+
/**
|
|
807
|
+
* Which navigations load in place, open externally or are blocked, decided natively; the first matching rule wins,
|
|
808
|
+
* and with no match the navigation is blocked.
|
|
809
|
+
*
|
|
810
|
+
* Unset, the rules are {@link defaultNavigationRules}; `[]` is no rules, so every navigation they decide is blocked.
|
|
811
|
+
*
|
|
812
|
+
* A malformed rule, one with an unknown key, a field of the wrong type (a list that isn't an array of strings, a flag
|
|
813
|
+
* that isn't a boolean) or no valid `action`, blocks every navigation that reaches it, whatever it was meant to match
|
|
814
|
+
* or do: native would otherwise skip the key or flag and match more than intended, and so could allow more than a
|
|
815
|
+
* correct rule would. In development a warning names the rule's index and the problem, once. The rules before it
|
|
816
|
+
* decide as usual; one that isn't an array is `[]`.
|
|
817
|
+
*
|
|
818
|
+
* `about:`, `data:` and `javascript:` always load in place. A `blob:` URL is decided as the origin it carries:
|
|
819
|
+
* `blob:https://example.com/…` as `https://example.com/`, and one with an opaque origin (`blob:null/…`, made by a
|
|
820
|
+
* `data:` or sandboxed frame, or a page from an html `source` without `baseUrl`) is blocked, a download of it too.
|
|
821
|
+
*
|
|
822
|
+
* The `source` load, reloads and back/forward skip the rules; a redirect of the `source` load doesn't, nor on Android
|
|
823
|
+
* a redirect of a reload or back/forward. The rules never see a form POST, on either platform: it loads in place, and
|
|
824
|
+
* one to a new window loads in place on iOS, and on Android isn't shown (the request may still reach the server).
|
|
825
|
+
*
|
|
826
|
+
* An `external` `intent:` URL (Android) opens only as a browsable intent, without its explicit component or selector;
|
|
827
|
+
* when no app handles it, an http(s) `S.browser_fallback_url` goes through the rules (main frame, no user gesture),
|
|
828
|
+
* and the `intent:` itself is reported as `block`.
|
|
829
|
+
*
|
|
830
|
+
* A change applies from the next navigation.
|
|
831
|
+
*
|
|
832
|
+
* A native hook can take these decisions, defaults included: see `WebViewContext.clients` (Android) and
|
|
833
|
+
* `navigationDelegates` (iOS).
|
|
834
|
+
* @example
|
|
835
|
+
* ```tsx
|
|
836
|
+
* <WebView
|
|
837
|
+
* source={{ uri: 'https://example.com' }}
|
|
838
|
+
* // myapp: opens externally even without a user gesture; everything else as by default
|
|
839
|
+
* navigationRules={[{ scheme: ['myapp'], action: 'external' }, ...defaultNavigationRules]}
|
|
840
|
+
* />
|
|
841
|
+
* ```
|
|
842
|
+
* @see [Navigation rules example](https://rasmuskard.github.io/react-native-minimal-webview/guides/examples/#navigation-rules)
|
|
843
|
+
*/
|
|
844
|
+
navigationRules?: readonly NavigationRule[];
|
|
845
|
+
/**
|
|
846
|
+
* Decides each navigation an `ask` rule matches, within 1 s.
|
|
847
|
+
*
|
|
848
|
+
* A late, missing or other answer is `block`, as is one still pending when another page commits; `signal` aborts at
|
|
849
|
+
* the timeout and at that commit. Without this prop, `ask` is `block`. Until the next commit, a URL it allowed isn't
|
|
850
|
+
* asked again on a redirect back to it (Android), or when the same kind of frame loads it again (iOS).
|
|
851
|
+
*/
|
|
852
|
+
onNavigationRequest?: (request: NavigationRequest, signal: AbortSignal) => NavigationAction | Promise<NavigationAction>;
|
|
853
|
+
/**
|
|
854
|
+
* Opens `external` navigations, every one, an Android `intent:` URL included, before `onNavigation` reports them.
|
|
855
|
+
*
|
|
856
|
+
* Return false (a promise is awaited) to hand it back: the core then opens it as it does without this prop, an
|
|
857
|
+
* `intent:`'s fallback URL included. Any other answer, `undefined` too, means you opened it. When `signal` aborts,
|
|
858
|
+
* nothing opens it.
|
|
859
|
+
*
|
|
860
|
+
* Without this prop, the system browser or app for the URL opens it, without asking `canOpenURL` first, so no
|
|
861
|
+
* `LSApplicationQueriesSchemes` (iOS) or `<queries>` (Android) entries are needed.
|
|
862
|
+
*/
|
|
863
|
+
onOpenExternal?: (request: NavigationRequest, signal: AbortSignal) => boolean | void | Promise<boolean | void>;
|
|
864
|
+
/**
|
|
865
|
+
* The bridge to the page, from `createBridge`, of any schema.
|
|
866
|
+
*
|
|
867
|
+
* Read when the WebView is created: another bridge recreates it (the page reloads and the navigation state starts
|
|
868
|
+
* over), so make it once, at module scope or with `useMemo` (in development, a WebView warns the first time another
|
|
869
|
+
* bridge replaces its own). A bridge serves one mounted WebView at a time: attaching it to a second throws.
|
|
870
|
+
*
|
|
871
|
+
* On Android with a WebView older than 101 there is no bridge: calls reject with `unavailable`, and a warning says
|
|
872
|
+
* so, once per process.
|
|
873
|
+
*/
|
|
874
|
+
bridge?: Pick<Bridge, typeof CONNECTION>;
|
|
875
|
+
/**
|
|
876
|
+
* The native hooks to turn on, as {@link HookExtension}s: a package with a native hook exports a function that
|
|
877
|
+
* returns one.
|
|
878
|
+
*
|
|
879
|
+
* A false, null or undefined entry is left out, so `on && extension(…)` switches one on and off; see
|
|
880
|
+
* {@link HookExtension.options} for what off means. A hook attaches when its key first appears, and a later change of
|
|
881
|
+
* its `options` reaches it live, without recreating the WebView. A key turned on after the WebView was created
|
|
882
|
+
* attaches its hook then, unless the hook has a `configure` step or its own WebView class (`webViewClass`,
|
|
883
|
+
* `webViewFactory`): that one attaches only when the WebView is created, and a warning says so.
|
|
884
|
+
* @see [Native hooks guide](https://rasmuskard.github.io/react-native-minimal-webview/guides/native-hooks/)
|
|
885
|
+
*/
|
|
886
|
+
extensions?: readonly (HookExtension | false | null | undefined)[];
|
|
887
|
+
/**
|
|
888
|
+
* Pull down at the top of the page to reload: `UIRefreshControl` on iOS, `SwipeRefreshLayout` on Android, where a
|
|
889
|
+
* pull inside a scrolled inner scroller (`overflow: auto`) scrolls it instead.
|
|
890
|
+
*
|
|
891
|
+
* It reloads as {@link WebViewRef.reload | reload()} does, a POST `source` included. A change applies at once.
|
|
892
|
+
* @default `false`
|
|
893
|
+
*/
|
|
894
|
+
pullToRefresh?: boolean;
|
|
895
|
+
/**
|
|
896
|
+
* Copies `HTTPCookieStorage` (native requests) into the WebView before each `source` load, which waits for it, and
|
|
897
|
+
* the WebView's cookie changes, deletions included, back as they happen.
|
|
898
|
+
*
|
|
899
|
+
* Android needs nothing: `CookieManager` is shared app-wide, and flushed when the app goes to the background. A
|
|
900
|
+
* WebView on another `android.webView.profile` has that profile's cookies instead.
|
|
901
|
+
* @default `false`
|
|
902
|
+
* @ios
|
|
903
|
+
*/
|
|
904
|
+
sharedCookies?: boolean;
|
|
905
|
+
/**
|
|
906
|
+
* Answers `<input type="file">` with `file://` or `content://` URIs, or null to cancel.
|
|
907
|
+
*
|
|
908
|
+
* Without it, the system file chooser opens, one at a time for the whole app, and ignores `capture`: a request while
|
|
909
|
+
* one is open is cancelled, and if the chooser's activity died without returning a result, so is every request until
|
|
910
|
+
* the app restarts.
|
|
911
|
+
*
|
|
912
|
+
* iOS always shows WebKit's picker, which needs `NSCameraUsageDescription` and `NSPhotoLibraryUsageDescription`.
|
|
913
|
+
*
|
|
914
|
+
* Until the answer settles, the page's next file request waits.
|
|
915
|
+
* @android
|
|
916
|
+
*/
|
|
917
|
+
onFileRequest?: (request: FileRequest, signal: AbortSignal) => string[] | null | Promise<string[] | null>;
|
|
918
|
+
/**
|
|
919
|
+
* The page entered or left fullscreen (`active`).
|
|
920
|
+
*
|
|
921
|
+
* Fullscreen works without it: the page's view covers the window, the system bars hide and back exits. iOS shows
|
|
922
|
+
* WebKit's own fullscreen, not reported.
|
|
923
|
+
* @android
|
|
924
|
+
*/
|
|
925
|
+
onFullscreenChange?: (event: FullscreenChangeEvent) => void;
|
|
926
|
+
/**
|
|
927
|
+
* Camera, microphone and, on Android, location and protected media (DRM) for the main frame.
|
|
928
|
+
*
|
|
929
|
+
* Answer true to grant, having asked the OS first (expo-camera, expo-location): without OS permission the page's
|
|
930
|
+
* request fails anyway. On iOS, true also skips WebKit's own prompt.
|
|
931
|
+
*
|
|
932
|
+
* Without this prop, Android denies and iOS shows WebKit's prompt (location always does). iOS needs
|
|
933
|
+
* `NSCameraUsageDescription` and `NSMicrophoneUsageDescription`, Android the `CAMERA` and `RECORD_AUDIO` manifest
|
|
934
|
+
* permissions. Android keeps a location answer until the page changes.
|
|
935
|
+
*
|
|
936
|
+
* Anything but true denies. `signal` also aborts when Android withdraws the request. Until the answer settles, the
|
|
937
|
+
* page's next request of the same kind waits.
|
|
938
|
+
*/
|
|
939
|
+
onPermissionRequest?: (request: PermissionRequest, signal: AbortSignal) => boolean | Promise<boolean>;
|
|
940
|
+
/**
|
|
941
|
+
* A download finished ({@link Download}) or failed ({@link DownloadFailure}).
|
|
942
|
+
*
|
|
943
|
+
* With this prop the core saves the file: iOS downloads it in the page's session; Android fetches it again with the
|
|
944
|
+
* host's cookies (whatever their SameSite) and the page's user agent. That fetch sends no POST body, `source` headers
|
|
945
|
+
* or HTTP auth, so a download that needs them can fail, or save the server's login page as the file, and it fails
|
|
946
|
+
* above 100 MB, on a redirect off https, and for `blob:` on a WebView older than 83.
|
|
947
|
+
*
|
|
948
|
+
* Without this prop, an http(s) download opens externally (`onOpenExternal` or the system browser, without the page's
|
|
949
|
+
* session), and a `data:` or `blob:` one is dropped. A PDF is a download on Android; iOS shows it inline.
|
|
950
|
+
*
|
|
951
|
+
* On iOS a file response is a download only in the main frame; an `<a download>` link is one from any frame.
|
|
952
|
+
*
|
|
953
|
+
* On Android a native hook's `WebViewContext.onDownloadStart` is offered each download first, and one it takes
|
|
954
|
+
* reaches neither this prop nor the external fallback.
|
|
955
|
+
*/
|
|
956
|
+
onDownload?: (result: DownloadResult) => void;
|
|
957
|
+
/**
|
|
958
|
+
* Every navigation the rules, or their defaults, decided.
|
|
959
|
+
*
|
|
960
|
+
* Not reported: the `source` load, reloads, back/forward, and a navigation a native hook decides.
|
|
961
|
+
*/
|
|
962
|
+
onNavigation?: (event: NavigationEvent) => void;
|
|
963
|
+
/**
|
|
964
|
+
* Every change to the navigation state.
|
|
965
|
+
*
|
|
966
|
+
* Changes that arrive together may be combined into one call; `loading` going true is never combined away, so a load
|
|
967
|
+
* that starts and fails at once still reports both.
|
|
968
|
+
*/
|
|
969
|
+
onNavigationStateChange?: (state: NavigationState) => void;
|
|
970
|
+
/**
|
|
971
|
+
* A main-frame load failed, or got an HTTP error status.
|
|
972
|
+
*
|
|
973
|
+
* After a failed load iOS keeps the page before it on screen, and Android shows WebView's own error page. On Android
|
|
974
|
+
* with a WebView older than 146, an SSL error on a back/forward load isn't reported.
|
|
975
|
+
*
|
|
976
|
+
* A renderer crash isn't reported here: it makes the cut-off load's `outcome` `failed` and calls `onProcessGone`.
|
|
977
|
+
*/
|
|
978
|
+
onError?: (event: LoadErrorEvent) => void;
|
|
979
|
+
/**
|
|
980
|
+
* The renderer died; see {@link ProcessGoneEvent} for the recovery.
|
|
981
|
+
*
|
|
982
|
+
* Android's WebViews share one renderer, so each of them reports its death.
|
|
983
|
+
*/
|
|
984
|
+
onProcessGone?: (event: ProcessGoneEvent) => void;
|
|
985
|
+
/**
|
|
986
|
+
* `loading` went true.
|
|
987
|
+
*
|
|
988
|
+
* On Android with a WebView older than 146, a load that never commits fires neither this nor `onLoadEnd`.
|
|
989
|
+
*/
|
|
990
|
+
onLoadStart?: (state: NavigationState) => void;
|
|
991
|
+
/** `loading` went false: `state.outcome` says how the load ended. */
|
|
992
|
+
onLoadEnd?: (state: NavigationState) => void;
|
|
993
|
+
/**
|
|
994
|
+
* Gets the {@link WebViewRef}.
|
|
995
|
+
*
|
|
996
|
+
* Give each mounted WebView its own ref: one shared by two points at the last mounted (with a warning in
|
|
997
|
+
* development), and can turn null when either unmounts.
|
|
998
|
+
*/
|
|
999
|
+
ref?: Ref<WebViewRef>;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* The methods a WebView's `ref` gets: navigation, scripts, and platform and hook calls.
|
|
1003
|
+
*
|
|
1004
|
+
* A handle kept after its WebView unmounted is safe to call: the navigation methods do nothing, and `evaluate`, `get`,
|
|
1005
|
+
* `call` and `hook` reject with `unavailable`. One still pending when the WebView unmounts rejects with `navigated`.
|
|
1006
|
+
*
|
|
1007
|
+
* Those four wait up to about a second for a WebView that has just mounted (on Android, a call from the parent's first
|
|
1008
|
+
* effect can come before the view exists), then reject with `unavailable`; in the background they don't wait.
|
|
1009
|
+
* @group WebView
|
|
1010
|
+
*/
|
|
1011
|
+
export type WebViewRef = {
|
|
1012
|
+
/**
|
|
1013
|
+
* Goes back one page; does nothing unless `canGoBack`.
|
|
1014
|
+
*
|
|
1015
|
+
* On Android, as Chrome's back button, it also skips the history entries a page added without a user gesture
|
|
1016
|
+
* (`pushState`, a hash change) and the page that added them.
|
|
1017
|
+
*/
|
|
1018
|
+
goBack(): void;
|
|
1019
|
+
/** Goes forward one page; does nothing unless `canGoForward`. */
|
|
1020
|
+
goForward(): void;
|
|
1021
|
+
/**
|
|
1022
|
+
* Reloads the page; after a failed load, retries it (the `source` again, with its headers, when that failed), and
|
|
1023
|
+
* loads the `source` when nothing is loaded.
|
|
1024
|
+
*
|
|
1025
|
+
* Sends a POST `source` again, and lifts the limit on {@link ProcessGoneEvent.recovered | crash recovery}.
|
|
1026
|
+
*/
|
|
1027
|
+
reload(): void;
|
|
1028
|
+
/** Stops the load: its `outcome` is `cancelled`, or on Android `finished` once the page has arrived. */
|
|
1029
|
+
stopLoading(): void;
|
|
1030
|
+
/**
|
|
1031
|
+
* Runs an expression (not statements) in the page's main frame and awaits it.
|
|
1032
|
+
*
|
|
1033
|
+
* Resolves with its value through JSON: `null` for what JSON can't encode (a function, `NaN`, a cycle), a `Date` as
|
|
1034
|
+
* its ISO string. The value is the page's, so treat it as page data. Rejects with a {@link CodedError}:
|
|
1035
|
+
* `script_error`, `navigated` or `unavailable`. There is no timeout: a page can hold a call until it navigates. On
|
|
1036
|
+
* Android with a WebView older than 83 it rejects with `unavailable`.
|
|
1037
|
+
*/
|
|
1038
|
+
evaluate(script: string): Promise<unknown>;
|
|
1039
|
+
/**
|
|
1040
|
+
* Reads a platform setting, `root.key`, and resolves with its value through JSON, as {@link WebViewRef.call | call}
|
|
1041
|
+
* converts results.
|
|
1042
|
+
*
|
|
1043
|
+
* The roots and keys are those of {@link IOSSettings} and {@link AndroidSettings}.
|
|
1044
|
+
*
|
|
1045
|
+
* It reads the WebView as it is when the call arrives: a navigation doesn't reject it. Rejects with a
|
|
1046
|
+
* {@link CodedError}: `not_found`, `unavailable` (Android) or `native_error`.
|
|
1047
|
+
* @example
|
|
1048
|
+
* ```ts
|
|
1049
|
+
* const bounces = await ref.current?.get('scrollView.bounces'); // iOS
|
|
1050
|
+
* const textZoom = await ref.current?.get('settings.textZoom'); // Android
|
|
1051
|
+
* ```
|
|
1052
|
+
*/
|
|
1053
|
+
get(key: string): Promise<unknown>;
|
|
1054
|
+
/**
|
|
1055
|
+
* Calls a platform method, `root.method`, and resolves with its result through JSON. It acts on the WebView as it is
|
|
1056
|
+
* when the call arrives: a navigation doesn't reject it.
|
|
1057
|
+
*
|
|
1058
|
+
* The roots are those of {@link IOSSettings} and {@link AndroidSettings}. On Android `method` is a public method of
|
|
1059
|
+
* the root's object; on iOS an Objective-C selector.
|
|
1060
|
+
*
|
|
1061
|
+
* Rejects with a {@link CodedError}: `not_found`, `unavailable` (Android) or `native_error`.
|
|
1062
|
+
*
|
|
1063
|
+
* The core owns, and so rejects:
|
|
1064
|
+
*
|
|
1065
|
+
* - iOS: every selector that loads, navigates (`setInteractionState:` too), runs a script, starts a download or sets
|
|
1066
|
+
* a delegate, and private (`_`), KVC, `willChange…`, `didChange…`, `performSelector`, `init…`, `copy…`,
|
|
1067
|
+
* `mutable…`, `new…` and reference-counting selectors.
|
|
1068
|
+
* - Android: the loads, navigations, clients, JavaScript interfaces, web message listeners,
|
|
1069
|
+
* document-start scripts, the touch, scroll-change and navigation listeners, `destroy`, `removeAllViews`,
|
|
1070
|
+
* `pauseTimers`, `resumeTimers`, `evaluateJavascript` and remote debugging
|
|
1071
|
+
* ({@link AndroidWebView.setWebContentsDebuggingEnabled}).
|
|
1072
|
+
*
|
|
1073
|
+
* A trailing completion handler (iOS) or `ValueCallback` (Android) is left out of `args`: the call resolves with what
|
|
1074
|
+
* it reports, or on iOS rejects with its error. On iOS, a selector whose completion handler isn't in this package's
|
|
1075
|
+
* SDK headers, or reports something other than an object, an error or an integer, rejects with `native_error`. On
|
|
1076
|
+
* Android, a method that takes `args` as they are wins over one that also takes a callback, and a `null` in the
|
|
1077
|
+
* callback's place passes none.
|
|
1078
|
+
*
|
|
1079
|
+
* One mistake the signature can't catch crashes the app: on iOS an object argument of the wrong class, and on Android
|
|
1080
|
+
* a `null` listener or callback other than the trailing `ValueCallback`, once WebView calls it. For a method that
|
|
1081
|
+
* takes another object, write a native hook.
|
|
1082
|
+
*
|
|
1083
|
+
* @param args The method's arguments: booleans, numbers, strings and `null`. On iOS also the structs as objects, and
|
|
1084
|
+
* `{ set: [...] }`, `{ date: seconds }`, `{ data: base64 }` or `{ cookie: properties }` for an `NSSet`, `NSDate`,
|
|
1085
|
+
* `NSData` or `NSHTTPCookie`; any other string stays an `NSString`. A cookie's properties are `NSHTTPCookie`'s keys:
|
|
1086
|
+
* `Name`, `Value`, `Domain` and `Path` are required; `Max-Age` (seconds, as a string), `Secure`, `HttpOnly` and
|
|
1087
|
+
* `SameSite` are some of the optional ones. A tag that can't make its object (`{ data }` that isn't base64, a
|
|
1088
|
+
* `{ cookie }` without a required key) rejects with `native_error`.
|
|
1089
|
+
* @returns The result: primitives, strings, and collections of them; on iOS also the structs as objects, an `NSURL`
|
|
1090
|
+
* as a string, an `NSDate` as seconds, `NSData` as base64, a `UIImage` as a base64 PNG and an `NSHTTPCookie` as its
|
|
1091
|
+
* properties (`Expires` in seconds). Any other object is `null`.
|
|
1092
|
+
* @example
|
|
1093
|
+
* ```ts
|
|
1094
|
+
* await ref.current?.call('webView.clearCache', [true]); // Android
|
|
1095
|
+
* await ref.current?.call('scrollView.setContentOffset:animated:', [{ x: 0, y: 100 }, true]); // iOS
|
|
1096
|
+
* ```
|
|
1097
|
+
*/
|
|
1098
|
+
call(method: string, args?: unknown[]): Promise<unknown>;
|
|
1099
|
+
/**
|
|
1100
|
+
* Calls the `method` handler the native hook `name` registered with `context.handle`, with `args`, and resolves with
|
|
1101
|
+
* its reply; both cross as JSON.
|
|
1102
|
+
*
|
|
1103
|
+
* The reply converts as {@link WebViewRef.call | call}'s results do. Rejects with a {@link CodedError}:
|
|
1104
|
+
* `not_found`, `unavailable` (Android), `handler_error` when the handler fails the call (`reply.fail(message)`, or a
|
|
1105
|
+
* throw on Android), or `native_error` for a reply JSON can't encode. A navigation or a crash doesn't settle a call
|
|
1106
|
+
* whose handler hasn't replied; only an unmount does.
|
|
1107
|
+
*/
|
|
1108
|
+
hook(name: string, method: string, args?: unknown[]): Promise<unknown>;
|
|
1109
|
+
};
|
|
1110
|
+
/**
|
|
1111
|
+
* What `createWebViewRef()` returns: a ref that outlives the WebViews mounted with it.
|
|
1112
|
+
* @group WebView
|
|
1113
|
+
*/
|
|
1114
|
+
export type WebViewRefObject = {
|
|
1115
|
+
/** The mounted WebView's ref, or null while none is mounted. */
|
|
1116
|
+
readonly current: WebViewRef | null;
|
|
1117
|
+
/** The mounted WebView's navigation state, read synchronously. */
|
|
1118
|
+
getState(): NavigationState;
|
|
1119
|
+
/**
|
|
1120
|
+
* Calls `listener` on every state change, including a WebView mounting or unmounting, and returns the unsubscribe.
|
|
1121
|
+
*
|
|
1122
|
+
* A subscriber stays across remounts.
|
|
1123
|
+
*/
|
|
1124
|
+
subscribe(listener: () => void): () => void;
|
|
1125
|
+
};
|
|
1126
|
+
export {};
|
|
1127
|
+
//# sourceMappingURL=types.d.ts.map
|