Почему событие не выполняется полностью?

Всем привет. Очень долго искал как запустить событие клавиатуры чтоб имитировать пользовательские событие (чтоб свойство isTrusted было true ) . Подсказали что это можно реализовать только с помощью браузерных расширений. Нашел один небольшой скрипт который имитировал событие клика мышкой и переписал под свои нужды. Скрипт работает частично. Событие вызывается, свойство istrusted как я и хотел true , но ввод в текстовое поле не происходит .

  chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
      if(request.state=="d"){
        chrome.debugger.sendCommand( { tabId: sender.tab.id }, 
          "Input.dispatchKeyEvent", { type: "keyDown", key:request.key }, 
          function (e) { console.log('keyPressed', request.key ) 
        });
      }else if(request.state=="u"){
        chrome.debugger.sendCommand( { tabId: sender.tab.id } , 
          "Input.dispatchKeyEvent", { type: "keyUp" , key:request.key }, 
          function (e) { console.log('keyReleased', request.key ) 
        });
  
      }else{
        console.log('Что-то пошло не так!')
      }
  })
  
  
document.addEventListener('keydown', keyBoardDown);
document.addEventListener('keyup', keyBoardUp);
let press=false;
window.press=press;

function keyBoardDown(e){
  console.log(' Down ', e.key, e.isTrusted);
  if(e.isTrusted) console.log(e.target);
  if (e.key && !press && !e.isTrusted) {
    press = true;
    chrome.runtime.sendMessage({ key: e.key, state:'d'}, function (response) {
      keyBoardUp(e);
      // console.log(' Down ', e.key, e.isTrusted);
    });
  }
}

function keyBoardUp(e){
  console.log(' Up ', e.key, e.isTrusted );
  if(e.isTrusted) console.log(e.target);  
   if (e.key && press && !e.isTrusted) {
    press = false;
    chrome.runtime.sendMessage({ key: e.key, state:'u'}, function (response) {
      // console.log(' Up ', e.key, e.isTrusted);
    });
  }
}

Необходимое текстовое поле скрипт сам находит , дело не в таргете. Как сымитировать полноценный пользовательский ввод с клавиатуры ?


Ответы (0 шт):