-
-
Save siuying/87942 to your computer and use it in GitHub Desktop.
List your recent bookmarks on delicious.com
This file contains 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
var cookie_mgr = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager); | |
CmdUtils.CreateCommand({ | |
name: 'digest', | |
icon: 'http://delicious.com/favicon.ico', | |
description: 'List your recent bookmarks on delicious.com, optionally specify a tag', | |
help: 'digest <tags>', | |
homepage: 'http://www.reality.hk', | |
author: { | |
name: 'siuying', | |
email: 'siuying@reality.hk' | |
}, | |
license: 'MPL', | |
takes: { | |
count: noun_arb_text | |
}, | |
/** | |
* Command configuration settings. | |
*/ | |
_config: { | |
api_base: 'http://feeds.delicious.com', | |
cookie_domain: '.delicious.com', | |
cookie_name: '_user', | |
show_tags: false, | |
count: 50 | |
}, | |
/** | |
* Preview list of bookmarks. | |
*/ | |
preview: function(pblock, input_obj, mods) { | |
var user_cookie = this._getUserCookie(); | |
var user_name = (user_cookie) ? user_cookie.split(' ')[0] : ''; | |
var tags = jQuery.trim(input_obj.text); | |
pblock.innerHTML = "loading ..."; | |
CmdUtils.log("user name: ", user_name, " url: ", url, " count:", this._config.count); | |
if (!user_name) { | |
// If there's no user name, there's no login, so this command won't work. | |
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>' | |
].join(''); | |
pblock.innerHTML = CmdUtils.renderTemplate(tmpl, ns); | |
} else { | |
var url = this._getUrl(user_name, tags); | |
jQuery.getJSON(url, | |
{ | |
count: this._config.count | |
}, | |
function(data) { | |
pblock.innerHTML = DeliciousUtils.RenderJson(data, this.show_tags);; | |
}); | |
} | |
}, | |
/** | |
* Insert list of recents bookmarks into current rich text field, or show error message if not in rich text field | |
*/ | |
execute: function(input_obj, mods) { | |
var user_cookie = this._getUserCookie(); | |
var user_name = (user_cookie) ? user_cookie.split(' ')[0] : ''; | |
var tags = jQuery.trim(input_obj.text); | |
var url = this._getUrl(user_name, tags); | |
CmdUtils.log("GET ", url); | |
jQuery.getJSON(url, | |
{ | |
count: this._config.count | |
}, | |
function(data) { | |
var html = DeliciousUtils.RenderJson(data, this.show_tags); | |
// Insert content into HTML | |
var doc = context.focusedWindow.document; | |
if (doc.designMode == "on") { | |
doc.execCommand("insertHTML", false, html); | |
} else { | |
displayMessage("You're not in a rich text editing field."); | |
} | |
}); | |
}, | |
/** | |
* Dig up the Delicious login session cookie. | |
*/ | |
_getUserCookie: function() { | |
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); | |
} | |
} | |
}, | |
_getUrl: function(user_name, tags) { | |
var url = tags.length > 0 ? | |
(this._config.api_base + '/v2/json/' + user_name + '/' + tags): | |
(this._config.api_base + '/v2/json/' + user_name); | |
return url; | |
} | |
}); | |
DeliciousUtils = {}; | |
// Render HTML From delicious JSON API | |
DeliciousUtils.RenderJson = function(data, show_tags) { | |
CmdUtils.log("data: ", data); | |
var html = "<ul>"; | |
jQuery.each(data, | |
function(i, item) { | |
var title = item["d"]; | |
var link = item["u"]; | |
var content = item["n"]; | |
var tags = item["t"]; | |
CmdUtils.log("title: ", title, ", link: ", link, ", content: ", content); | |
// Render HTML | |
html += "<li><a href='" + encodeURI(link) + "'>" + Utils.escapeHtml(title) + "</a> "; | |
if (content != "") { | |
html += "<span> - " + Utils.escapeHtml(content) + "</span>"; | |
} | |
if (show_tags) { | |
var tagsText = []; | |
jQuery.each(tags, | |
function(n, tag) { | |
tagsText[n] = Utils.escapeHtml(tag); | |
}); | |
html += tagsText.join(", "); | |
} | |
html += "</li>"; | |
}); | |
html += "</ul>"; | |
return html; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment