简体   繁体   中英

Send tweet using 'enter' key (Greasemonkey)

I'm attempting to create a Greasemonkey script that can submit a tweet when a user hits the 'enter' key. I've gotten this to work fine on a simple HTML page (with the help of a few excellent tips on this site). However, when I try to use the code on my twitter page, the alert only fires if a tweet is not currently being authored.

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");
    }
}   

My next thought was to test for a more specific keypress event. So I tried testing for a key event within the new tweet textarea:

document.getElementsByClassName("twitter-anywhere-tweet-box-editor")[0].onkeyup = function(event)

...but this event never seems to fire. I also tried grabbing the element by tag:

document.getElementsByTagName("textarea")[0].onkeyup = function(event)

...but not dice there either. I wonder if this has to do with the fact that the new tweet window is not loaded from the get-go at window.onload(). Thoughts?

I got it thanks to this post . I've also posted the full Greasemonkey script here .

setInterval (function() { checkForTweetbox (); }, 500);

function checkForTweetbox () {
    var tweetbox = document.querySelector ('div.tweet-box textarea');   //check for new tweet window
    if (tweetbox) {
        if (! tweetbox.weHaveProcessed) {
            tweetbox.weHaveProcessed    = true;
//          alert ('New tweet-box found!');
        }
    }   
    tweetbox.onkeydown = function(event){
        if(event.keyCode == 13){                                        //13 = Enter keycode
            document.querySelector ('a.primary-btn').click();           //there must be at least one character in the textarea  
        }
    }           
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM