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