Skip to content

Instantly share code, notes, and snippets.

@gialloporpora
Created March 16, 2009 20:10
Show Gist options
  • Save gialloporpora/80046 to your computer and use it in GitHub Desktop.
Save gialloporpora/80046 to your computer and use it in GitHub Desktop.
/* TODO
From Abi:
I think the ones I most often use would be to check the current status
of a specific friend (or maybe, the last 3 statuses). The ability to
check your friends timeline as a whole would also be nice.
*/
// max of 140 chars is recommended, but it really allows 160... but that gets truncated on some displays? grr
const TWITTER_STATUS_MAXLEN = 140;
CmdUtils.CreateCommand({
name: "twitter",
synonyms: ["tweet"],
icon: "http://assets3.twitter.com/images/favicon.ico",
takes: {status: noun_arb_text},
modifiers: {},
description: "Sets your Twitter status to a message of at most 160 characters.",
help: "You'll need a <a href=\"http://twitter.com\">Twitter account</a>, obviously. If you're not already logged in" +
" you'll be asked to log in.",
preview: function(previewBlock, directObj) {
// these are converted in the Twitter database anyway, and counted as 4 characters
var statusText = directObj.text
.replace("<", "&lt;")
.replace(">", "&gt;");
var previewTemplate = "Updates your Twitter status to: <br /><b>${status}</b><br /><br />Characters remaining: <b>${chars}</b> <p style='font-size:11px'> tip: tweet @mozillaubiquity for help </p>";
var truncateTemplate = "<br />The last <b>${truncate}</b> characters will be truncated!";
var previewData = {
status: statusText,
chars: TWITTER_STATUS_MAXLEN - statusText.length
};
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
if(previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(directObj) {
var statusText = directObj.text;
if(statusText.length < 1) {
displayMessage("Twitter requires a status to be entered");
return;
}
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: statusText.slice(0, TWITTER_STATUS_MAXLEN)
};
jQuery.ajax({
type: "POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error: function() {
displayMessage("Twitter error - status not updated");
},
success: function() {
displayMessage("Twitter status updated");
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment