Tweet your ideas through JavaScript


Do you have a website and would like to post your tweets from it ? Or maybe you want to create a widget that posts weather on your twitter account?

I encountered this situation and overcome it with the following script. It is pretty simple, but beware that JavaScript is run on CLIENT side, thus any code must be sent in clear.Since it is needed that you provide a password for your account, would not be so wise to put it in clear.

NOTE: I use it for a Vista sidebar gadget, so only IE code is below. For FireFox or Opera the modifications are trivial.

So let’s code speak for itself:

function TweetIt(textToTweet ){
  // first some configurations
  var username = "twitter_user";
  var password = "twitter_pass";  
  //create the request
  var request = new ActiveXObject("Msxml2.XMLHTTP.3.0");
  
  // post the tweet
  request.open(
    "POST", //we want to post the update
    "https://twitter.com/statuses/update.xml?status=" + textToTweet, 
    true, 
    username, 
    password); 
   
   //document.write("https://twitter.com/statuses/update.xml?status=" + txt);
   
  // receive the response asyncroniously
  request.onreadystatechange = function()
  {
    if(request.readyState==4){ 
      if(request.status>=400){ 
      // post some error or log it
      }
    }
  }
   
  // send the post data
  request.send();
}

Leave a comment

Your email address will not be published. Required fields are marked *