-
-
Save jmgarnier/208244 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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; | |
} | |
}; | |
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", "diigo"], | |
homepage: "blog.wangbin1979.com", | |
author: { name: "Wang Bin", email: "[email protected]" }, | |
version: "0.5", | |
license: "MPL,GPL", | |
description: "bookmark the page to your Diigo account", | |
icon: "http://www.diigo.com/favicon.ico", | |
arguments: [ | |
{role: 'object', nountype: noun_arb_text, label: 'notes'}, | |
{role: 'instrument', nountype: noun_arb_text, label: 'tags'}, | |
{role: 'alias', nountype: noun_type_share, label: 'share'} | |
], | |
preview: function(pblock, args) { | |
var doc = CmdUtils.getDocument(); | |
args.object.text = doc.getSelection() ? doc.getSelection().substring(0,140) : args.object.text; | |
args.instrument.text = split_tags(args.instrument.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>', | |
args.object.text ? '<dd>Notes:</dd><dt>${desc}</dt>':' ', | |
args.instrument.text ? '<dd>Tags:</dd><dt>${tags}</dt>':' ', | |
args.alias.text ? '<dd>Share:</dd><dt>${shared}</dt>': ' ', | |
'</dl>', | |
'</div>'].join("\n"); | |
var previewData = { | |
user: curLogin ? curLogin.username: '', | |
title: doc.title, | |
url: doc.URL, | |
tags: args.instrument.text, | |
desc: args.object.text, | |
shared: args.alias.text | |
}; | |
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData); | |
pblock.innerHTML = previewHTML; | |
}, | |
execute: function(args) { | |
var bookmark_url = "http://api2.diigo.com/bookmarks"; | |
var doc = CmdUtils.getDocument(); | |
args.instrument.text = split_tags(args.instrument.text); | |
var bookmark_params = { | |
title: doc.title, | |
url: doc.URL, | |
tags: args.instrument.text, | |
desc: args.object.text || "", | |
shared: args.alias.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); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment