0

[NOTE]: I've checked SO answers which might have worked 5 years ago which don't seem to work. Please don't copy-paste that junk. thank you.

task:

Take a string, conver it into sequece of keystrokes, do something with that sequence and ultimately come to the conclusion that the webpage you want to put that string in has gone through everything it needs to go through as if the string characters were sequentially pressed by a human through a keyboard.

Question:

How the hell is this done?

element.value = 'test123' // BAD EXAMPLE

if you don't know why this is a bad example, don't answer this question.

element.dispatchEvent(/*event for keydown*/)
element.dispatchEvent(/*event for textInput*/)
element.dispatchEvent(/*event for keyup*/)

^ this... this should technically work, however I've not found a working example.

Additionally apparently there may be some issues with event.isTrusted which I don't know how to get around.

... maybe I'm not using the right tools...

chrome.input.ime ... this seems to be related to chromiumOS and chrome doesn't react to it. (at least to what i wrote (i'm using windows) )

maybe chrome.debugger.... ??? though I can't figure out how to dispatch a keyboard event to an element through this api

TL/DR

Please post a valid example (which works beyond element.onkeydown(***)); which results in a keystroke being registered by a webpage as if it originated from a keyboard (or OS) when in fact it's from javascript.

EverNight
  • 964
  • 7
  • 16

1 Answers1

0

From here:

jQuery has a .keypress method accepting no arguments that simulates a keypress.

$("#target").keypress();

Will trigger a keypress on #target

If you'd like to also select which key was pressed, you can use .trigger. This example is from the docs:

var e = $.Event("keydown", { keyCode: 8}); //"keydown" if that's what you're doing
$("body").trigger(e);

The key code 8 is the key code for backspace in JavaScript.

Let me know how that works for you :)

Sam Mullinix
  • 196
  • 1
  • 10
  • Hi Sam. Chrome extensions do not support jquery(natively). Additionally, adding jquery.js files in the extension manifest does not seem to allow $(xxx) lookup... – EverNight Jul 08 '19 at 14:38
  • @EverNight you can add jQuery to your own extension, just use a jquery.js from your extension, not from an external URL. – SHamel Jul 09 '19 at 12:48
  • @EverNight since the code is isolated from the main content, you have to "inject" the keyboard and clicks in the page context - and it will be better not to use jQuery in your solution. See https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script on how to inject code in the page context. – SHamel Jul 09 '19 at 12:50
  • @SHamel Good advice. It doesn't apply to my case since i'm already operating in the page context. I have issues with the exact structure of the code or sequence of events and possibly order of events which need to be injected into that page context. It reacts to " element.click() ", and events are dispathed (verified with listeners) but I can't figure out what the entire sequence is for a full coverage of whatever it takes to make that page register an artificial keystroke. – EverNight Jul 10 '19 at 16:25