Created
November 16, 2008 12:28
-
-
Save dsample/25469 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
CmdUtils.CreateCommand({ | |
name: "magicious", | |
takes: {"tags": noun_arb_text}, | |
homepage: "http://www.sample.me.uk/ubiquity/", | |
author: { name: "Duncan Sample" }, | |
description: "Adds the current page to both Delicious and Magnolia", | |
help: "To run this you will first need to set the credentials by Ubiq'ing magicious-credentials. Then simply Ubiq magicious with the tags you want to save", | |
license: "MPL", | |
preview: function( pblock ) { | |
var doc = Application.activeWindow.activeTab.document; | |
pblock.innerHTML = CmdUtils.renderTemplate( "URL: ${url}<br />Description:${desc}", {url: doc.location, desc: doc.title} ); | |
}, | |
execute: function(tags) { | |
//CmdUtils.setSelection( this._date() ); | |
var doc = Application.activeWindow.activeTab.document; | |
var del = Application.prefs.getValue("magicious.delicious", "1"); | |
var mag = Application.prefs.getValue("magicious.magnolia", "1"); | |
if (del=="1") | |
{ | |
var api = ""; | |
var del_cred = dsample.retrieveLogins("magicious.delicious"); | |
if (del_cred.length>0) { | |
var del_username = del_cred[0].username; | |
var del_password = del_cred[0].password; | |
api = "https://" + del_username + ":" + del_password + "api.del.icio.us/v1/posts/add"; | |
} else { | |
api = "https://api.del.icio.us/v1/posts/add"; | |
} | |
var params = { url: doc.URL, description: doc.title, tags: tags.text }; | |
jQuery.get(api, params, | |
function(data, textStatus) { | |
result = jQuery(data).find("result"); | |
if (result.attr("code")=="done"){ | |
displayMessage("Posted to Delicious"); | |
}else{ | |
displayMessage("There was an error with Delicious"); | |
CmdUtils.log(data.text) | |
} | |
}, | |
"xml"); | |
} | |
if (mag=="1") | |
{ | |
var api = "https://ma.gnolia.com/api/rest/1/bookmarks_add"; | |
var mag_cred = dsample.retrieveLogins("magicious.magnolia"); | |
if (mag_cred.length>0) { | |
var api_key = mag_cred[0].password; | |
var params = { api_key: api_key, url: doc.URL, title: doc.title, tags: tags.text.split(' ').join(',') }; | |
jQuery.post(api, params, | |
function(data, textStatus) { | |
result = jQuery(data).find("response"); | |
if (result.attr("status")=="ok") | |
{ | |
displayMessage("Posted to Ma.gnolia"); | |
}else{ | |
var error = jQuery(data).find("error"); | |
displayMessage("Ma.gnolia error: " + error.attr("code") + " - " + error.attr("message")); | |
CmdUtils.log("Magnolia error:" + error.attr("code") + " - " + error.attr("message")) | |
} | |
}, | |
"xml"); | |
} | |
} | |
}, | |
}); | |
CmdUtils.CreateCommand({ | |
name: "magicious-set", | |
// takes: {"nothing": noun_arb_text}, | |
// takes: {"delicious": noun_arb_text, "magnolia": noun_arb_text}, | |
modifiers: { delicious: noun_arb_text, magnolia: noun_arb_text}, | |
homepage: "http://www.sample.me.uk/ubiquity/", | |
author: { name: "Duncan Sample" }, | |
description: "Sets which service(s) to post bookmarks to", | |
help: "By ubiq'ing magicious-set with on/off for both delcious and magnolia, you can set which of the services your bookmarks will be posted to.", | |
license: "MPL", | |
preview: function( pblock ) { | |
var del = Application.prefs.getValue("magicious.delicious", "1"); | |
var mag = Application.prefs.getValue("magicious.magnolia", "1"); | |
pblock.innerHTML = CmdUtils.renderTemplate( "<strong>Current settings</strong><br />Delicious: ${del}<br />Ma.gnolia: ${mag}", {del: (del==1)?"on":"off", mag: (mag==1)?"on":"off"}); | |
}, | |
execute: function(nothing, mods) { | |
if (mods.delicious.text != null) { | |
Application.prefs.setValue("magicious.delicious", (mods.delicious.text=="on")?"1":"0"); | |
} | |
if (mods.magnolia.text != null) { | |
Application.prefs.setValue("magicious.magnolia", (mods.magnolia.text=="on")?"1":"0"); | |
} | |
} | |
}); | |
CmdUtils.CreateCommand({ | |
name: "magicious-credentials", | |
takes: {"service": noun_arb_text/*, "username": noun_arb_text, "password": noun_arb_text*/}, | |
modifiers: { username: noun_arb_text, password: noun_arb_text}, | |
homepage: "http://www.sample.me.uk/ubiquity/", | |
author: { name: "Duncan Sample" }, | |
description: "Sets the login credentials for delicious and ma.gnolia", | |
help: "service should be either 'delicious' or 'magnolia'", | |
license: "MPL", | |
preview: function( pblock ) { | |
var del_cred = dsample.retrieveLogins("magicious.delicious"); | |
var mag_cred = dsample.retrieveLogins("magicious.magnolia"); | |
pblock.innerHTML = CmdUtils.renderTemplate( "<strong>Current user</strong><br />Delicious: ${del}<br />Ma.gnolia: ${mag}", {del: (del_cred.length==0)?"<em>none</em>":del_cred[0].username, mag: (mag_cred.length==0)?"<em>none</em>":mag_cred[0].username}); | |
}, | |
execute: function(service, mods) { | |
switch(service.text.toLowerCase()) | |
{ | |
case 'delicious': | |
dsample.savePassword({name: "magicious.delicious", username: mods.username.text, password: mods.password.text}); | |
break; | |
/* | |
case 'magnolia': | |
dsample.savePassword({name: "magicious.magnolia", username: mods.username.text, password: mods.password.text}); | |
break; | |
*/ | |
case 'magnolia': | |
var api = "https://ma.gnolia.com/api/rest/1/get_key"; | |
var params = { id: mods.username.text, password: mods.password.text }; | |
jQuery.get(api, params, | |
function(data, textStatus) { | |
result = jQuery(data).find("response"); | |
if (result.attr("status")=="ok") | |
{ | |
var mag_key = jQuery(data).find("key").text(); | |
dsample.savePassword({name:"magicious.magnolia", username: mods.username.text, password: mag_key}); | |
displayMessage("Ma.gnolia credentials saved"); | |
}else{ | |
var error = jQuery(data).find("error"); | |
displayMessage("Ma.gnolia error: " + error.attr("code") + " - " + error.attr("message")); | |
CmdUtils.log("Magnolia error:" + error.attr("code") + " - " + error.attr("message")) | |
} | |
}, | |
"xml"); | |
break; | |
} | |
} | |
}) | |
// --------------------------- | |
// FUNCTIONS FOR STORING AND RETRIEVING PASSWORDS AND OTHER SENSITIVE INFORMATION | |
// --------------------------- | |
var dsample = {}; | |
/** | |
* Saves a pair of username/password (or username/api key) to the password manager. You have to pass the name of your command | |
* (or other identifier) as to the command like: | |
* CmdUtils.savePassword( {name:'my command', username:'myUserName', password:'gu3ssm3'} ) | |
*/ | |
dsample.savePassword = function savePassword( opts ){ | |
var passwordManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager); | |
var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init"); | |
//var loginInfo = new nsLoginInfo(hostname, formSubmitURL, httprealm, username, password, usernameField, passwordField); | |
var loginInfo = new nsLoginInfo('chrome://ubiquity/content', null, opts.name, opts.username, opts.password, "", ""); | |
try { | |
passwordManager.addLogin(loginInfo); | |
} catch(e) { | |
// "This login already exists." | |
var logins = passwordManager.findLogins({}, "chrome://ubiquity/content", null, opts.name); | |
CmdUtils.log(logins.length); | |
for each(login in logins) { | |
if (login.username == opts.username) { | |
//modifyLogin(oldLoginInfo, newLoginInfo); | |
passwordManager.modifyLogin(login, loginInfo); | |
break; | |
} | |
} | |
} | |
} | |
/* | |
* Retrieve one or more username/password saved with CmdUtils.savePassword | |
* All you have to pass is the identifier (the name option) used on the other function. | |
* You will get as return an array of { username:'', password:'' } objects. | |
*/ | |
dsample.retrieveLogins = function retrieveLogins( name ){ | |
var passwordManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager); | |
var logins = passwordManager.findLogins({}, "chrome://ubiquity/content", null, name); | |
var returnedLogins = []; | |
for each(login in logins){ | |
loginObj = { | |
username: login.username, | |
password: login.password | |
} | |
returnedLogins.push(loginObj); | |
} | |
return returnedLogins; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment