0

The following code words if run in the console itself:

    var $inputbox = $('input#inputfield');
    var SPACE_KEYCODE = 32;
    var space_down = $.Event( 'keyup', { which: SPACE_KEYCODE } );
    $inputbox.trigger(space_down)

I can see the event being triggered and the page responding.

However when running the same code in a content script via a Chrome extension, it fails silently. Logging the results of '$inputbox.trigger(space_down)' shows it correctly returning the element.

The intention here is to have the existing page JS respond to the keyboard event from the extension. Is this possible?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494

1 Answers1

1

Although I haven't been able to find documentation about whether events are distinct between the content script JS 'world' and the origin site's world, I made the following in the content script to load some JS into window.location, making it run in the content of the origin site.

// In order to send keyboard events, we'll need to send them from the page's JS
var load_into_page_context = function(file) {
    var file_url = chrome.extension.getURL(file);
    $.get(file_url, function(script_contents) {
        window.location = 'javascript:'+script_contents
    })

}

    load_into_page_context("injectme.js");

This will load injectme.js (bundled with the extension) into the window.location, and make the generated keyboard events activate the origin site's event handlers.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 1
    Just thought Id point out theres quite a few ways to inject code into the page.... http://stackoverflow.com/questions/9906083/chrome-extension-invoke-page-script-by-injection http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script and Ive seen a few other variations on it all – PAEz Mar 29 '12 at 13:52