Created
October 1, 2009 16:27
-
-
Save mooz/199079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
key.setViewKey(['C-c', 't'], function (aEvent, aArg) { | |
prompt.read("tweet:", function (aTweet) { | |
if (aTweet == null) | |
return; | |
// get user name / password | |
var passwordManager = Cc["@mozilla.org/login-manager;1"] | |
.getService(Ci.nsILoginManager); | |
var logins = passwordManager | |
.findLogins({}, "http://twitter.com", "https://twitter.com", null); | |
var username = "", password = ""; | |
if (logins.length) | |
[username, password] = [logins[0].username, logins[0].password]; | |
// create request | |
var xhr = new XMLHttpRequest(); | |
// pretty notification | |
var alertsService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService); | |
xhr.onreadystatechange = function (aEvent) { | |
if (xhr.readyState == 4) { | |
if (xhr.status != 200) { | |
alertsService.showAlertNotification(null, "I'm sorry...", "Failed to tweet", | |
false, "", null); | |
return; | |
} | |
// get safe eval for JSON parsing | |
var evalFunc = window.eval; | |
try { | |
var sandbox = new Components.utils.Sandbox("about:blank"); | |
if (Components.utils.evalInSandbox("true", sandbox) === true) { | |
evalFunc = function(text) { | |
return Components.utils.evalInSandbox(text, sandbox); | |
}; | |
} | |
} catch (e) {} | |
var status = evalFunc("(" + xhr.responseText + ")"); | |
var icon_url = status.user.profile_image_url; | |
var user_name = status.user.name; | |
var message = status.text; | |
alertsService.showAlertNotification(icon_url, user_name, message, | |
false, "", null); | |
} | |
}; | |
// 3rd value true means Asynchronous connection | |
xhr.open("POST", "http://twitter.com/statuses/update.json", true, username, password); | |
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); | |
xhr.setRequestHeader("X-Twitter-Client", "KeySnail"); | |
xhr.setRequestHeader("X-Twitter-Client-Version", "0.1"); | |
xhr.send("status=" + aTweet); | |
}); | |
}, 'Tweet!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment