git.fiddlerwoaroof.com
popup.js
f204a43a
 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
 function getCurrentTabUrl(callback) {
   var queryInfo = {active: true, currentWindow: true};
 
   chrome.tabs.query(queryInfo, function(tabs) {
     var tab = tabs[0];
     var url = tab.url;
     console.assert(typeof url == 'string', 'tab.url should be a string');
     callback(url, tab.title);
   });
 }
 
af0e348e
 function getSubscriptions(count, username, ak, callback, errorCallback) {
   console.log('getSubscriptions!');
   var subUrl = 'http://marrow.elangley.org/api/bones/subscriptions/count/' + count;
   var getData = {username: username, ak: ak};
   console.log(getData);
   $.getJSON(subUrl, getData).done(callback)
    .fail(function(){errorCallback('Network Error.');});
 }
 
f204a43a
 function postURL(url, title, username, ak, callback, errorCallback) {
   var urlObj = JSON.stringify({"url": url, "title": title, "username": username, "ak": ak});
af0e348e
   var addUrl = 'http://marrow.elangley.org/api/bones/add';
f204a43a
   var x = new XMLHttpRequest();
   x.open('POST', addUrl);
   x.setRequestHeader("Content-type","application/json");
   x.responseType = 'json';
   x.onload = function() {
     var response = x.response;
     callback(response);
   };
   x.onerror = function() {
     errorCallback('Network error.');
   };
   x.send(urlObj);
 }
 
 function renderStatus(statusText) {
   document.getElementById('status').textContent = statusText;
 }
af0e348e
 function authenticateMarrow(cb) {
f204a43a
   chrome.storage.sync.get({
     username: '',
     password: ''
   }, function (items) {
     var username = items.username;
     var password = items.password;
     if (username === '') {
       renderStatus('Fail :(');
       return;
     }
af0e348e
     var postData = {"username":username,"password":password};
     return $.ajax('http://marrow.elangley.org/api/user/login?ak=1', {
       data: JSON.stringify(postData),
       contentType: 'application/json',
       type: 'POST',
       dataType: 'json'
     }).done(function(response) {
f204a43a
       if (response === false) {
         renderStatus('Fail :(');
       } else {
af0e348e
         console.log(response);
         cb(username, response);
f204a43a
       }
     });
   });
af0e348e
 }
 
 document.addEventListener('DOMContentLoaded', function() {
   var button = document.getElementById('submitButton');
 
   authenticateMarrow(function(username, response) {
     getSubscriptions(5, username, response.ak,
       function(response) {
         console.log('gotten subs');
         console.log(response);
         $(response.marrow).each(function() {
           var newLink = $('<a href="'+this.url+'">'+this.title+'</a>');
           $('ul#recentLinks').append($('<li></li>').append(newLink));
         });
       }, function(errorMessage) {
         console.log('Can\'t load subscriptions!');
         console.log(errorMessage);
       });
   });
 
   button.addEventListener('click', function() {authenticateMarrow(function(username, response) {
     getCurrentTabUrl(function(url, title) {
       renderStatus('Submitting url ' + url + ' to Marrow');
       postURL(url, title, username, response.ak,
         function(response) {
           if (response.success === true) {
             renderStatus('Success :)');
           } else {
             console.log('fail!');
             console.log(response);
             renderStatus('Fail :(');
           }
         }, function(errorMessage) {
           renderStatus('Unexpected Error: ' + errorMessage);
         });
     });
   });});
 
f204a43a
 });