Skip to content

Instantly share code, notes, and snippets.

@octatone
Created May 27, 2011 20:13
Show Gist options
  • Save octatone/996064 to your computer and use it in GitHub Desktop.
Save octatone/996064 to your computer and use it in GitHub Desktop.
reddit api fiddlin'
/*
reddit api javascript interface for phonegap
requires: jquery
requires: external hosts reddit.com, www.reddit.com in phonegap plist
*/
var reddit = new function() {
/*
* globals
*/
this.login_api_url = "http://www.reddit.com/api/login";
/*
* functions
*/
//login
this.login = function(username, password, callback) {
if(typeof username == "undefined" || typeof password == "undefined"){
username = window.localStorage.getItem("reddit_username");
password = window.localStorage.getItem("reddit_password");
}
//do login
$.ajax({
type: 'POST',
url: this.login_api_url,
data: ({user:username,passwd:password}),
success: function(response, status, object){
var header = object.getAllResponseHeaders();
var match = header.match(/(Set-Cookie|set-cookie): reddit_session=(.+?);/);
if(match){
reddit_session = match[2];
window.localStorage.setItem("reddit_session", reddit_session);
window.localStorage.setItem("reddit_username", username);
window.localStorage.setItem("reddit_password", password);
}
else{
reddit_session = null;
window.localStorage.setItem("reddit_session", null);
}
var reddit_session = window.localStorage.getItem("reddit_session");
var reddit_username = window.localStorage.getItem("reddit_username");
var reddit_password =window.localStorage.getItem("reddit_password");
console.log("login success ("+reddit_username+":"+reddit_password+")\nreddit_session: " + reddit_session);
callback();
},
error: function(jqXHR, textStatus, errorThrown){
console.log('error: '+textStatus+': '+errorThrown);
callback();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment