简体   繁体   English

以编程方式填写reactjs表单

[英]Programmatically fill reactjs form

I am writing an userscript and I can't manage to fill a form made by reactjs. 我正在写一个用户脚本,我无法填写reactjs制作的表格。 My code: 我的代码:

document.querySelector("#id-username").value = "name@domain.xx";
// Attempt to notify framework using input event
document.querySelector("#id-username").dispatchEvent(new Event("input", {data:"name@domain.xx"}));
// Attempt to notify framework using change event
document.querySelector("#id-username").dispatchEvent(new Event("change"));
// This doesn't help either
document.querySelector("#id-username").dispatchEvent(new Event("blur"));
// Submit the form using button (it's AJAX form)
document.querySelector("fieldset div.wrap button").click();

I entered this code into developper tools console after loading the page. 我在加载页面后将此代码输入到开发工具控制台中。 However the form ignores my programatical input: 然而,表格忽略了我的编程输入:

图片描述

The form can be found here . 表格可以在这里找到。 The purpose of my work is to automate login to given website. 我的工作目的是自动登录到给定的网站。 I provided the specific URL, but I expect generic solution to this problem (eg. using some reactjs API) that can be applied to any reactjs form. 我提供了特定的URL,但我期望这个问题的通用解决方案(例如,使用一些reactjs API)可以应用于任何reactjs表单。 Other users might need this solution for writing automated tests for their sites. 其他用户可能需要此解决方案来为其站点编写自动化测试。

Event must be emmited to ReactJS to make it register the value. 必须将事件发送到ReactJS以使其注册该值。 In particular, the input event. 特别是input事件。 It is really important to ensure that the event bubbles - React JS has only one listener at the document level, rather than on the input field. 确保事件起泡非常重要 - React JS在document级别只有一个侦听器,而不是输入字段。 I crafted the following method to set the value for input field element: 我精心设计了以下方法来设置输入字段元素的值:

function reactJSSetValue(elm, value) {
    elm.value = value;
    elm.defaultValue = value;
    elm.dispatchEvent(new Event("input", {bubbles: true, target: elm, data: value}));
}

For Chrome version 64, there is a workaround. 对于Chrome版本64,有一种解决方法。 Otherwise the event is being ignored. 否则该事件将被忽略。

See: https://github.com/facebook/react/issues/10135#issuecomment-314441175 请参阅: https//github.com/facebook/react/issues/10135#issuecomment-314441175

(Full credit to fatfisz in the link) (在链接中完全归功于fatfisz)

Code

function setNativeValue(element, value) {
  const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
  const prototype = Object.getPrototypeOf(element);
  const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;

  if (valueSetter && valueSetter !== prototypeValueSetter) {
    prototypeValueSetter.call(element, value);
  } else {
    valueSetter.call(element, value);
  }
}

Usage 用法

setNativeValue(textarea, 'some text');
// you must dispatch the input event, or the value will not update !!!
textarea.dispatchEvent(new Event('input', { bubbles: true }));

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

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