Created
June 14, 2009 15:13
-
-
Save edgargoncalves/129718 to your computer and use it in GitHub Desktop.
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
// Buxfer Slidebar jetpack, by Edgar Gonçalves | |
jetpack.future.import("slideBar"); | |
function displayMessage(text) { | |
jetpack.notifications.show({title: 'Buxfer Jetpack', | |
body: text, | |
icon: 'https://www.buxfer.com/media/favicon/favicon-moneybag.ico'}); | |
} | |
function Buxfer () {} | |
Buxfer.prototype = { | |
//variables | |
buxferUrl: "www.buxfer.com", | |
buxferAPIUrl: "https://www.buxfer.com/api/", | |
//methods | |
// gets username and password for a given domain, from Firefox's login-manager. | |
getLoginInfo: function (domain, fuzzySearch) { | |
var loginManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager); | |
var domains = []; | |
if(fuzzySearch) { | |
domains.push("http://" + domain); | |
domains.push("http://www." + domain); | |
domains.push("https://" + domain); | |
domains.push("https://www." + domain); | |
} else { | |
domains = [domain]; | |
} | |
for(var i = 0; i < domains.length; i++) { | |
for(var j = 0; j < domains.length; j++) { | |
var logins = loginManager.findLogins({}, domains[i], domains[j], null); | |
if(logins.length > 0) | |
return {username: logins[0].username, | |
password: logins[0].password}; | |
} | |
} | |
return null; | |
}, | |
// Function called to log in the user in buxfer. | |
// Receives the function (with a token and an uid) to be performed after the login is done. | |
loginUser: function (successCallback) { | |
if ( !(successCallback)) { | |
successCallback = function(token, uid){}; | |
} | |
var username, password; | |
var loginInfo = this.getLoginInfo(this.buxferUrl, true); | |
if (loginInfo) { | |
username = loginInfo.username; | |
password = loginInfo.password; | |
} else { | |
// We have no login stored, send the user to the Buxfer and tell him/her to login | |
displayMessage("You have to login to Buxfer before using buxfer-send."); | |
jetpack.tabs.open(this.buxferUrl); | |
jetpack.tabs[ Jetpack.tabs.length-1 ].focus(); | |
return; | |
} | |
var onloadHandler = function(data, status) { | |
if(status !== "success") { | |
displayMessage("Error logging in: " + status); | |
return; | |
} | |
var jsonResponse = data.response; | |
if(jsonResponse.status === "OK"){ | |
successCallback(jsonResponse.token, jsonResponse.uid); | |
}else{ | |
displayMessage("Error logging in: "+jsonResponse.status); | |
} | |
}; | |
var loginurl = this.buxferAPIUrl + | |
"login.json?userid=" + | |
encodeURIComponent(username) + | |
"&password=" + encodeURIComponent(password); | |
jQuery.getJSON( loginurl, onloadHandler); | |
}, | |
// adds a buxfer transaction: | |
addTransaction: function (description, amount) { | |
var that = this; | |
this.loginUser( function(token, uid) { | |
var transactionUrl = that.buxferAPIUrl + "add_transaction.json"; | |
var onloadHandler = function(data, status) { | |
if(status !== "success"){ | |
displayMessage("Error logging adding it: " + status); | |
return; | |
} | |
var jsonResponse = data.response; | |
if (jsonResponse.status !== "OK") { | |
displayMessage("Error adding transaction : "+jsonResponse.status); | |
} | |
if (!jsonResponse.transactionAdded){ | |
displayMessage("Transaction wasn't added..."); | |
return; | |
} | |
if (jsonResponse.parseStatus != "success"){ | |
displayMessage("Transaction wasn't parsed correctly..."); | |
return; | |
} | |
displayMessage("Buxfer added your transaction."); | |
}; | |
var text = description + " " + amount; | |
var params = "token="+token+"&format=sms&text="+encodeURIComponent(text); | |
jQuery.ajax( { | |
type: "POST", | |
data: params, | |
dataType: "json", | |
url: transactionUrl, | |
cache: false, | |
success: onloadHandler, | |
error: function (XMLHttpRequest, textStatus, errorThrown) { | |
displayMessage("Something very wrong happened: " + textStatus + ", " + errorThrown);} | |
}); | |
}); | |
}, | |
postTransaction: function(doc) { | |
var description = $(doc).find("#description").attr('value') || "undefined"; | |
var amount = $(doc).find("#amount").attr('value') || "0"; | |
this.addTransaction(description, amount); | |
} | |
} | |
jetpack.slideBar.append({ | |
icon: "https://www.buxfer.com/media/favicon/favicon-moneybag.ico", | |
width: 350, | |
html: '<style>scrollbar * {display: none !important;}</style><h1>Buxfer Slidebar</h1><p>What did you spend money on?<br/></p><input id="description" size="45" type="text"/><br/><p>What much did you spend?<br/></p><input id="amount" "type="text"/><br/><br/><input type="button" id="post" value="Send to Buxfer!"/><br/><hl /> <p>Buxfer Slidebar is brought to you by:<br/><a href="http://sites.google.com/site/edgargoncalves/">Edgar Goncalves</a><br/>(no affiliation to <a href="http://buxfer.com">Buxfer</a>!)</p>', | |
onSelect: function(slide) { | |
slide({ size: 350 }); | |
var buxfer = new Buxfer(); | |
$(slide.contentDocument).find("#description").attr('value', jetpack.tabs.focused.contentDocument.getSelection()); | |
$(slide.contentDocument).find("#amount").focus(); | |
$(slide.contentDocument).find("#post").click(function () {buxfer.postTransaction(slide.contentDocument);}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment