Skip to content

register

fun register(key: String, webViewFactory: (Context) -> WebView? = null, configure: (WebView, WebViewContext) -> Unit? = null, newWebViewsOnly: Boolean = false, attach: (WebView, WebViewContext) -> Unit)

Registers the hook for key.

Call it from your module's OnCreate, before the first WebView.

A hook under a key that is taken already replaces that hook in its place, with a warning, so a key collision doesn't stop the app.

The hook attaches to each WebView whose extensions prop has an extension with key that is on; HookExtension.options says when. After a renderer crash it attaches again, to the WebView that replaces the dead one.

Hooks attach in registration order, which across packages is autolinking's (packages sorted by name), so an earlier hook's clients come first. A hook whose key turns on after the WebView was created attaches after the hooks attached already; after a crash it takes its place in registration order again.

Parameters

key the key of the app's extension that turns on the hook. Keys are global, so prefix yours with your package's name. core is reserved for the package's own events: a hook under it is logged and not registered.
webViewFactory makes each WebView this hook is attached to, on or off: the first one and each after a renderer crash, as a subclass that overrides what WebView lets a subclass override. Null leaves it to the other hooks. When the attached hooks name different factories (the same object is one), the first one's in registration order makes the WebView, and a warning names the others. Those still attach, so check the class attach gets.
configure runs on the main thread as soon as each WebView this hook is attached to, on or off, is made, before the package sets it up (WebViewCompat.setProfile goes here). It gets the same context that attach gets next for that WebView, so state it makes can be keyed by the context.
newWebViewsOnly true attaches the hook only when a WebView is created, never to one whose key turns on later, as a hook with configure or webViewFactory does.
attach runs on the main thread once the WebView has its settings and is in the view hierarchy, so what it sets wins over those settings. Register what to run when that WebView goes away with WebViewContext.onDestroyed. Document-start scripts you add here run before the package's, so window.nativeBridge and injectedScripts aren't there yet (on iOS yours run after them).