简体   繁体   English

在 react-native 中格式注入 javascript 的最佳方式是什么?

[英]What is the best way format injected javascript in react-native?

Is there an efficient way format large amounts of injected javascript strings and retain type completion + testable functionality for react-native-webview?是否有一种有效的方式格式化大量注入的 javascript 字符串并为 react-native-webview 保留类型完成 + 可测试功能?

For react-native, the react-native-webview library allows communication across a native WebView and react-native.对于 react-native, react-native-webview库允许跨原生 WebView 和 react-native 进行通信。 This requires injection of javascript in string format to customize what is communicated and how user interactions are processed.这需要以字符串格式注入 javascript 以自定义通信内容以及处理用户交互的方式。

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)我对injectableStrings的要求(影响Webview DOM)

  1. Readable syntax可读的语法
  2. Type completions类型补全

The react-native-webview docs give simplified references to javascript in string format which do not have syntax or type completions. react-native-webview 文档以字符串格式提供了对 javascript 的简化引用,这些格式没有语法或类型完成。

Current approach and Answer当前的方法和答案

  1. Create a Webview file and an injection file.创建一个 Webview 文件和一个注入文件。
  2. Utilize the injection file to contain all web typings and functionality.利用注入文件包含所有网络类型和功能。 This functionality is formatted in javascript (not stringified)此功能以 javascript 格式(未字符串化)
  3. Create an initializer that converts all functionality into a string version of the functionality.创建一个初始化程序,将所有功能转换为该功能的字符串版本。 The initializer should always return string.初始值设定项应始终返回字符串。

Example例子

Browser.injectableJavascript.ts 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浏览器.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:其他想法:

  • Leverage a bundler to compile a stringified version of a javascript file.利用捆绑器来编译 javascript 文件的字符串化版本。 (cons: it's more processing power and not as fast as the first approach)(pros: way more browser compatibility but most polyifills won't be needed since this is only targeting android and iOS webviews...might be useful for older devices on lower webview specs...) (缺点:它的处理能力更强,不如第一种方法快)(优点:浏览器兼容性更高,但不需要大多数 polyifills,因为这仅针对 android 和 iOS webviews ......可能对旧设备有用较低的 webview 规格...)
  • Use a bunch of template literals.使用一堆模板文字。 (cons: this can get messy and is very similar to using pure strings. No type completions) (缺点:这可能会变得混乱,并且与使用纯字符串非常相似。没有类型完成)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM