aboutsummaryrefslogtreecommitdiff
path: root/res.js
blob: 25c52a92e2c01ade8e095e079f1e8da54c56c32c (plain)
1
2
3
4
var itemEntries=nodeListToArray(document.querySelectorAll(".g-Recipe ul > li")).map(function(a){return{tag:"li",node:a}}),h1=document.querySelector(".g-Recipe h1");null!==h1&&itemEntries.push({tag:"h1",node:h1});var inputs=setInputs(itemEntries);inputs.map(function(a){a.node.oninput=function(c){if(null!==c.target&&(c=parseNumber(c.target.value),void 0!==c&&""===c.before&&""===c.after)){var b=parseFloat(c.number)/a.number;inputs.map(function(a){a.node.value=(a.number*b).toString()})}}});
function setInputs(a){for(var c=[],b=0;b<a.length;b++){var d=parseNumber(a[b].node.innerText);if(void 0!==d){var e=parsedNumberNode(a[b].tag,d),f=a[b].node.parentNode;f&&(f.replaceChild(e.all,a[b].node),c.push({number:parseFloat(d.number),node:e.number}))}}return c}
function parsedNumberNode(a,c){a=document.createElement(a);a.appendChild(document.createTextNode(c.before));var b=document.createElement("input");b.className="g-Number";b.value=c.number;a.appendChild(b);a.appendChild(document.createTextNode(c.after));return{all:a,number:b}}
function parseNumber(a){for(var c=0;c<a.length&&!isDigit(a.charAt(c));c++);if(c!==a.length){for(var b=c+1;b<a.length&&isDigit(a.charAt(b));b++);for(b<a.length&&("."===a.charAt(b)||","===a.charAt(b))&&b++;b<a.length&&isDigit(a.charAt(b));b++);return{before:a.substring(0,c),number:a.substring(c,b),after:a.substring(b,a.length)}}}function isDigit(a){return"0"<=a&&"9">=a}function nodeListToArray(a){var c=[];a.forEach(function(a){c.push(a)});return c};