react-native-minimal-webview 1.2.0 → 1.3.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/README.md +35 -31
- package/android/build.gradle +1 -1
- package/android/src/main/java/expo/modules/minimalwebview/Asks.kt +5 -11
- package/android/src/main/java/expo/modules/minimalwebview/Hooks.kt +17 -11
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebView.kt +83 -125
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewClients.kt +16 -31
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewModule.kt +18 -19
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewNavigation.kt +24 -38
- package/android/src/main/java/expo/modules/minimalwebview/MinimalWebViewScripts.kt +24 -36
- package/android/src/main/java/expo/modules/minimalwebview/NavigationRules.kt +19 -20
- package/android/src/main/java/expo/modules/minimalwebview/PlatformSettings.kt +41 -37
- package/android/src/main/java/expo/modules/minimalwebview/PullToRefresh.kt +14 -18
- package/android/src/main/java/expo/modules/minimalwebview/requests/Downloads.kt +12 -23
- package/android/src/main/java/expo/modules/minimalwebview/requests/FileUpload.kt +0 -1
- package/android/src/main/java/expo/modules/minimalwebview/requests/FullscreenVideo.kt +1 -4
- package/android/src/main/java/expo/modules/minimalwebview/requests/Permissions.kt +34 -24
- package/build/WebView.d.ts +11 -2
- package/build/WebView.d.ts.map +1 -1
- package/build/WebView.js +45 -71
- package/build/WebView.js.map +1 -1
- package/build/bridge/createBridge.d.ts +1 -5
- package/build/bridge/createBridge.d.ts.map +1 -1
- package/build/bridge/createBridge.js +3 -15
- package/build/bridge/createBridge.js.map +1 -1
- package/build/bridge/endpoint.d.ts +1 -2
- package/build/bridge/endpoint.d.ts.map +1 -1
- package/build/bridge/endpoint.js +5 -6
- package/build/bridge/endpoint.js.map +1 -1
- package/build/bridge/matchesOrigin.d.ts +11 -0
- package/build/bridge/matchesOrigin.d.ts.map +1 -0
- package/build/bridge/matchesOrigin.js +26 -0
- package/build/bridge/matchesOrigin.js.map +1 -0
- package/build/bridge/origins.d.ts +2 -13
- package/build/bridge/origins.d.ts.map +1 -1
- package/build/bridge/origins.js +1 -30
- package/build/bridge/origins.js.map +1 -1
- package/build/bridge/pageClient.generated.d.ts +1 -1
- package/build/bridge/pageClient.generated.d.ts.map +1 -1
- package/build/bridge/pageClient.generated.js +2 -2
- package/build/bridge/pageClient.generated.js.map +1 -1
- package/build/bridge/protocol.d.ts +3 -5
- package/build/bridge/protocol.d.ts.map +1 -1
- package/build/bridge/protocol.js +12 -14
- package/build/bridge/protocol.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/navigationRules.d.ts +0 -5
- package/build/navigationRules.d.ts.map +1 -1
- package/build/navigationRules.js +3 -12
- package/build/navigationRules.js.map +1 -1
- package/build/reactNativeWebViewDefaults.d.ts +0 -4
- package/build/reactNativeWebViewDefaults.d.ts.map +1 -1
- package/build/reactNativeWebViewDefaults.js +0 -4
- package/build/reactNativeWebViewDefaults.js.map +1 -1
- package/build/reactNativeWebViewMessages.d.ts.map +1 -1
- package/build/reactNativeWebViewMessages.js +0 -1
- package/build/reactNativeWebViewMessages.js.map +1 -1
- package/build/refStore.d.ts +0 -2
- package/build/refStore.d.ts.map +1 -1
- package/build/refStore.js +0 -6
- package/build/refStore.js.map +1 -1
- package/build/types.d.ts +55 -39
- package/build/types.d.ts.map +1 -1
- package/ios/Asks.swift +6 -9
- package/ios/DelegateProxy.swift +4 -18
- package/ios/DownloadName.swift +0 -2
- package/ios/Downloads.swift +0 -2
- package/ios/Hooks.swift +4 -6
- package/ios/Loads.swift +37 -19
- package/ios/MinimalWebView+Dialogs.swift +5 -7
- package/ios/MinimalWebView+Navigation.swift +23 -49
- package/ios/MinimalWebView+Scripts.swift +10 -17
- package/ios/MinimalWebView.podspec +1 -2
- package/ios/MinimalWebView.swift +63 -128
- package/ios/MinimalWebViewModule.swift +1 -18
- package/ios/NavigationRules.swift +2 -9
- package/ios/OriginRules.swift +4 -13
- package/ios/PlatformSettings.swift +9 -30
- package/ios/ReadAccess.swift +0 -2
- package/ios/Records.swift +18 -0
- package/ios/Recovery.swift +2 -8
- package/ios/SharedCookies.swift +10 -25
- package/mocks/MinimalWebView.ts +5 -0
- package/package.json +11 -10
- package/src/WebView.tsx +58 -80
- package/src/bridge/createBridge.ts +5 -20
- package/src/bridge/endpoint.ts +6 -9
- package/src/bridge/matchesOrigin.ts +27 -0
- package/src/bridge/origins.ts +2 -30
- package/src/bridge/pageClient.generated.ts +2 -2
- package/src/bridge/pageClient.ts +1 -7
- package/src/bridge/protocol.ts +16 -18
- package/src/index.ts +1 -1
- package/src/navigationRules.ts +6 -12
- package/src/reactNativeWebViewDefaults.ts +0 -4
- package/src/reactNativeWebViewMessages.ts +0 -1
- package/src/refStore.ts +0 -7
- package/src/types.ts +55 -39
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@ A React Native WebView that hands you the iOS and Android WebView APIs, instead
|
|
|
4
4
|
|
|
5
5
|
It runs on iOS and Android as an Expo module.
|
|
6
6
|
|
|
7
|
+
[Get started](guides/getting-started.md) ·
|
|
7
8
|
[Install](#install) ·
|
|
8
9
|
[npm](https://www.npmjs.com/package/react-native-minimal-webview) ·
|
|
9
10
|
[API reference](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/) ·
|
|
@@ -37,6 +38,7 @@ import { WebView, type WebViewRef } from 'react-native-minimal-webview';
|
|
|
37
38
|
export function Browser() {
|
|
38
39
|
const ref = useRef<WebViewRef>(null);
|
|
39
40
|
const pageDown = () => {
|
|
41
|
+
// Android's WebView.pageDown(boolean): a method string names one platform's method
|
|
40
42
|
if (Platform.OS === 'android') ref.current?.call('webView.pageDown', [true]);
|
|
41
43
|
};
|
|
42
44
|
return (
|
|
@@ -54,7 +56,8 @@ export function Browser() {
|
|
|
54
56
|
}
|
|
55
57
|
```
|
|
56
58
|
|
|
57
|
-
A setting
|
|
59
|
+
A setting a newer SDK adds goes under its root's `other` key, and `call` takes any public method, so neither waits for a
|
|
60
|
+
release of this package: see
|
|
58
61
|
[`ios`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#ios),
|
|
59
62
|
[`android`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#android) and
|
|
60
63
|
[`call`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/WebViewRef/#call).
|
|
@@ -63,10 +66,13 @@ A setting or method a newer SDK adds works without a release of this package: se
|
|
|
63
66
|
|
|
64
67
|
[How it works](guides/how-it-works.md) explains the parts. The core adds only what the platform APIs lack:
|
|
65
68
|
|
|
66
|
-
- `navigationRules
|
|
67
|
-
|
|
68
|
-
-
|
|
69
|
-
|
|
69
|
+
- [`navigationRules`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#navigationrules): navigations decided
|
|
70
|
+
natively, with a JS round trip only where a prop asks for one.
|
|
71
|
+
- A typed promise bridge ([`createBridge`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/createBridge/)): `window.nativeBridge` in
|
|
72
|
+
the page, open only to the bridge's [`allowedOrigins`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/type-aliases/BridgeOptions/#allowedorigins).
|
|
73
|
+
- [`useWebViewState(ref)`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/functions/useWebViewState/): the navigation state, kept natively.
|
|
74
|
+
- [`onPermissionRequest`](https://rasmuskard.github.io/react-native-minimal-webview/typescript/react-native-minimal-webview/interfaces/WebViewProps/#onpermissionrequest): your app answers the
|
|
75
|
+
page's permission requests (camera, microphone, location).
|
|
70
76
|
|
|
71
77
|
The [migration guide](guides/migrating-from-react-native-webview.md) covers each react-native-webview prop that works differently here.
|
|
72
78
|
|
|
@@ -78,13 +84,34 @@ The [migration guide](guides/migrating-from-react-native-webview.md) covers each
|
|
|
78
84
|
|
|
79
85
|
## Install
|
|
80
86
|
|
|
87
|
+
[Get started](guides/getting-started.md) walks you through a new Expo app with a WebView on screen.
|
|
88
|
+
|
|
89
|
+
In an Expo app, install the package and expo-build-properties:
|
|
90
|
+
|
|
81
91
|
```sh
|
|
82
|
-
npx expo install react-native-minimal-webview
|
|
92
|
+
npx expo install react-native-minimal-webview expo-build-properties
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Set the iOS and Android [minimums](#requirements) in `app.json`, before you rebuild: below iOS 17.0, the build leaves
|
|
96
|
+
the package's native module out, and `WebView` throws as it renders.
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"expo": {
|
|
101
|
+
"plugins": [
|
|
102
|
+
[
|
|
103
|
+
"expo-build-properties",
|
|
104
|
+
{ "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
|
|
105
|
+
]
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
}
|
|
83
109
|
```
|
|
84
110
|
|
|
85
111
|
Then rebuild the app (`npx expo run:ios`, `npx expo run:android`).
|
|
86
112
|
|
|
87
|
-
A bare React Native app adds Expo modules first, then installs with its package manager
|
|
113
|
+
A bare React Native app adds Expo modules first, then installs with its package manager. It sets the minimums in
|
|
114
|
+
`ios/Podfile` (`platform :ios, '17.0'`) and `android/build.gradle` (`minSdkVersion = 29`) before `pod-install`:
|
|
88
115
|
|
|
89
116
|
```sh
|
|
90
117
|
npx install-expo-modules@latest
|
|
@@ -101,34 +128,11 @@ npx pod-install
|
|
|
101
128
|
| Expo SDK | 53 (React Native 0.79, React 19) |
|
|
102
129
|
| Architecture | [New architecture](https://reactnative.dev/architecture/landing-page) only |
|
|
103
130
|
|
|
104
|
-
An Expo app sets the two minimums with expo-build-properties (`npx expo install expo-build-properties`) in `app.json`:
|
|
105
|
-
|
|
106
|
-
```json
|
|
107
|
-
{
|
|
108
|
-
"expo": {
|
|
109
|
-
"plugins": [
|
|
110
|
-
[
|
|
111
|
-
"expo-build-properties",
|
|
112
|
-
{ "ios": { "deploymentTarget": "17.0" }, "android": { "minSdkVersion": 29 } }
|
|
113
|
-
]
|
|
114
|
-
]
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
131
|
Some features need a newer Android System WebView: [which ones](guides/android-system-webview.md).
|
|
120
132
|
|
|
121
133
|
## Usage
|
|
122
134
|
|
|
123
|
-
|
|
124
|
-
import { WebView } from 'react-native-minimal-webview';
|
|
125
|
-
|
|
126
|
-
export function Browser() {
|
|
127
|
-
return <WebView source={{ uri: 'https://example.com' }} style={{ flex: 1 }} />;
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
Then pick what you need:
|
|
135
|
+
Pick what you need:
|
|
132
136
|
|
|
133
137
|
| To | See |
|
|
134
138
|
| --------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
package/android/build.gradle
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
package expo.modules.minimalwebview
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Every `ask` (a page request, a navigation request or a hook's) waits here, by id, until JS answers it through [answer]
|
|
6
|
-
* or native gives up (null, and the core's `askCancelled` to JS): a Document end, a timeout or the platform. [send] puts an
|
|
7
|
-
* event on the view's `onHookEvent`, which reaches the app's extensions' `onEvent`. Main thread only.
|
|
8
|
-
*/
|
|
3
|
+
/** Main thread only. */
|
|
9
4
|
internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
|
|
10
5
|
private val pending = mutableMapOf<Int, (Any?) -> Unit>()
|
|
11
6
|
private var nextId = 0
|
|
12
|
-
/** The view closed: JS is unmounted, and a hook may still hold its context. */
|
|
13
7
|
private var closed = false
|
|
14
8
|
|
|
15
|
-
/** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once. */
|
|
16
|
-
fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int {
|
|
9
|
+
/** Sends the ask to JS and returns its id, for [cancel]. Once the view closed it answers null at once and returns null. */
|
|
10
|
+
fun ask(key: String, name: String, payload: Map<String, Any?>, onAnswer: (Any?) -> Unit): Int? {
|
|
17
11
|
if (closed) {
|
|
18
12
|
onAnswer(null)
|
|
19
|
-
return
|
|
13
|
+
return null
|
|
20
14
|
}
|
|
21
15
|
val id = ++nextId
|
|
22
16
|
pending[id] = onAnswer
|
|
@@ -36,7 +30,7 @@ internal class Asks(private val send: (Map<String, Any?>) -> Unit) {
|
|
|
36
30
|
onAnswer(null)
|
|
37
31
|
}
|
|
38
32
|
|
|
39
|
-
/** Cancels every pending ask. At closing JS isn't told
|
|
33
|
+
/** Cancels every pending ask. At closing JS isn't told. */
|
|
40
34
|
fun documentEnded(reason: DocumentEnd) {
|
|
41
35
|
if (reason == DocumentEnd.CLOSING) closed = true
|
|
42
36
|
pending.keys.toList().forEach { cancel(it, tell = reason != DocumentEnd.CLOSING) }
|
|
@@ -33,8 +33,16 @@ object MinimalWebViewHooks {
|
|
|
33
33
|
val key: String,
|
|
34
34
|
val webViewFactory: ((Context) -> WebView)?,
|
|
35
35
|
val configure: ((WebView, WebViewContext) -> Unit)?,
|
|
36
|
+
val newWebViewsOnly: Boolean,
|
|
36
37
|
val attach: (WebView, WebViewContext) -> Unit
|
|
37
|
-
)
|
|
38
|
+
) {
|
|
39
|
+
val whyNotLate get() = when {
|
|
40
|
+
configure != null -> "configure"
|
|
41
|
+
webViewFactory != null -> "a webViewFactory"
|
|
42
|
+
newWebViewsOnly -> "newWebViewsOnly"
|
|
43
|
+
else -> null
|
|
44
|
+
}
|
|
45
|
+
}
|
|
38
46
|
|
|
39
47
|
internal val hooks = mutableListOf<Hook>()
|
|
40
48
|
|
|
@@ -64,6 +72,8 @@ object MinimalWebViewHooks {
|
|
|
64
72
|
* @param configure runs on the main thread as soon as each WebView this hook is attached to, on or off, is made,
|
|
65
73
|
* before the package sets it up ([WebViewCompat.setProfile][androidx.webkit.WebViewCompat.setProfile] goes here).
|
|
66
74
|
* It gets the same context that [attach] gets next for that WebView, so state it makes can be keyed by the context.
|
|
75
|
+
* @param newWebViewsOnly true attaches the hook only when a WebView is created, never to one whose key turns on
|
|
76
|
+
* later, as a hook with [configure] or [webViewFactory] does.
|
|
67
77
|
* @param attach runs on the main thread once the WebView has its settings and is in the view hierarchy, so what it
|
|
68
78
|
* sets wins over those settings. Register what to run when that WebView goes away with
|
|
69
79
|
* [WebViewContext.onDestroyed]. Document-start scripts you add here run before the package's, so
|
|
@@ -73,13 +83,14 @@ object MinimalWebViewHooks {
|
|
|
73
83
|
key: String,
|
|
74
84
|
webViewFactory: ((Context) -> WebView)? = null,
|
|
75
85
|
configure: ((WebView, WebViewContext) -> Unit)? = null,
|
|
86
|
+
newWebViewsOnly: Boolean = false,
|
|
76
87
|
attach: (WebView, WebViewContext) -> Unit
|
|
77
88
|
) {
|
|
78
89
|
if (key == "core") {
|
|
79
90
|
Log.e(TAG, "the hook key `core` is reserved: the hook is not registered")
|
|
80
91
|
return
|
|
81
92
|
}
|
|
82
|
-
if (putHook(hooks, Hook(key, webViewFactory, configure, attach))) {
|
|
93
|
+
if (putHook(hooks, Hook(key, webViewFactory, configure, newWebViewsOnly, attach))) {
|
|
83
94
|
Log.w(
|
|
84
95
|
TAG,
|
|
85
96
|
"a hook under `$key` replaces the one registered before: a JS reload registers every hook again; " +
|
|
@@ -228,8 +239,8 @@ interface WebViewContext {
|
|
|
228
239
|
fun emit(name: String, payload: Map<String, Any?>)
|
|
229
240
|
|
|
230
241
|
/**
|
|
231
|
-
* Sends the event as [emit] does,
|
|
232
|
-
* promise awaited, in JSON types as [handle]'s arguments are.
|
|
242
|
+
* Sends the event as [emit] does, but to the extension's `onAsk` when it has one, and calls [onAnswer] with JS's
|
|
243
|
+
* answer: that handler's return value, a promise awaited, in JSON types as [handle]'s arguments are.
|
|
233
244
|
*
|
|
234
245
|
* It answers null when the handler throws or there is none, or when the Document ends first; there is no timeout. It
|
|
235
246
|
* answers null at once while the hook is off, and after the context's attach ended. An ask made before the WebView's
|
|
@@ -310,9 +321,8 @@ class HookReply internal constructor(
|
|
|
310
321
|
}
|
|
311
322
|
|
|
312
323
|
/**
|
|
313
|
-
*
|
|
314
|
-
*
|
|
315
|
-
* are volatile; the rest is main thread only.
|
|
324
|
+
* Chromium's IO thread reads [routed] and [clients] (shouldInterceptRequest), so [options] and the detach are volatile;
|
|
325
|
+
* the rest is main thread only.
|
|
316
326
|
*/
|
|
317
327
|
internal open class Attachment(value: Any?) {
|
|
318
328
|
@Volatile var options: Any? = value
|
|
@@ -325,7 +335,6 @@ internal open class Attachment(value: Any?) {
|
|
|
325
335
|
// own changes write them.
|
|
326
336
|
val clients: MutableList<WebViewClient> = CopyOnWriteArrayList()
|
|
327
337
|
val chromeClients = mutableListOf<WebChromeClient>()
|
|
328
|
-
// Copy-on-write too: the core iterates them live, and a listener may remove itself while it runs.
|
|
329
338
|
val touchListeners: MutableList<View.OnTouchListener> = CopyOnWriteArrayList()
|
|
330
339
|
val scrollListeners: MutableList<View.OnScrollChangeListener> = CopyOnWriteArrayList()
|
|
331
340
|
|
|
@@ -374,10 +383,8 @@ internal open class Attachment(value: Any?) {
|
|
|
374
383
|
internal fun <T> routed(attachments: List<Attachment>, pick: (Attachment) -> List<T>) =
|
|
375
384
|
attachments.filter { it.routed }.flatMap(pick)
|
|
376
385
|
|
|
377
|
-
/** The core's [WebViewContext] for one hook's attach: the hook's key goes on its events and asks. */
|
|
378
386
|
internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebView) :
|
|
379
387
|
Attachment(view.hookOptions[hook.key]), WebViewContext {
|
|
380
|
-
/** Null once the attach ended. Main thread only. */
|
|
381
388
|
private var view: MinimalWebView? = view
|
|
382
389
|
val key get() = hook.key
|
|
383
390
|
override val url get() = view?.url.orEmpty()
|
|
@@ -385,7 +392,6 @@ internal class HookContext(val hook: MinimalWebViewHooks.Hook, view: MinimalWebV
|
|
|
385
392
|
private val destroyedHandlers = mutableListOf<(DocumentEnd) -> Unit>()
|
|
386
393
|
val handlers = mutableMapOf<String, (List<Any?>, HookReply) -> Unit>()
|
|
387
394
|
|
|
388
|
-
/** The attach ended: also drops the view and what the hook gave it, bar the [onDestroyed] handlers [destroyed] runs next. */
|
|
389
395
|
override fun detach() {
|
|
390
396
|
super.detach()
|
|
391
397
|
view = null
|