Skip to content

Instantly share code, notes, and snippets.

@yoshimov
Created March 25, 2009 02:51
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 yoshimov/84524 to your computer and use it in GitHub Desktop.
Save yoshimov/84524 to your computer and use it in GitHub Desktop.
Share quoted text and url and comment on Twitter.
Cu.import("resource://ubiquity/modules/oauth.js");
const TWITTER_STATUS_MAXLEN = 140;
CmdUtils.CreateCommand({
names: ["share url on twitter", "twitter url"],
description: "Share quoted text and url on Twitter.",
icon: "http://yoshimov.com/favicon.ico",
homepage: "http://yoshimov.com",
author: { name: "Yoshimov", email: "yoshimov@pobox.com"},
arguments: [
{role: "object", label: _("status"), nountype: noun_arb_text},
{role: "alias", label: _("user"), nountype: noun_type_twitter_user}],
preview: function(block, args){
this._createStatusText(args.object.text, function(statusText) {
if (!statusText) {
this.previewDefault(block);
return;
}
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>"));
block.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"));
this._createStatusText(text, function(textStatus) {
var login = alias.data || Bin.twitterLastLogin() || {};
me._auth(login, function twitter_tweet(username, key, secret) {
me._post({
url: "https://twitter.com/statuses/update.json",
data: {status: textStatus},
dataType: "json",
success: function twitter_success(res) {
me._show(
text, username, text === res.text && function twitter_onclick() {
Utils.openUrlInBrowser(
"http://twitter.com/" + username + "/status/" + res.id);
});
},
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: "dummy"});
});
});
},
_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();
for each (let saved in CmdUtils.retrieveLogins("TwitterOAuth")) {
if (saved.username !== username) continue;
let [key, secret] = saved.password.split(" ");
return cb.call(this, username, key, secret);
}
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);
},
}, {});
},
_createStatusText: function(inputText, func) {
var d = CmdUtils.getDocument();
var urlOrg = d.location.href;
var selectedText = CmdUtils.getSelection();
var callback = function(urlText) {
if (selectedText == null || selectedText.length < 1) {
func(inputText + " " + urlText);
return;
}
var template = '${status} "${selectedText}" ${url}';
var data = {
status: inputText,
selectedText: selectedText,
url: urlText
};
var statusText = CmdUtils.renderTemplate(template, data);
func(statusText);
};
if (inputText.length + urlOrg.length + (selectedText != null ? selectedText.length : 0) + 4 > TWITTER_STATUS_MAXLEN) {
jQuery.get(this._tinyurlapi(urlOrg), callback);
} else {
callback(urlOrg);
}
},
_tinyurlapi: function(url)("http://tinyurl.com/api-create.php?url=" +
encodeURIComponent(url)),
});
@yoshimov
Copy link
Author

Change to use OAuth to access Twitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment