Last active
December 14, 2015 14:18
-
-
Save chanwit/5099358 to your computer and use it in GitHub Desktop.
Toodledo command via Twitter
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
Cu.import("resource://ubiquity/modules/oauth.js"); | |
const TWITTER_STATUS_MAXLEN = 140; | |
CmdUtils.CreateCommand({ | |
name: ["todo", "task"], | |
description: ("Add a task to Toodledo at most " + TWITTER_STATUS_MAXLEN + " characters."), | |
help: ("You'll need a <a href=\"http://twitter.com\">Twitter account</a>," + | |
" to add task. If the account isn't already authorized," + | |
" you'll be asked for password."), | |
icon: "http://toodledo.com/favicon.ico", | |
arguments: [ | |
{role: "object", label: _("status"), nountype: noun_arb_text}, | |
{role: "alias", label: _("user"), nountype: noun_type_twitter_user}], | |
preview: function twitter_preview(previewBlock, args) { | |
var statusText = args.object.text; | |
if (!statusText) return void this.previewDefault(previewBlock); | |
var username = args.alias.text || (Bin.twitterLastLogin() || 0).username; | |
var remaining = TWITTER_STATUS_MAXLEN - statusText.length; | |
var previewTemplate = ( | |
_("Updates Twitter status of ${username} to:") + | |
"<p><strong class='status'>${status}</strong></p>" + | |
_("Characters remaining: ${chars}") + | |
(remaining >= 0 ? "" : | |
"<br/><strong class='warning'>" + | |
_("The last ${truncate} characters will be truncated!", | |
{truncate: "<b class='truncate'>" + -remaining + "</b>"}) + | |
"</strong>") + | |
"<p><small>" + | |
_("tip: tweet @mozillaubiquity for help") + | |
"</small></p>"); | |
previewBlock.innerHTML = ( | |
"<div class='twitter'>" + | |
CmdUtils.renderTemplate(previewTemplate, { | |
status: Utils.escapeHtml(statusText), | |
username: ("<b class='username'>" + | |
(username ? Utils.escapeHtml(username) : "??") + "</b>"), | |
chars: "<b class='remaining'>" + remaining + "</b>", | |
}) + | |
"</div>"); | |
}, | |
execute: function twitter_execute({object: {text}, alias}) { | |
var me = this; | |
if (!text) return me._show(_("requires a status to be entered")); | |
var oldText = text; | |
text = "d toodledo " + text + " *Inbox"; | |
var login = alias.data || Bin.twitterLastLogin() || {}; | |
me._auth(login, function twitter_tweet(username, key, secret) { | |
me._post({ | |
url: "https://api.twitter.com/1/statuses/update.json", | |
data: {status: text = text.slice(0, TWITTER_STATUS_MAXLEN)}, | |
dataType: "json", | |
success: function twitter_success(res) { | |
me._show(oldText, username, text === res.text && function twitter_onclick() { | |
Utils.openUrlInBrowser( | |
"http://twitter.com/" + username + "/status/" + res.id_str); | |
}); | |
}, | |
error: function twitter_error(xhr) { | |
me._show(_("error - status not updated") + " / " + | |
xhr.status + " " + xhr.statusText, | |
username); | |
}, | |
}, {token: key, tokenSecret: secret}); | |
Bin.twitterLastLogin({username: username, password: ""}); | |
}); | |
}, | |
_show: function twitter_show(text, user, cb) { | |
var title = this.name; | |
if (user) title += " \u2013 " + user; | |
displayMessage({icon: this.icon, title: title, text: text, onclick: cb}); | |
}, | |
_post: function twitter_post(settings, accessor) { | |
settings.type = "POST"; | |
accessor.consumerKey = "C6h2HUUjmOcqXTtPRYqAVg"; | |
accessor.consumerSecret = "AYNHPfkpm5lL3uPKXRCuzGFYItA8EOWlrkajyEBOd6s"; | |
return $.ajax(OAuth.completeAjaxSettings(settings, accessor)); | |
}, | |
_auth: function twitter_auth({username, password}, cb) { | |
username = username && username.toLowerCase(); | |
var keys = CmdUtils.loadPassword("TwitterOAuth", username); | |
if (keys) return cb.apply(this, [username].concat(keys.split(" "))); | |
const APIURL = "https://api.twitter.com/oauth/access_token"; | |
if (!username || !password) { | |
let un = {value: username || ""}; | |
let pw = {value: password || ""}; | |
let ok = Utils.PromptService.promptUsernameAndPassword( | |
context.chromeWindow, this.name, APIURL, un, pw, null, {}); | |
if (!ok || !un.value || !pw.value) return; | |
username = un.value.toLowerCase(); | |
password = pw.value; | |
} | |
var me = this; | |
this._post({ | |
url: APIURL, | |
data: { | |
x_auth_mode: "client_auth", | |
x_auth_username: username, | |
x_auth_password: password, | |
}, | |
success: function twitter_xAuth_success(res) { | |
var {oauth_token, oauth_token_secret} = Utils.urlToParams(res); | |
CmdUtils.savePassword({ | |
name: "TwitterOAuth", | |
username: username, | |
password: oauth_token + " " + oauth_token_secret, | |
}); | |
cb.call(me, username, oauth_token, oauth_token_secret); | |
}, | |
error: function twitter_xAuth_error(xhr) { | |
var status = xhr.status + " " + xhr.statusText; | |
Cu.reportError("Twitter xAuth for " + username + ": " + status); | |
me._show(status, username); | |
Bin.twitterLastLogin(null); | |
}, | |
}, {}); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment