Skip to content

Instantly share code, notes, and snippets.

@thorin87
Last active June 23, 2017 07:52
Show Gist options
  • Save thorin87/be677813f4b30110c74b8fb742608549 to your computer and use it in GitHub Desktop.
Save thorin87/be677813f4b30110c74b8fb742608549 to your computer and use it in GitHub Desktop.
Kontomierz - poprawa rozbijania transakcji - pozwala wpisać działanie matematyczne w oknie dzielenia wydatku
// ==UserScript==
// @name Kontomierz - poprawa rozbijania transakcji
// @namespace https://github.com/thorin87/
// @description Pozwala wpisać działanie matematyczne w oknie dzielenia wydatku
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @include https://kontomierz.pl/k4/analizuj*
// @version 1
// @grant none
// ==/UserScript==
var lastCorrectResult;
var originalValueJNode;
function parseInput(input) {
input = input.replace(/,/g, '.');
try {
return eval(input);
} catch (e) {
if (e instanceof SyntaxError) {
return null;
}
}
}
function formatOutput(output) {
if(output === undefined)
return 0;
else if(output % 1 === 0)
return output;
else
return output.toFixed(2).replace('.', ',');
}
function evalEquation(jNode) {
lastCorrectResult = 0;
originalValueJNode = $('#parent_money_transaction_currency_amount');
jNode.keyup(function() {
let newResult = parseInput(this.value);
if(newResult !== null) {
lastCorrectResult = newResult;
}
let newValue = originalValueJNode.attr('value').replace(',', '.') - lastCorrectResult;
if(!isNaN(newValue))
originalValueJNode.val(formatOutput(newValue));
});
jNode.blur(function() {
this.value = formatOutput(lastCorrectResult);
});
}
waitForKeyElements("#money-transaction-new-split #money_transaction_currency_amount", evalEquation);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment