From 5a9aab5e4ebb71770c67fa266ef651b2e8dcbbd6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 24 May 2015 16:49:47 -0400 Subject: 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. --- manifest.json | 2 +- 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": [""], - "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); })(); -- cgit v1.2.3