Pregunta

I would like to trigger a Javascript function on an already existing website I don't own by pressing a key.

Example : trigger my_function('my parameter 1'); when I press the 1 key on the keyboard.

I can type manually the command on the Firefox's debugger console, it works. But I can't figure a simple way to trigger the Javascript function from an external program. Can I use Firefox command line ? EventGhost ? AutoHotKey ? Should I write a Firefox extension ?

If your solution implies the use of another web browser you're welcome !

¿Fue útil?

Solución 2

OK ! I finally created an AutoHotkey script and Chrome because :

  • I met focus problems with Javascript injection when Flash was full screen
  • Chrome accepts javascript commands typed in Omnibox

Here's how the script manages a channel change request :

  • Press Escape to exit full screen
  • Click in a blank zone of the webpage to be sure it gains focus
  • Press F6 to activate Omnibox
  • Past Javascript code needed to change channel
  • Click in the flash video area
  • Press F to return full screen

I don't paste the script here because it's long and Stack Overflow code parser really don't like AutoHotkey code ! Just ask if you want it.

@EdoPut suggestion is way cleaner, but it didn't solved the focus problem when Flash went full screen. Anyway thanks again EdoPut for your efforts.

Otros consejos

I suggest you to look at this https://api.jquery.com/keydown/. Jquery could be the solution but the problem isn't clear. The site you're talking about is yours or you want to inject this trigger event from outside? Do you want this trigger to be injected every time?

If it's just a one-night stand you can use the browser console but if it's not you could be interested in developing an add-on or a greasemonkey extention.

Update: Yo can use eventghost, specifically the window plugin http://www.eventghost.org/docs/pluginlist.html, to trigger a keypress on firefox. Now you have to inject a javascript event handler in the page every time you visit it and most of the work is done.

Update 2: Here's the basic add-on, ;) have fun

var self = require('sdk/self');
exports.main = function() {
    require('sdk/page-mod').PageMod({
        include: '*.org',
        contentScript: 'document.onkeypress = function (e) {console.log(e.which);}'
    });
}

To use this thing download the cfx-tools from the mozilla site (sorry I can't post more than 2 links) follow the first tutorial ( be sure to read how to use the cfx tools) and when you're ready use the code I posted above.

  1. replace the "include" value with the name of the site of your interest
  2. Replace console.log(e.which) with your code

Last suggestion:

document.onkeypress = function (e) { if(e.which == 49){ /* trigger my function if I press 1*/}}

Bye

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top