DIY Soylent Scale to Ingredient

Allows you to scale all ingredients to a set amount of a specific ingredient.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          DIY Soylent Scale to Ingredient 
// @version       0.0.1
// @namespace     http://userscripts.psbarrett.com
// @description	  Allows you to scale all ingredients to a set amount of a specific ingredient.
// @include       https://diy.soylent.com/recipes/*
// @grant         none
// ==/UserScript==
'use strict'

var amount_box_list = document.querySelectorAll('td.amount');
var unit_box_list = document.querySelectorAll('td.unit');
var name_box_list = document.querySelectorAll('td.name');

function click_handler_builder(amountb, unitsb, nameb) {
  return function() {
    var amount = amountb.getAttribute("data-amount");
    var units = unitsb.textContent;
    var name = nameb.textContent;
    var new_amount = window.prompt(
      "How many <" + units + "> of <" + name + "> do you have?",
      amount)

    var scale_factor = Number(new_amount)/Number(amount);

    for (var box of amount_box_list) {
      var scaled_amount = Number(box.getAttribute("data-amount"))*scale_factor;
      box.textContent = Math.round(scaled_amount*100)/100;
    }
  }
}

for (var i in amount_box_list) {
  var amountb = amount_box_list[i];
  var unitb = unit_box_list[i];
  var nameb = name_box_list[i];

  amountb.onclick = click_handler_builder(amountb, unitb, nameb);
}
  
console.log("Ingredient Scaling Enabled - Click to Scale");