Skip to content

Instantly share code, notes, and snippets.

@larubbio
Created February 8, 2009 00:28
Show Gist options
  • Save larubbio/60120 to your computer and use it in GitHub Desktop.
Save larubbio/60120 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
/*
Todo:
- Add paging using:
&start={#}
(optional) Start returning posts this many results into the set.
&results={#}
(optional) Return this many results.
*/
name: "delicious-list",
homepage: "",
author: { name: "Rob LaRubbio",
email: "rob@larubbio.org"},
contributors: ["Rob LaRubbio"],
license: "GPL",
description: "Retrieves a list of bookmarks for a tag.",
icon: "http://delicious.com/favicon.ico",
help: "Retrieve all bookmarks from your account matching 'tag'",
takes: { "tags": noun_arb_text },
timerId: undefined,
preview: function( pblock, tags ) {
var user_cookie = this.getUserCookie();
var user_name = (user_cookie) ? user_cookie.split(' ')[0] : '';
var tmpl = [
'<p style="color: #d44">',
' No active user found - log in at',
' <img src="http://delicious.com/favicon.ico" />',
' <b><a style="color: #3774D0" href="http://delicious.com">delicious.com</a></b>',
' to use this command.',
'</p>'
];
if (!user_name) {
pblock.innerHTML = CmdUtils.renderTemplate( tmpl, {} );
} else {
var url = "https://api.del.icio.us/v1/posts/all";
var params = { tag: tags.text,
_user: this.getUserCookie()
};
// Build a User-Agent string derived from the browser's, if none
// previously defined.
if (!this.user_agent) {
var mediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var win = mediator.getMostRecentWindow(null);
this.user_agent = win.navigator.userAgent + ";Ubiquity-share-on-delicious";
}
Utils.clearTimeout(this.timerId);
this.timerId = Utils.setTimeout(
function() {
CmdUtils.previewGet( pblock, url, params, function(data) {
var numToDisplay = 3;
var results = [];
try {
jQuery("post",data).each(
function() {
var i = results.length;
results[i] = {};
results[i].href = jQuery(this).attr('href');
results[i].description = jQuery(this).attr('description');
results[i].key = i+1;
});
} catch (e) { displayMessage("Exception: " + e.name + " " + e.message); }
var tmpl = [
'<style type="text/css">',
' div.delicious-list {',
' border-top:1px solid #CCCCCC;',
' margin-bottom: 5px;',
' }',
' div.dresult {',
' border-bottom:1px solid #CCCCCC;',
' margin: 0pt;',
' overflow:hidden;',
' padding: 3px;',
' color: #CCCCCC;',
' font-size: 12px;',
' font-family: Lucida Grande,Tahoma,Myriad,Arial;',
' }',
' div.access-key {',
' overflow:hidden;',
' width: 20px;',
' float: left;',
' }',
' div.link {',
' overflow:hidden;',
' text-align: left;',
' }',
' small {',
' color: #CCCCCC;',
' font-size: 10px;',
' }',
'</style>',
'<div class="delicious-list">',
' {for result in results}',
' <div class="dresult">',
' <div class="access-key">${result.key}</div> <div class="link"><a href="${result.href}" accesskey="${result.key}">${result.description}</a></div>',
' </div>',
' {forelse}',
' <b>Your search - ${searchTerm} - did not match any documents. </b>',
' {/for}',
'',
'</div>',
'<small>Tip: You can go to any result in this preview by using its number as an access key. (Linux and Windows: alt-number, Mac: control-number)</small>'
].join('');
try {
pblock.innerHTML = CmdUtils.renderTemplate(tmpl,
{results:results,
searchTerm:tags.text}
);
} catch (e) { displayMessage("Template Exception: " + e.name + " " + e.message); }
}, "xml")}, 2000);
}
},
/**
* Command configuration settings.
*/
config: {
// Domain and name of the delicious login session cookie.
cookie_domain: '.delicious.com',
cookie_name: '_user',
},
/**
* Dig up the Delicious login session cookie.
*/
getUserCookie: function() {
var cookie_mgr = Components.classes["@mozilla.org/cookiemanager;1"]
.getService(Components.interfaces.nsICookieManager);
var iter = cookie_mgr.enumerator;
while (iter.hasMoreElements()) {
var cookie = iter.getNext();
if( cookie instanceof Components.interfaces.nsICookie &&
cookie.host.indexOf(this.config.cookie_domain) != -1 &&
cookie.name == this.config.cookie_name) {
return decodeURIComponent(cookie.value);
}
}
},
/**
* Given an object, build a URL query string
*/
buildQueryString: function(data) {
var qs = [];
for (k in data) if (data[k])
qs.push( encodeURIComponent(k) + '=' +
encodeURIComponent(data[k]) );
return qs.join('&');
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment