Skip to content

Instantly share code, notes, and snippets.

@winder
Created July 1, 2015 22:30
Show Gist options
  • Save winder/b9869bf5aeb375497635 to your computer and use it in GitHub Desktop.
Save winder/b9869bf5aeb375497635 to your computer and use it in GitHub Desktop.
pebbleLogin
var URL_BASE="http://www.baby-connect.com";
var URL_AUTH="/Cmd?cmd=UserAuth";
var URL_MAIN="/home";
function logWithPrefix(prefix, msg) {
console.log('(' + prefix + ') ' + msg);
}
function requestHandler(response, num) {
try {
logWithPrefix(num, " onreadystatechange");
logWithPrefix(num, "Ready State = " + response.readyState);
logWithPrefix(num, "Status = " + response.status);
if (response.readyState == 4 && response.status == 200) {
logWithPrefix(num, 'good');
// This line causes a crash of some sort.
logWithPrefix(num, "response:\n" + response.responseText);
if (num === 1) {
var url = URL_BASE + URL_MAIN;
logWithPrefix(num, 'Sending GET request to: ' + url);
var req2 = new XMLHttpRequest();
req2.onreadystatechange = requestHandler(req2, 2);
req2.open("GET", url);
req2.send();
}
}else {
logWithPrefix(num, response.readyState + ' state and ' + response.status + ' status onreadystatechange event ' + response.responseText);
}
}
catch(err) {
logWithPrefix(num, "There was an error: " + err);
}
}
// Listen for when the watchface is opened
Pebble.addEventListener('ready',
function(e) {
console.log('PebbleKit JS ready!');
var authurl = URL_BASE + URL_AUTH;
console.log("Auth URL = " + authurl);
var params = "email=<my-email>&pass=<my-password>";
var cookieParams = "email=<my-email>;pass=<my-password>";
var req = new XMLHttpRequest();
// POST works.
// With content header
// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
req.open("POST", authurl, true);
req.onreadystatechange = function(){ requestHandler(req, 1); };
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
console.log('Sending POST request');
req.send(params);
/*
// POST works.
// Full content header
// http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest
req.open("POST", authurl, true);
req.onreadystatechange = function(){ requestHandler(req, 1); };
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", params.length);
req.setRequestHeader("Connection", "close");
console.log('Sending POST request');
req.send(params);
*/
/*
// Doesn't work, parameters not received by target.
// Cookie
req.open("POST", authurl, true);
req.onreadystatechange = function(){ requestHandler(req, 1); };
req.setRequestHeader("Cookie",cookieParams); // param separated with ';' instead of '&'
console.log('Sending POST request');
req.send();
*/
/*
// Doesn't work, parameters not received by target.
// open with credentials
req.open("POST", authurl, true, '<my-login>', '<my-password>');
req.onreadystatechange = function(){ requestHandler(req, 1); };
console.log('Sending POST request');
req.send();
*/
}
);
// Listen for when an AppMessage is received
Pebble.addEventListener('appmessage',
function(e) {
console.log('AppMessage received!');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment