Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity Source Tip Author Tutorial

1,539 bytes added, 05:54, 22 June 2009
Twitter: Putting It All Together
= Twitter: Putting It All Together =
We've now covered everything we need to cover in order to write a command that allows us to [http://en.wikipedia.org/wiki/Twitter Twitter] from Ubiquity. The code below is the actual source code of the Twitter command as it appears in Ubiquity 0.5. Many thanks to [http://theunfocused.net/moz/ubiquity/verbs/ Blair McBride] for writing this command. This The source code is a fully functioning command: bit more intricate than anything we've seen so far, but it's built using exactly the browser takes care same basic pieces -- and demonstrates nearly all of the odd edge cases, like not being logged those pieces inaction.
<pre>
// max of 140 chars is recommended, but it really allows 160const TWITTER_STATUS_MAXLEN = 160140;
CmdUtils.CreateCommand({
namenames: ["twitter", "tweet", "share using twitter"], takesarguments: [ {role: "object", label: 'status', nountype: noun_arb_text}, {role: "alias", nountype: noun_type_twitter_user} ], homepageicon: "http://theunfocusedtwitter.net/moz/ubiquity/verbscom/favicon.ico", author: {namedescription: "Blair McBrideSets your Twitter status to a message of at most 160 characters.", homepage help: ("You'll need a <a href=\"http://theunfocusedtwitter.netcom\">Twitter account</a>," + " obviously. If you're not already logged in"},+ license: "MPLyou'll be asked to log in."), preview: function(previewBlock, args) { var statusText= (args.object ? args.object.text : ''); var usernameText = ""; if (args.alias) { usernameText = args.alias.text; } else if (args.as) { usernameText = args.as.text; } var previewTemplate = ( "<div class='twitter'>"+_("Updates your Twitter status ${username} to: ")+"<br/>" + "<bclass='status'>${status}</b><br /><br />" + _("Characters remaining: <b>${chars}</b>") + "<p><small>"+_("tip: tweet @mozillaubiquity for help")+"</small></p></div>"); var truncateTemplate = ( "<br /strong>"+_("The last <b>${truncate}</b> " + "characters will be truncated!")+"</strong>");
var previewData = {
status: <>{statusText}</>.texttoXMLString(), username: usernameText && _("(For user <b>${usernameText}</b>)"), chars: TWITTER_STATUS_MAXLEN - statusText.text.length
};
var previewHTML = CmdUtils.renderTemplate( CmdUtils.renderTemplate(previewTemplate, previewData), previewData {usernameText:usernameText}); if(previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(statusTextargs) { var statusText = args.object.text; if(statusText.text.length < 1) { displayMessagethis._show(_("Twitter requires a status to be entered"));
return;
}
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: statusText.text //dont cut the input since sometimes, the user selects a big url, //and the total lenght is more than 140, but tinyurl takes care of that
};
var me = this;  function sendMessage() { jQuery.ajax({ type: "POST", url: updateUrl, data: updateParams, dataType: "json", error: function() { displayMessage me._show(_("Twitter error - status not updated")); }, success: function() { displayMessage me._show(/^d /.test(statusText) ? _("direct message sent") : _("Twitter status updated")); }, username: login.username, password: login.password }); }  var login; var alias = args.alias; if (alias && alias.text && alias.data) { login = alias.data; sendMessage(); } else { login = {username: null, password: null}; if (alias && alias.text) login.username = alias.text; sendMessage(); } }, _show: function(txt){ displayMessage({icon: this.icon, title: this.name, text: txt});
}
});
1,007
edits

Navigation menu