git.fiddlerwoaroof.com
Raw Blame History
// 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);
  });
}

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.');});
}

function postURL(url, title, username, ak, callback, errorCallback) {
  var urlObj = JSON.stringify({"url": url, "title": title, "username": username, "ak": ak});
  var addUrl = 'http://marrow.elangley.org/api/bones/add';
  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;
}
function authenticateMarrow(cb) {
  chrome.storage.sync.get({
    username: '',
    password: ''
  }, function (items) {
    var username = items.username;
    var password = items.password;
    if (username === '') {
      renderStatus('Fail :(');
      return;
    }
    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) {
      if (response === false) {
        renderStatus('Fail :(');
      } else {
        console.log(response);
        cb(username, response);
      }
    });
  });
}

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);
        });
    });
  });});

});