// ==UserScript==
// @name           Twitter blend goji
// @namespace      http://polog.org/
// @include        http://twitter.com/home
// ==/UserScript==
// thx http://userscripts.org/scripts/show/9086 and youpy

(function() {
  var endpoint = 'http://goji.polog.org/api/get.json?sentence=';
  var lastIndex = 2254;
  var onclick_orig;

  var w = (typeof unsafeWindow == 'undefined') ? window : unsafeWindow;
  var submit = w.document.getElementsByClassName('update-button')[0];
  var onclick = function() {
    GM_xmlhttpRequest({
      method : "GET",
      url : endpoint + document.getElementById('status').value + '&lastindex=' + Math.floor(Math.random() * lastIndex),
      onload : function(response) {
	var json = eval('(' + response.responseText + ')');
	document.getElementById('status').value = json.sentence;
	
	submit.onclick = onclick_orig;
	submit.click();
	submit.onclick = onclick;
      }
    });
    
    return false;
  };
  
  onclick_orig = submit.onclick;
  submit.onclick = onclick;
})();
