Skip to content

Instantly share code, notes, and snippets.

@asafd1
Created April 22, 2010 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asafd1/375975 to your computer and use it in GitHub Desktop.
Save asafd1/375975 to your computer and use it in GitHub Desktop.
twitter commands
const TWITTER_STATUS_MAXLEN = 140;
CmdUtils.CreateCommand({
names: ["tweet-page", "tp"],
arguments: [
{role: "object", label: 'status', nountype: noun_arb_text},
{role: "alias", nountype: noun_type_twitter_user}
],
icon: "http://twitter.com/favicon.ico",
description:
"Sets your Twitter status to the current page plus 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, args) {
var {title, URL} = context.focusedWindow.document;
var statusText = (args.object ? args.object.text + " " + URL: URL);
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/>" +
"<b class='status'>${status}</b><br/><br/>" +
_("Characters remaining: <b>${chars}</b>") +
"<p><small>"+_("tip: tweet @mozillaubiquity for help")+"</small></p></div>");
var truncateTemplate = (
"<strong>"+_("The last <b>${truncate}</b> characters will be truncated!")+"</strong>");
var previewData = {
status: <>{statusText}</>.toXMLString(),
username: usernameText && _("(For user <b>${usernameText}</b>)"),
chars: TWITTER_STATUS_MAXLEN - statusText.length
};
var previewHTML = CmdUtils.renderTemplate(
CmdUtils.renderTemplate(previewTemplate, previewData),
{usernameText:usernameText});
if (previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(args) {
var {title, URL} = context.focusedWindow.document;
var statusText = args.object.text + " " + URL;
if(statusText.length < 1) {
this._show(_("requires a status to be entered"));
return;
}
var updateUrl = "http://api.twitter.com/1/statuses/update.xml";
var updateParams = {
source: "ubiquity",
status: statusText
//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: "xml",
error: function() {
me._show(_("error - status not updated"));
},
success: function() {
me._show(/^d /.test(statusText)
? _("direct message sent")
: _("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});
}
});
CmdUtils.CreateCommand({
names: ["tweet", "share using twitter"],
arguments: [
{role: "object", label: 'status', nountype: noun_arb_text},
{role: "alias", nountype: noun_type_twitter_user}
],
icon: "http://twitter.com/favicon.ico",
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, 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/>" +
"<b class='status'>${status}</b><br/><br/>" +
_("Characters remaining: <b>${chars}</b>") +
"<p><small>"+_("tip: tweet @mozillaubiquity for help")+"</small></p></div>");
var truncateTemplate = (
"<strong>"+_("The last <b>${truncate}</b> characters will be truncated!")+"</strong>");
var previewData = {
status: <>{statusText}</>.toXMLString(),
username: usernameText && _("(For user <b>${usernameText}</b>)"),
chars: TWITTER_STATUS_MAXLEN - statusText.length
};
var previewHTML = CmdUtils.renderTemplate(
CmdUtils.renderTemplate(previewTemplate, previewData),
{usernameText:usernameText});
if (previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},
execute: function(args) {
var statusText = args.object.text;
if(statusText.length < 1) {
this._show(_("requires a status to be entered"));
return;
}
var updateUrl = "http://api.twitter.com/1/statuses/update.xml";
var updateParams = {
source: "ubiquity",
status: statusText
//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: "xml",
error: function() {
me._show(_("error - status not updated"));
},
success: function() {
me._show(/^d /.test(statusText)
? _("direct message sent")
: _("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});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment