I am working on a Greasemonkey script that will automatically submit a tweet when the user presses the 'enter' key. The script works perfectly on a basic HTML page with some help from tips I found on this website. However, when I attempt to use the same code on my Twitter page, the alert only triggers if there is no tweet already being composed.
document.onkeyup = function(event){
var keyCode;
if (window.event) // IE/Safari/Chrome/Firefox(?)
{
keyCode = event.keyCode;
}
else if (event.which) // Netscape/Firefox/Opera
{
keyCode = event.which;
}
if (keyCode == 13){
alert("Enter pressed");
}
}
To be more specific in detecting keypress events, I attempted to test for a key event within the new tweet textarea:
document.getElementsByClassName("twitter-anywhere-tweet-box-editor")[0].onkeyup = function(event)
However, this event does not seem to trigger. I also tried selecting the element by tag:
document.getElementsByTagName("textarea")[0].onkeyup = function(event)
Unfortunately, this method did not work either. I suspect it may be related to the new tweet window not loading initially at window.onload(). Any thoughts or suggestions?