-
-
Save wangbin/70996 to your computer and use it in GitHub Desktop.
Ubiquity command for Diigo
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
/* Mozilla Ubiquity Command for Diigo, currently only support add bookmark. | |
* Some code from https://ubiquity.mozilla.com/standard-feeds/social.js | |
*/ | |
var store = Application.storage; | |
const DIIGO_CUR_LOGIN = "ubiquity_diigo_cur_login"; | |
var Choices = {"yes": "yes", "no": "no"}; | |
var sortChoices = { "created": 0, "updated": 1, "popularity": 2, "hot": 3 }; | |
var noun_type_share = { | |
_name: "yes/no", | |
suggest: function(text, html) { | |
var suggestions = []; | |
for ( var word in Choices ) { | |
if ( word.indexOf(text.toLowerCase()) > -1 ) { | |
var sugg = CmdUtils.makeSugg(word, word, Choices[word]); | |
suggestions.push(sugg); | |
} | |
} | |
return suggestions; | |
} | |
}; | |
var noun_type_sort = { | |
_name: "order", | |
suggest: function(text, html) { | |
var suggestions = []; | |
for ( var word in sortChoices ) { | |
if ( word.indexOf(text.toLowerCase()) > -1 ) { | |
var sugg = CmdUtils.makeSugg(word, word, sortChoices[word]); | |
suggestions.push(sugg); | |
} | |
} | |
return suggestions; | |
} | |
}; | |
function setCurLogin(login) { | |
store.set(DIIGO_CUR_LOGIN, login); | |
} | |
function getCurLogin() { | |
var login = store.get(DIIGO_CUR_LOGIN, ''); | |
if(!login){ | |
var logins = getDiigoIds(); | |
if(logins.length > 0){ | |
login = logins[0]; | |
setCurLogin(login); | |
} | |
} | |
return login; | |
} | |
function make_basic_auth(user, password){ | |
var tok = user + ":" + password; | |
var hash = CmdUtils.getWindow().btoa(tok); //Base64 encode. | |
return "Basic " + hash; | |
} | |
function getDiigoIds () { | |
var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); | |
var logins = passwordManager.findLogins({}, "https://secure.diigo.com", "", null); | |
return logins; | |
} | |
function split_tags(tags) { | |
if (tags.search(/,/) < 0) { | |
return tags.replace(/ /g, ", "); | |
} | |
return tags; | |
} | |
CmdUtils.CreateCommand({ | |
name: "Diigo", | |
homepage: "blog.wangbin1979.com", | |
author: { name: "Wang Bin", email: "wangbin.zibo@gmail.com" }, | |
version: "0.2", | |
license: "MPL,GPL", | |
description: "bookmark the page to your Diigo account", | |
icon: "http://www.diigo.com/favicon.ico", | |
takes: { "notes": noun_arb_text }, | |
modifiers: { "tags": noun_arb_text, "share": noun_type_share }, | |
preview: function(pblock, notes, mods) { | |
var doc = CmdUtils.getDocument(); | |
notes.text = doc.getSelection() ? doc.getSelection().substring(0,140) : notes.text; | |
mods.tags.text = split_tags(mods.tags.text); | |
var curLogin = getCurLogin(); | |
var previewTemplate = [ | |
'<style type="text/css">', | |
'.preview a { color: #3774D0 }', | |
'.preview dl {margin: 0; padding: 0; }', | |
'.preview dd {float: left; text-align: right; width: 50px; margin: 0; padding: 0; }', | |
'.preview dt {margin-left: 60px;}', | |
'</style>', | |
'<div class="preview">', | |
'<p>Bookmark to <img src="http://www.diigo.com/favicon.ico" /> <a href="http://www.diigo.com/user/${user}">Diigo</a>: </p>', | |
'<dl>', | |
'<dd>Title:</dd><dt>${title}</dt>', | |
'<dd>Url:</dd><dt>${url}</dt>', | |
notes.text ? '<dd>Notes:</dd><dt>${desc}</dt>':' ', | |
mods.tags.text ? '<dd>Tags:</dd><dt>${tags}</dt>':' ', | |
mods.share.text ? '<dd>Share:</dd><dt>${shared}</dt>': ' ', | |
'</dl>', | |
'</div>'].join("\n"); | |
var previewData = { | |
user: curLogin ? curLogin.username: '', | |
title: doc.title, | |
url: doc.URL, | |
tags: mods.tags.text, | |
desc: notes.text, | |
shared: mods.share.text | |
}; | |
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData); | |
pblock.innerHTML = previewHTML; | |
}, | |
execute: function(notes, mods) { | |
var bookmark_url = "http://api2.diigo.com/bookmarks"; | |
var doc = CmdUtils.getDocument(); | |
mods.tags.text = split_tags(mods.tags.text); | |
var bookmark_params = { | |
title: doc.title, | |
url: doc.URL, | |
tags: '"' + mods.tags.text + '"', | |
desc: notes.text || "", | |
shared: mods.share.text | |
}; | |
var curLogin = getCurLogin(); | |
if( curLogin ){ | |
var auth = make_basic_auth(curLogin.username, curLogin.password); | |
} else { | |
var auth = null; | |
} | |
jQuery.ajax({ | |
type: "POST", | |
url: bookmark_url, | |
data: bookmark_params, | |
dataType: "json", | |
beforeSend: function(req) { | |
if( auth ) req.setRequestHeader("Authorization", auth); | |
}, | |
error: function() { | |
displayMessage("Diigo error - bookmark failure."); | |
}, | |
success: function(resp) { | |
displayMessage(resp.message); | |
} | |
}); | |
} | |
}); | |
CmdUtils.CreateCommand({ | |
name: "Diigo-search", | |
homepage: "blog.wangbin1979.com", | |
author: { name: "Wang Bin", email: "wangbin.zibo@gmail.com" }, | |
version: "0.1", | |
license: "MPL,GPL", | |
description: "Search on Diigo", | |
icon: "http://www.diigo.com/favicon.ico", | |
modifiers: { "users": noun_arb_text, "tags": noun_arb_text, "list": noun_arb_text, "sort": noun_type_sort }, | |
preview: function(pblock, tags, mods) { | |
var previewTemplate = [ | |
'<style type="text/css">', | |
'.preview { overflow: auto; padding-right: 4px; }', | |
'.preview a { color: #3774D0 }', | |
'.preview ul { margin: 0; padding: 0; }', | |
'.preview li { list-style: none; border-top: 1px dotted #ccc; margin-bottom: 2px; }', | |
'.preview p { margin: 0 2px 0 0; }', | |
'</style>', | |
'<div class="preview">', | |
'<p>Search bookmarks on <img src="http://www.diigo.com/favicon.ico" /> <a href="${url}">Diigo:</a></p>', | |
'<ul>', | |
'{for item in data }', | |
'<li>', | |
'<p><a href="${item.url}">${item.title}</a></p>', | |
'{if item.desc }<p>Notes: ${item.desc}</p>{/if}', | |
'{if item.tags != "no_tag" }<p>Tags: ${item.tags}</p>{/if}', | |
'</li>', | |
'{/for}', | |
'</ul>', | |
'</div>'].join("\n"); | |
var search_url = "http://api2.diigo.com/bookmarks"; | |
var url = ''; | |
var params = { rows: 5 }; | |
params["sort"] = mods.sort.data || 0; | |
if (mods.users.text) { | |
params['users'] = mods.users.text; | |
if (mods.tags.text) { | |
params['tags'] = split_tags(mods.tags.text); | |
url = 'http://www.diigo.com/user/' + mods.users.text + '/' + mods.tags.text; | |
} else if (mods.list.text) { | |
params['list'] = mods.list.text; | |
url = 'http://www.diigo.com/list/' + mods.users.text + '/' + mods.list.text; | |
} else { | |
url = 'http://www.diigo.com/user/' + mods.users.text; | |
} | |
} else { | |
if (mods.tags.text) { | |
params['tags'] = split_tags(mods.tags.text); | |
url = 'http://www.diigo.com/tag/' + mods.tags.text; | |
} else { | |
url = 'http://www.diigo.com/buzz/justin/'; | |
} | |
} | |
jQuery.getJSON(search_url, params, function(data){ | |
var previewHTML = CmdUtils.renderTemplate(previewTemplate, {data: data, url: url}); | |
pblock.innerHTML = previewHTML; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment