Beeminding number of webpages visited with Firefox

My new GreaseMonkey script! Since you can only enter 150 data points a day, I changed the script so it increments the datapoint for today if there already is one.

// ==UserScript==
// @name     Beeminder
// @version  1
// @grant    GM.xmlHttpRequest
// ==/UserScript==

// get most recent datapoint and send to update
GM.xmlHttpRequest({
  method: "GET",
  url: "https://www.beeminder.com/api/v1/users/zedmango/goals/web/datapoints.json?auth_token=myauthtoken",
  onload: function(response) {
    update(JSON.parse(response.responseText)[0]);
  }
});

// if it matches today's date, increment count by 1, otherwise add a new datapoint
function update(latest) {
  var today = new Date();
  var daystamp = latest.daystamp;
  var id = latest.id;
  var newValue = parseInt(latest.value) + 1;
  
  if (  parseInt(daystamp.substr(0,4)) == today.getFullYear()
     && parseInt(daystamp.substr(4,2)) == 1 + today.getMonth()
     && parseInt(daystamp.substr(6,2)) == today.getDate()
     ) {    
    GM.xmlHttpRequest({
      method: "PUT",
      url: "https://www.beeminder.com/api/v1/users/zedmango/goals/web/datapoints/" + id + ".json",
      data: "auth_token=myauthtoken&value=" + newValue.toString(),
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      }
    });
  }
  else {
    GM.xmlHttpRequest({
      method: "POST",
      url: "https://www.beeminder.com/api/v1/users/zedmango/goals/web/datapoints.json",
      data: "auth_token=myauthtoken&value=1",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      }
    });
  }
}
1 Like