Jun 26 2012
Simulating Keypresses / keystrokes with Javascript using Greesemonkey in Google Docs
This is a quick post about creating dispatching events using javascript and specifically key press events (for use with greesemonkey or whatnot and firefox).
Note that this will not work for the latest versions of firefox in many instances. I believe this is due to security updates.
Personally I am using this dispatch keypresses to google docs from a greesemonkey script.
Below the code for typing “hello world” to google docs from a greesemonkey user script.
I suspect there may be a more efficient ways though I noticed not all browsers support the same API 🙁 .
Specifically, on http://help.dottoro.com/ljbwbehw.php we see that initKeyEvent is only available in firefox. On the other hand, initTextEventis available in IE, chrome, and Safari.
Sample Greesemonkey Script:
// ==UserScript==
// @name Type in google docs
// @namespace http://http://xn--mxahaychr6a.com/
// @description Demo
// @include https://docs.google.com/document/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js
// ==/UserScript==
//find the line spacing button
var someButton = document.getElementById(“lineSpacingMenuButton”);
var button = $(“<button>Encrypt</button>”);
button.insertAfter(someButton);
$(button).click(function() {
var event_object = document.getElementsByTagName(“iframe”)[0].contentDocument;
//CTRL + A
var pressEvent = document.createEvent (“KeyboardEvent”); //https://developer.mozilla.org/en/DOM/event.initKeyEvent
pressEvent.initKeyEvent (“keypress”, true, true, window,
true, false, false, false,
65, 0);
event_object.dispatchEvent (pressEvent);
//Must find a better way to type than char by char
var ct=”hello world”;
var i = 0;
for(;i<ct.length; i++)
{
var pressEvent = document.createEvent (“KeyboardEvent”);
pressEvent.initKeyEvent (“keypress”, true, true, window,
false, false, false, false,
0, ct.charCodeAt (i));
event_object.dispatchEvent (pressEvent);
}
});
https://developer.mozilla.org/en/DOM/event.initKeyEvent
Jul 17, 2012 @ 04:45:30
hi
I have this wonderful idea of a webpage transliterating english to hindi regardless of availability of internet.
I have made some progress.Please see http://www.gidforums.com/showthread.php?p=96786
Now I am trying to copy contents of text area to clipboard.
I have thought this
set focus to textarea
Simulate keystroke ctrl+a followed by ctr+c then ctrl+end
This way user can paste the contents of text area using ctrl+v anywhere.
I have tried this
[code]
function setFocus()
{ document.hien.input.focus();
var event_object = document.getElementById(“t1”);
var pressEvent = document.createEvent (“KeyboardEvent”);
pressEvent.initKeyEvent (“keypress”, true, true, window,true, false, false, false, 65, 0);
event_object.dispatchEvent (pressEvent);
}
[/code]
while body of webpage has
[code]
label>Transliteration is: </label><label id="state">OFF</label><br /><br />
<form name="hien">
<textarea id="t1" rows="10" cols="45" type="text" name="input" style="overflow:hidden" onkeyup="if(document.hien.lang[1].checked==true){checkKey(event)};"></textarea><br>
<input type="radio" name="lang" value="english" onclick="toggleState(this.value);" /> english
<input type="radio" name="lang" value="hindi" onclick="toggleState(this.value);" /> hindi<br>
</form>
<input type=BUTTON value="Click to Set focus in textarea" name="mybutton" onClick="setFocus();">
[/code]
Please tell me , where I am going wrong ?
Thank You.
Jul 17, 2012 @ 17:32:57
Howdy, from a preliminary search I can you it’s not your fault the code does not play but there have been bug reports in various versions of firefox. However the more I read, the more I think this is a security feature to stop you from hijacking the users keyboard. Please note there is a huge discussion about sending to the clipboard and how to do it e.g. http://brooknovak.wordpress.com/2009/07/28/accessing-the-system-clipboard-with-javascript/.
Furthermore, the code on http://help.dottoro.com/external/examples/ljbwbehw/initKeyEvent_1.htm
works for firefox 3.6.24 . I have a portable legacy version from: http://portableapps.com/news/2011-11-10_-_firefox_portable_3.6.24_released for testing.
The same code however does not work with firefox 13.
To make matters worse I see quite a few bug reports related to this though they seem to be around from even 2007:
https://bugzilla.mozilla.org/buglist.cgi?quicksearch=key+events+dispatch
Final conclusion I have reached is that it’s a security update.
Feb 13, 2013 @ 20:37:53
Hi,
I try to simulate ctrl+a and it workd fine. But when I similate ctrl+v it doesn’t work.
My code is:
var pressEvent = document.createEvent (“KeyboardEvent”);
pressEvent.initKeyEvent (“keypress”, true, true, window, true, false, false, false, 86, 0);
event_object.dispatchEvent (pressEvent);
Do yo know what could be wrong??
Feb 14, 2013 @ 20:45:05
Hi, this is a security feature. You cannot dispatch CTRL+C or CTRL+V because you can steal/modify the content in a users clipboard.
Otherwise you would be able to steal the previous copied text and send it via an ajax request (etc) to a webserver for storage.
However, you could use a hidden element to simulate copying and pasting if this is for a web application.
KR,
Menelaos
Nov 13, 2013 @ 02:42:07
Browser security stops javascript code from dispatching events that will pop up browser dialogs such as search.