javascript - ReactJS 使用 C# Webview 从外部组件设置值
问题描述
尝试抓取使用 ReactJS、Formit 和 yup 编写的现有网站。我的 C# 代码片段使用 Microsoft.Toolkit.Forms.UI.Controls.Webview
functionString = String.Format("document.getElementById('ghinNumber').value = {0};", b.Ghin);
await wbc.InvokeScriptAsync("eval", new string[] { functionString });
C# 代码按预期工作。我坚持如何处理 LoginForm React 组件(请参阅下面的 Login 组件 JS 代码),因为它保持自己的状态,最终呈现 HTML DOM 元素覆盖我用 C# 以编程方式设置的内容。我已经阅读了 ReactJS 文档和教程,让我对 PROPS 和 STATE 有了基本的了解。我意识到我必须以某种方式更新登录组件维护的状态。我知道模拟keydown、keyup、input、click等各种标准事件不是一件简单的事情。我不知道如何使用C#和Webview控件来处理它。因为我正在抓取一个网站,所以我无法修改来自服务器的现有 JS 代码。有人可以帮忙吗?
import React, { Component, Fragment } from "react";
import PropTypes from "prop-types";
import keyboardKeys from "../../../shared/variables/keyboard-keys";
import { Formik } from "formik";
import * as Yup from "yup";
class LoginForm extends Component {
validationSchema = Yup.object({
lastName: Yup.string().required("Required"),
ghinNumber: Yup.string().required("Required")
});
onSubmit = (values, { setSubmitting }) => {
setSubmitting(false);
this.props.login(values.lastName, values.ghinNumber);
};
render() {
return (
<Formik
onSubmit={this.onSubmit}
initialValues={{ lastName: "", ghinNumber: "" }}
validationSchema={this.validationSchema}
>
{options => this.renderForm(options)}
</Formik>
);
}
renderForm(options) {
return (
<Fragment>
<div className="login_form columns">
<div className="row">
<div className="col is-full">
<label className="white" htmlFor="login_id">
GHIN Number
</label>
<input
type="text"
id="ghinNumber"
value={options.values.ghinNumber}
name="ghinNumber"
onBlur={options.handleBlur}
onChange={options.handleChange}
/>
<span className="error">
{options.touched.ghinNumber ? options.errors.ghinNumber : ""}
</span>
</div>
</div>
<div className="row">
<div className="col is-full">
<label className="white" htmlFor="login_password">
Last Name
</label>
<input
type="text"
id="lastName"
value={options.values.lastName}
name="lastName"
onBlur={options.handleBlur}
onChange={options.handleChange}
onKeyDown={e => {
if (e.keyCode === keyboardKeys.ENTER) {
options.handleSubmit();
}
}}
/>
<span className="error">
{options.touched.lastName ? options.errors.lastName : ""}
</span>
</div>
</div>
{/* <div className="row">
<div className="col is-full">
<input type="checkbox" id="remember_me" name="remember_me" />
<label className="white" htmlFor="remember_me">Remember Me</label>
</div>
</div> */}
<div className="row">
<div className="col is-2-of-3 h-centered">
<button
type="submit"
className="btn fill cardinal"
onClick={options.handleSubmit}
>
LOG IN
</button>
</div>
</div>
{/* <div className="row">
<div className="col is-full">
<button className="btn lnk white">Forgot login information?</button>
</div>
</div> */}
</div>
</Fragment>
);
}
}
LoginForm.propTypes = {
login: PropTypes.func.isRequired
};
export default LoginForm;
解决方案
推荐阅读
- javascript - 替代使用多个标志来显示隐藏多个元素
- amazon-web-services - Amazon EMR 与 Amazon Redshift
- slack - 用于搜索外部数据源的 Slack 对话框
- javascript - 页面在浏览器中被杀死后如何运行任务?
- firebase - 在 null 上调用了 Flutter 方法“resetPassword”
- java - 如何为自定义光标创建单独的类
- flutter - 你如何在 Flutter Web 上使用 SVG?
- javascript - 如何将我的数组数组中每个数组的第一个索引从字符串转换为整数?
- android - 如何在 Mapbox android 中添加自定义视图作为标记?
- python-3.x - 如何训练 TensorFlow 读取图像上的可变长度数字?