User:V111P/js/SumNumbers.js
Appearance
< User:V111P | js
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:V111P/js/SumNumbers. |
// See https://en.wikipedia.org/wiki/User:V111P/js/Sum_numbers
function sumSelectedNumbers() {
var lang = mw.config.get('wgContentLanguage');
var decimalMark = window.decimalMark || (lang == 'en' ? '.' : ',');
var reNonNumNonDecimalMark = new RegExp('[^0-9' + decimalMark + ']', 'g');
var reNonCommon = new RegExp('[^0-9\s,' + decimalMark + ']');
function sumFields(str) { // \n or \t divided fields
var fields = str.split(/[\n\t]/);
var textVal, numVal, expr = '', ignored = '', total = 0;
for (var i = 0; i < fields.length; i++) {
textVal = fields[i];
if (textVal === '') continue;
numVal = parseFloat(textVal.replace(reNonNumNonDecimalMark, '').replace(decimalMark, '.'));
if (isNaN(numVal) || reNonCommon.test(textVal)) {
ignored += textVal + ' | ';
continue;
}
expr += (numVal + ' + ').replace('.', decimalMark);
total += numVal;
}
return {total: total, expr: expr, ignored: ignored};
}
var total = 0, expr = '', ignored = '', r;
if (window.getSelection) {
var sel = window.getSelection();
for (var i = 0; i < sel.rangeCount; i++) {
r = sumFields(sel.getRangeAt(i).toString());
total += r.total;
expr += r.expr;
ignored += r.ignored;
}
}
var msg = '';
total = ('' + total).replace('.', decimalMark);
if (expr !== '' || ignored !== '')
msg = expr.slice(0, -3) + ' = ' + total + (ignored ? '<br/>IGNORED: ' + ignored.slice(0, -3) : '');
return msg;
}
function sumNumbersShowButtons() {
var exist = $('.sumNumbersDiv');
if (exist.length > 0)
exist.remove();
else {
var div = $('<div class="sumNumbersDiv"><input class="sumNumbersButton" type="button" value="+"><div class="sumNumbers_msgDiv"></div></div>');
$('h1,h2').after(div);
$('table').after(div).before(div);
$($('.sumNumbersButton')[0]).after(' (<a target="_blank" href="//en.wikipedia.org/wiki/User:V111P/js/Sum_numbers">?</a>)');
$('input.sumNumbersButton').click(function (e) {
var msg = sumSelectedNumbers();
if (console && console.log) console.log(msg);
var t = $(e.target);
$(e.target).parent().children('.sumNumbers_msgDiv').html(msg);
});
}
}
$(function () {
mw.util.addPortletLink('p-tb', 'javascript:sumNumbersShowButtons()',
'Sum numbers', 'sumNumbersToolbarLink',
'Select some numbers on the page (in Firefox you can hold Ctrl to select multiple text ranges '
+ 'and you can drag across table cells to select whole columns) '
+ 'and press one of the + buttons to see the sum');
});