在Edge / IE上无法填充在新窗口中打开的React应用程序的文本区域

时间:2018-07-13 20:18:52

标签: javascript html reactjs internet-explorer ui-automation

长话短说

Object.getOwnPropertyDescriptor(prototype, 'value').set.call(htmlElement, 'input value') 

抛出"Error: Invalid argument."

有帮助吗?

遵循一些代码以获取更多上下文。这段代码是解决React输入字段输入问题的一种变通方法。 See here for more info on the bug使得此变通办法变得必要。

var valueSetter;
var prototypeValueSetter;

var setNativeValue = (element, value, newTab) => {
  valueSetter = !newTab ? // reference to the tab only needed on IE
    Object.getOwnPropertyDescriptor(element, 'value').set : 
    newTab.window.Object.getOwnPropertyDescriptor(element, 'value').set ;
  var prototype = !newTab ?
    Object.getPrototypeOf(element) :
    newTab.window.Object.getPrototypeOf(element);
  prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
  if (valueSetter && valueSetter !== prototypeValueSetter) {
    debugger; // yep, element and value are ok..
    try {
      // throws  "Error: Invalid argument." vvvvvvvv
      prototypeValueSetter.call(element, value); 
    } catch (err) {  // not catched..! Is it async??
      console.log(`---> ${err}`);
      debugger;
    }
  } else {
    // (not called)
    valueSetter.call(element, value);
  }
}

// In which context it is used:

var w = open('https://steemit.com/steemit/@gaottantacinque/mission-complete-steemit-bot-ready-23c61cc6f3da8');

// #### await nap(1000);

// click Reply button
let replyBtn = w.document.getElementsByClassName('PostFull__reply')[0]
  .getElementsByTagName('a')[0];
replyBtn.click();

// #### await nap(1000);

// try to fill the textarea
let textarea = w.document.getElementsByTagName('textarea')[0];
setNativeValue(textarea, 'test', w);
textarea.dispatchEvent(new Event('input', { bubbles: true }));

0 个答案:

没有答案