MediaWiki:CharCount.js: Difference between revisions
Jump to navigation
Jump to search
Richardpruen (talk | contribs) (This should work, it's what the example says!) |
Richardpruen (talk | contribs) (Replace deleted by accident lines) |
||
Line 1: | Line 1: | ||
$(function () { | $(function () { | ||
var myPlace = document.getElementById('CharCount'); | var myPlace = document.getElementById('CharCount'); | ||
myPlace.innerHTML =$(function addNumericOptions(id, min, max, defIndex) { | myPlace.innerHTML =$(function getID(id) { | ||
return document.getElementById(id); | |||
} | |||
); | |||
$(function addNumericOptions(id, min, max, defIndex) { | |||
var s = getID(id); | var s = getID(id); | ||
for (ii=min; ii<=max; ii++) { | for (ii=min; ii<=max; ii++) { | ||
Line 10: | Line 14: | ||
} | } | ||
s.value = defIndex; | s.value = defIndex; | ||
}); | }); | ||
$(function getMaxChars() { | $(function getMaxChars() { | ||
var maxChar = getID('chars'); | |||
return maxChar.value; | |||
} | } | ||
); | ); | ||
$(function getLinkDiscount() { | $(function getLinkDiscount() { | ||
checkDiscount = getID('linkAbbreviate'); | |||
if (!checkDiscount.checked) return 0; | |||
input = getID('text'); | |||
sub = getID('linkChars'); | |||
let text = "" + input.value.length; | |||
text = text.replaceAll('\\n', ' '); | |||
text = text.replaceAll('\\t', ' '); | |||
var subLength = sub.value; | |||
var discount = 0; | |||
const parts = input.value.split(" "); | |||
for (ii=0; ii<parts.length; ii++) { | |||
part = parts[ii]; | |||
l1 = part.length; | l1 = part.length; | ||
discount+= l1-subLength; | l2 = part.replaceAll('http','').length; | ||
if (l1>l2) { | |||
l1 = part.length; | |||
discount+= l1-subLength; | |||
} | |||
} | } | ||
return discount; | |||
} | } | ||
); | |||
$(function updateParamValues() { | |||
); | var c = getParamValue('c'); | ||
$(function updateParamValues() { | lengthChoice = getID('chars'); | ||
lengthChoice.value = c; | |||
var l = getParamValue('l'); | |||
la = getID('linkAbbreviate'); | |||
if (l!=null) { | |||
linkChoice = getID('linkChars'); | |||
linkChoice.value = l; | |||
la.checked = true; | |||
} else { | |||
la.checked = false; | |||
} | |||
var text = getID('text'); | |||
text.rows = c/25; | |||
} | } | ||
); | |||
$(function getParamValue(name) { | |||
var url = window.location.href; | |||
l1 = url.length; | |||
$(function getParamValue(name) { | l2 = url.replaceAll('?','').length; | ||
if (l1==l2) { | |||
return null; | |||
} | |||
const params = url.split('?')[1]; // the params, hopefully | |||
const paramValues = params.split('&'); | |||
for (ii=0; ii<paramValues.length; ii++) { | |||
paramValue = paramValues[ii]; | |||
//alert(paramValue); | |||
paramName = paramValue.split('=')[0]; | |||
if (paramName==name) { | |||
return paramValue.split('=')[1]; | |||
} | |||
} | |||
return null; | return null; | ||
} | } | ||
); | |||
$(function updateOutput() { | |||
ld = getLinkDiscount(); | |||
cc = getCharacterCount(); | |||
ccd = cc-ld; | |||
max = getMaxChars(); | |||
if ( | rmn = max-ccd; | ||
var warningLevel = getID('warningLevel'); | |||
warningLevel.style.background = "rgb(0,255,0)"; | |||
if (rmn<10) { | |||
warningLevel.style.background = "rgb(255,255,0)"; | |||
} | |||
if (rmn<0) { | |||
warningLevel.style.background = "rgb(255,0,0)"; | |||
} | } | ||
var v = "This content uses " + ccd + " characters of " + | |||
getMaxChars() + " allowed. " + | |||
rmn + " characters to spare."; | |||
var o = getID('outputCount'); | |||
o.value = v; | |||
} | } | ||
); | |||
$(function getCharacterCount() { | |||
); | input = getID('text'); | ||
$(function | return input.value.length; | ||
} | } | ||
); | |||
$(function setup() { | |||
addNumericOptions('chars', 40, 1000, 280); | |||
addNumericOptions('linkChars', 0, 100, 13); | |||
updateParamValues(); | |||
updateOutput(); | |||
} | } | ||
); | |||
); | |||
}()); | }()); |
Revision as of 14:41, 8 December 2023
$(function () {
var myPlace = document.getElementById('CharCount');
myPlace.innerHTML =$(function getID(id) {
return document.getElementById(id);
}
);
$(function addNumericOptions(id, min, max, defIndex) {
var s = getID(id);
for (ii=min; ii<=max; ii++) {
var option = document.createElement("option");
option.text = ii;
option.value = ii;
s.add(option);
}
s.value = defIndex;
});
$(function getMaxChars() {
var maxChar = getID('chars');
return maxChar.value;
}
);
$(function getLinkDiscount() {
checkDiscount = getID('linkAbbreviate');
if (!checkDiscount.checked) return 0;
input = getID('text');
sub = getID('linkChars');
let text = "" + input.value.length;
text = text.replaceAll('\\n', ' ');
text = text.replaceAll('\\t', ' ');
var subLength = sub.value;
var discount = 0;
const parts = input.value.split(" ");
for (ii=0; ii<parts.length; ii++) {
part = parts[ii];
l1 = part.length;
l2 = part.replaceAll('http','').length;
if (l1>l2) {
l1 = part.length;
discount+= l1-subLength;
}
}
return discount;
}
);
$(function updateParamValues() {
var c = getParamValue('c');
lengthChoice = getID('chars');
lengthChoice.value = c;
var l = getParamValue('l');
la = getID('linkAbbreviate');
if (l!=null) {
linkChoice = getID('linkChars');
linkChoice.value = l;
la.checked = true;
} else {
la.checked = false;
}
var text = getID('text');
text.rows = c/25;
}
);
$(function getParamValue(name) {
var url = window.location.href;
l1 = url.length;
l2 = url.replaceAll('?','').length;
if (l1==l2) {
return null;
}
const params = url.split('?')[1]; // the params, hopefully
const paramValues = params.split('&');
for (ii=0; ii<paramValues.length; ii++) {
paramValue = paramValues[ii];
//alert(paramValue);
paramName = paramValue.split('=')[0];
if (paramName==name) {
return paramValue.split('=')[1];
}
}
return null;
}
);
$(function updateOutput() {
ld = getLinkDiscount();
cc = getCharacterCount();
ccd = cc-ld;
max = getMaxChars();
rmn = max-ccd;
var warningLevel = getID('warningLevel');
warningLevel.style.background = "rgb(0,255,0)";
if (rmn<10) {
warningLevel.style.background = "rgb(255,255,0)";
}
if (rmn<0) {
warningLevel.style.background = "rgb(255,0,0)";
}
var v = "This content uses " + ccd + " characters of " +
getMaxChars() + " allowed. " +
rmn + " characters to spare.";
var o = getID('outputCount');
o.value = v;
}
);
$(function getCharacterCount() {
input = getID('text');
return input.value.length;
}
);
$(function setup() {
addNumericOptions('chars', 40, 1000, 280);
addNumericOptions('linkChars', 0, 100, 13);
updateParamValues();
updateOutput();
}
);
}());