Skip to content

Instantly share code, notes, and snippets.

@makotokw
Created September 3, 2009 03:27
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 makotokw/180114 to your computer and use it in GitHub Desktop.
Save makotokw/180114 to your computer and use it in GitHub Desktop.
makoto_kw's Ubiquity Commands
/**
* Ubiquity Commands
* @author makoto_kw
* @see http://gist.github.com/180114
*/
Cu.import("resource://ubiquity/modules/oauth.js");
(function($){
var author = { name: 'makoto_kw', email: 'makoto.kw+ubiquity@gmail.com'};
/**
* php php-ja
* PHP Manual Quick Reference (only Japanese)
* @author makoto_kw
* @date
*/
CmdUtils.CreateCommand({
names: ['php', 'php-ja'],
icon: 'http://php.net/favicon.ico',
description: 'PHP: Manual Quick Reference',
help: 'PHP: Manual Quick Reference',
author: author,
license: 'new BSD license',
homepage: 'http://jp2.php.net/',
arguments: [
{role: 'object', nountype: noun_arb_text, label:'query'},
{role: 'source', nountype: noun_arb_text, label:'source'}
],
preview: function(pblock, arguments) {
var tpl = '<p>${help}</p><img src="http://mozshot.nemui.org/shot?${url}"/>';
pblock.innerHTML = CmdUtils.renderTemplate( tpl, {help: this.help, url: this.homepage} );
},
execute: function(arguments) {
var pattern = arguments.object.text, source = arguments.source.text;
if (!source) source = 'quickref';
var url = this.homepage + 'search.php';
Utils.openUrlInBrowser(url, {pattern: pattern, show: source});
}
});
// twitter common functions
function twitterGet(settings, accessor) {
settings.type = "GET";
accessor.consumerKey = "C6h2HUUjmOcqXTtPRYqAVg";
accessor.consumerSecret = "AYNHPfkpm5lL3uPKXRCuzGFYItA8EOWlrkajyEBOd6s";
return $.ajax(OAuth.completeAjaxSettings(settings, accessor));
}
function renderTwitterStatuses(data) {
var html = '<ul style="font-size:.8em; margin:0; padding:0; list-style:none;">';
var tpl = '<img src="${image_url}" style="height:2em;"/><strong style="margin:0 4px 0 2px; ">${screen_name}:</strong><p style="display:inline;">${status}</p><span>${date}</span>';
for (var i=0, len=data.length; i<len; i++) {
var s = data[i];
html += '<li style="border-bottom:1px solid #ddd; margin-bottom:2px; padding:4px 0 4px 0;">';
html += CmdUtils.renderTemplate(tpl, {
image_url:s.profile_image_url || s.user.profile_image_url,
screen_name: s.from_user || s.user.screen_name,
status: s.text,
date: s.created_at
});
if (s.entities) {
var user_mentions = s.entities.user_mentions;
if (user_mentions && user_mentions.length > 0) {
var users = [];
$.each(user_mentions, function(i, mention) {
users.push(mention.name);
});
html += ' by ' + users.join(', ');
}
}
html += '</li>';
}
html += '</ul>';
return html;
}
/**
* rt, retweet
* Retweet Current Page
* @author makoto_kw
* @date
*/
CmdUtils.CreateCommand({
names: ['rt', 'retweet'],
icon: 'http://twitter.com/favicon.ico',
description: 'retweet',
author: author,
license: 'new BSD license',
homeUrl: 'http://twitter.com/home',
arguments: [
{role: 'object', nountype: noun_arb_text, label:'new tweet'},
{role: 'source', nountype: noun_arb_text , label:'retweet'},
],
preview: 'Inserts status (tweet).',
execute: function(arguments) {
var rt = arguments.source.text || Application.activeWindow.activeTab.document.location;
var status = arguments.object.text + ' RT ' + rt;
Utils.openUrlInBrowser(this.homeUrl + Utils.paramsToString({status: status}));
}
});
/**
* twitter-friends
* Display twitter your timeline
* @author makoto_kw
* @date 2010-09-27
*/
CmdUtils.CreateCommand({
names: ['twitter-friends'],
icon: 'http://twitter.com/favicon.ico',
description: 'Display twitter your timeline',
author: author,
license: 'new BSD license',
homeUrl: 'http://twitter.com/home',
arguments: [{role: "alias", label: _("user"), nountype: noun_type_twitter_user}],
preview: function(pblock, args) {
var twitter = CmdUtils.getCommand('twitter');
var login = args.alias.data || Bin.twitterLastLogin() || {};
twitter._auth(login, function twitter_tweet(username, key, secret) {
twitterGet({
url: "http://api.twitter.com/1/statuses/friends_timeline.json",
data: {count:10},
dataType: "json",
success: function twitter_success(data) {
pblock.innerHTML = renderTwitterStatuses(data);
},
error: function twitter_error(xhr) {
twitter._show(_("error - status not retrieved") + " / " +
xhr.status + " " + xhr.statusText,
username);
},
}, {token: key, tokenSecret: secret});
Bin.twitterLastLogin({username: username, password: "dummy"});
});
},
execute: function(arguments) {
Utils.openUrlInBrowser(this.homeUrl);
}
});
/**
* twitter-mentions
* Display twitter replay messages
* @author makoto_kw
* @date 2010-09-27
*/
CmdUtils.CreateCommand({
names: ['twitter-mentions'],
icon: 'http://twitter.com/favicon.ico',
description: 'Display twitter reply messages',
author: author,
license: 'new BSD license',
url: 'http://twitter.com/#replies',
arguments: [{role: "alias", label: _("user"), nountype: noun_type_twitter_user}],
preview: function(pblock, args) {
var twitter = CmdUtils.getCommand('twitter');
var login = args.alias.data || Bin.twitterLastLogin() || {};
twitter._auth(login, function twitter_tweet(username, key, secret) {
twitterGet({
url: "http://api.twitter.com/1/statuses/mentions.json",
data: {count:10},
dataType: "json",
success: function twitter_success(data) {
pblock.innerHTML = renderTwitterStatuses(data);
},
error: function twitter_error(xhr) {
twitter._show(_("error - status not retrieved") + " / " +
xhr.status + " " + xhr.statusText,
username);
},
}, {token: key, tokenSecret: secret});
Bin.twitterLastLogin({username: username, password: "dummy"});
});
},
execute: function(arguments) {
Utils.openUrlInBrowser(this.url);
}
});
/**
* retweeted-of-mine
* @author makoto_kw
* @date 2010-09-27
*/
CmdUtils.CreateCommand({
names: ['retweeted-of-mine'],
icon: 'http://twitter.com/favicon.ico',
description: 'Display twitter reply messages',
author: author,
license: 'new BSD license',
url: 'http://twitter.com/#retweeted_of_mine',
arguments: [{role: "alias", label: _("user"), nountype: noun_type_twitter_user}],
preview: function(pblock, args) {
var twitter = CmdUtils.getCommand('twitter');
var login = args.alias.data || Bin.twitterLastLogin() || {};
twitter._auth(login, function twitter_tweet(username, key, secret) {
twitterGet({
url: "http://api.twitter.com/1/statuses/retweets_of_me.json",
data: {count:10,include_entities:true},
dataType: "json",
success: function twitter_success(data) {
pblock.innerHTML = renderTwitterStatuses(data);
},
error: function twitter_error(xhr) {
twitter._show(_("error - status not retrieved") + " / " +
xhr.status + " " + xhr.statusText,
username);
},
}, {token: key, tokenSecret: secret});
Bin.twitterLastLogin({username: username, password: "dummy"});
});
},
execute: function(arguments) {
Utils.openUrlInBrowser(this.url);
}
});
/**
* twitter-search
* @author makoto_kw
* @date 2010-09-27
*/
CmdUtils.CreateCommand({
names: ['twitter-search'],
icon: 'http://twitter.com/favicon.ico',
description: 'Display twitter reply messages',
author: author,
license: 'new BSD license',
url: 'http://twitter.com/',
arguments: [{role: 'object', nountype: noun_arb_text, label:'query'}],
preview: function(pblock, args) {
var q = args.object.text;
if (q && q != '') {
pblock.innerHTML = q + ' searching...';
$.ajax({
url:'http://search.twitter.com/search.json',
data:{q:q,show_user:true},
success: function(data){
pblock.innerHTML = renderTwitterStatuses(data.results);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
pblock.innerHTML = textStatus;
},
datatype:'json'
});
}
},
execute: function(arguments) {
Utils.openUrlInBrowser(this.url);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment