Soylent Stuff

spicing up the soylent

  1. // ==UserScript==
  2. // @name Soylent Stuff
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description spicing up the soylent
  6. // @author You
  7. // @match http*://diy.soylent.com/recipes/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // add function to format currency
  15. Number.prototype.formatMoney = function(c, d, t){
  16. c = isNaN(c = Math.abs(c)) ? 2 : c;
  17. d = d === undefined ? "." : d;
  18. t = t === undefined ? "," : t;
  19. var n = this,
  20. s = n < 0 ? "-" : "",
  21. i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
  22. j = (j = i.length) > 3 ? j % 3 : 0;
  23. return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
  24. };
  25.  
  26. // modify scaling selector
  27. $('.amount-selector').attr("id","select");
  28. for (var week = 5; week<=52;week++){
  29. $('#select').append($("<option></option>").attr("value",week * 7).text(week + " weeks"));
  30. }
  31.  
  32. // target total row
  33. $('table.ingredients > tbody:nth-child(3) > tr').attr("id","total-row");
  34.  
  35. // change daily cost label
  36. $('#total-row > td:nth-child(1) > div:nth-child(2)').text('Total Cost');
  37.  
  38. // add data-daily to total cost
  39. $('#total-row > td:nth-child(2)').attr("id","cost");
  40. $('#cost').attr("data-daily",$('#cost').text().replace("$", ""));
  41.  
  42. // add data-daily to table
  43. $('table.ingredients > tbody:first() > tr > td:nth-child(4)').each(function (index,element) {$(element).addClass("cost");$(element).attr("data-daily",$(element).text().replace("$",""));});
  44.  
  45. $('#select').on("change",function () {
  46. $('[data-daily]').each(function(index){$(this).text("$" + Number.parseFloat($(this).attr("data-daily") * $('#select').val()).formatMoney("."));});
  47. });
  48.  
  49. var amountNodes = $("[data-amount]");
  50. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  51. var amountObserver = new MutationObserver (amountHandler);
  52. var amountConfig = { childList: true, characterData: true, attributes: true, subtree: true };
  53.  
  54. //--- Add a target node to the observer. Can only add one node at a time.
  55. amountNodes.each ( function () {
  56. //console.log($(this));
  57. amountObserver.observe (this, amountConfig);
  58. } );
  59. var factor = 1;
  60. function amountHandler (mutationRecords) {
  61. //console.info ("amountHandler:");
  62.  
  63. mutationRecords.forEach ( function (mutation) {
  64. // debugger;
  65. // console.log (mutation.type);
  66. //debugger
  67. var amount = mutation.addedNodes[0].parentElement;
  68. var cost = $(amount.parentElement).find("td.cost");
  69. var newFactor = $(amount).text()/$(amount).attr("data-amount");
  70. if (factor==1) {factor = newFactor;}
  71. console.log(factor);
  72. var newCost = $(cost).attr("data-daily")*factor;
  73. cost.text("$"+newCost.formatMoney("."));
  74.  
  75. if (typeof mutation.removedNodes == "object") {
  76. var jq = $(mutation.removedNodes);
  77. //console.log (jq);
  78. //console.log (jq.is("span.myclass2"));
  79. //console.log (jq.find("span") );
  80. }
  81. } );
  82. factor = 1;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. })();