diff options
author | Teddy Wing | 2015-05-24 16:49:47 -0400 |
---|---|---|
committer | Teddy Wing | 2015-05-24 17:00:32 -0400 |
commit | 5a9aab5e4ebb71770c67fa266ef651b2e8dcbbd6 (patch) | |
tree | 846bc06804cba8cc6243047626e10df3a8fa2bbc | |
parent | e8c2b483d0ac3540dad9374ee00a586db321e016 (diff) | |
download | Peniquitous-5a9aab5e4ebb71770c67fa266ef651b2e8dcbbd6.tar.bz2 |
peniquitous.js: Try to listen for & simulate key events
Doesn't quite work. We're correctly listening for Ctrl-N/P presses, and
seemingly correctly simulating corresponding up/down key presses, but it
seems that the 'content scripts' can't trigger page events.
See
https://developer.chrome.com/extensions/content_scripts#execution-environment
which describes the "isolated" execution environment of Chrome content
scripts.
Spent a while trying to figure out why this wasn't working. Finally
looked back through my Chrome console history and found out what I did
previously to get it working and it turns out that was the same thing
I'm doing here. Finally had a moment of inspiration that prompted me to
look into the different execution environment thing.
Found a useful Stack Overflow post that descibes how to inject a script
onto the page, which will give me access to the page's context:
http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script
Doing that should get this working, so that's what I'm going to try
next.
-rw-r--r-- | manifest.json | 2 | ||||
-rw-r--r-- | peniquitous.js | 14 |
2 files changed, 13 insertions, 3 deletions
diff --git a/manifest.json b/manifest.json index f293dd7..68f6489 100644 --- a/manifest.json +++ b/manifest.json @@ -7,6 +7,6 @@ "content_scripts": [{ "matches": ["<all_urls>"], - "js": ["peniquitous.js"] + "js": ["lib/mousetrap/tests/libs/key-event.js", "peniquitous.js"] }] } diff --git a/peniquitous.js b/peniquitous.js index 2c01232..c858f42 100644 --- a/peniquitous.js +++ b/peniquitous.js @@ -8,8 +8,18 @@ var all_inputs = document.querySelectorAll('input[type="text"]'); for (var i = 0; i < all_inputs.length; i++) { - all_inputs[i].addEventListener('focus', function() { - // Bind keyboard event + all_inputs[i].addEventListener('keyup', function(e) { + if (e.ctrlKey && e.keyCode === 80) { + KeyEvent.simulate(0, 38, [], e.target); + } + else if (e.ctrlKey && e.keyCode === 78) { + KeyEvent.simulate(0, 40, [], e.target); + } }); } + + window.setTimeout(function() { + KeyEvent.simulate(0, 40, [], document.getElementById('lst-ib')); + console.log('fired'); + }, 6000); })(); |