Is there an efficient way format large amounts of injected javascript strings and retain type completion + testable functionality for react-native-webview?
For react-native, the react-native-webview library allows communication across a native WebView and react-native. This requires injection of javascript in string format to customize what is communicated and how user interactions are processed.
This injection can quickly get messy, unreadable, or have small bugs that are hard to test when a bunch of functionality needs to be combined.
Does anyone have a better scalable solution for this?
My requirements for injectableStrings (that affect the Webview DOM)
The react-native-webview docs give simplified references to javascript in string format which do not have syntax or type completions.
Current approach and Answer
Example
Browser.injectableJavascript.ts
const registerPressHandler = () => {
const imgNodes = document.getElementsByTagName('img')
const linkNodes = document.getElementsByTagName('a')
// add callbacks etc...
}
// re-registers events whenever the document re-renders nodes...
const createObservers = () => {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
// handle cases here...and rebind events to new nodes
registerPressHandler()
})
})
observer.observe(document.body, {
childList: true,
subtree: true,
})
}
export const browserInjection = `
${registerPressHandler.toString()};
// initialize web click handler
registerPressHandler()
${createObservers.toString()};
createObservers();
`
Browser.tsx
import React from 'react'
import Webview from 'react-native-webview'
import { browserInjection } from './Browser.injections'
const Browser = () => {
return <Webview injectedJavascript={browserInjection} />
}
export default Browser
Other ideas:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.