Created
November 20, 2008 03:17
-
-
Save hayamiz/26919 to your computer and use it in GitHub Desktop.
undefined
This file contains hidden or 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
CmdUtils.CreateCommand({ | |
name: "otwitter", | |
synonyms: ["tweet"], | |
icon: "http://assets3.twitter.com/images/favicon.ico", | |
takes: {status: noun_arb_text}, | |
modifiers: { "as" : noun_type_twitter_user }, | |
description: "Update your twitter status and Outputz records.", | |
help: "You'll need a <a href=\"http://twitter.com\">Twitter account</a>. If you're not already logged in you'll be asked to log in.", | |
preview: function(previewBlock, directObj, mods) { | |
// these are converted in the Twitter database anyway, and counted as 4 characters | |
var statusText = directObj.text | |
.replace("<", "<") | |
.replace(">", ">"); | |
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 = "<span style='color: red;'><br />The last <b>${truncate}</b> characters will be truncated!</span>"; | |
var previewData = { | |
status: statusText, | |
chars: TWITTER_STATUS_MAXLEN - statusText.length | |
}; | |
if(mods.as){ | |
previewData.username = mods.as.text; | |
} | |
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, mods) { | |
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 | |
//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 | |
}; | |
function sendMessage() { | |
jQuery.ajax({ | |
type: "POST", | |
url: "http://outputz.com/recorder/post", | |
data: "size="+updateParams.status.length+"&uri="+encodeURIComponent("http://twitter.com/statuses/update.json"), | |
error: function() { | |
displayMessage("Outputz error - not updated"); | |
}, | |
success: function() { | |
// do nothing | |
} | |
}); | |
jQuery.ajax({ | |
type: "POST", | |
url: updateUrl, | |
data: updateParams, | |
dataType: "json", | |
error: function() { | |
displayMessage("Twitter error - status not updated"); | |
}, | |
success: function() { | |
var msg = updateParams.status.match(/^d /) ? | |
"Twitter direct message sent" : | |
"Twitter status updated"; | |
displayMessage(msg); | |
}, | |
username: login.username, | |
password: login.password | |
}); | |
} | |
var login; | |
if (mods.as && mods.as.text && mods.as.data) { | |
login = mods.as.data; | |
sendMessage(); | |
} else if (mods.as && mods.as.text && !mods.as.data) { | |
displayMessage("I can't find the password for the " + | |
"Twitter user '" + mods.as.text + "'. Please " + | |
"log in as this user and tell Firefox " + | |
"to remember your password."); | |
return; | |
} else { | |
// Try to figure out who the currently logged-in Twitter user is. | |
jQuery.get( | |
"http://twitter.com/privacy", | |
null, | |
function(data) { | |
var parser = Cc["@mozilla.org/xmlextras/domparser;1"] | |
.createInstance(Ci.nsIDOMParser); | |
data = parser.parseFromString(data, "text/xml"); | |
var username = jQuery("meta[name=session-user-screen_name]", | |
data).attr("content"); | |
if (!username) { | |
displayMessage("Please log in to Twitter or specify a " + | |
"username."); | |
return; | |
} | |
var suggs = noun_type_twitter_user.suggest(username, ""); | |
if (!suggs[0].data) { | |
displayMessage("I can't find the password for the " + | |
"Twitter user '" + username + "'. Please " + | |
"log in again as this user and tell Firefox " + | |
"to remember your password."); | |
return; | |
} | |
login = suggs[0].data; | |
sendMessage(); | |
}, | |
"text" | |
); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment