User:Aesulus/common.js: Difference between revisions

add initialization logic; remove newline characters from list of punctuation
(Created page with "function addButton() { if(!(document.body.classList.contains('ns-subject') && document.body.classList.contains('action-view') && navigator.cookieEnabled)) { // only run pekofy script while viewing wiki subject pages, and only if cookies are enabled return; } // create the button const pekofyButton = document.createElement('svg'); pekofyButton.innerHTML ='<svg xmlns="http://www.w3.org/2000/svg" viewBox="-0.8 -0.5 235 235"><circle id="pekofyButtonCircle" cx="116.7...")
 
(add initialization logic; remove newline characters from list of punctuation)
Line 1:
function addButton() {
if(!(document.body.classList.contains('ns-subject') && document.body.classList.contains('action-view') && navigator.cookieEnabled)) { // only run pekofy script while viewing wiki subject pages, and only if cookies are enabled
return;
}
// create the button
const pekofyButton = document.createElement('svg');
Line 11 ⟶ 7:
document.body.appendChild(pekofyButton);
// initialize based on value of cookie
if (mw.cookie.get("pekofyButtonState") == "true") {
var pekofyButtonState = mw.cookie.get('pekofyButtonState');
if (mw.cookie.get("pekofyButtonState") == "true") {
pekofy(document.body);
}
else if (pekofyButtonState == "false") {
else {
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#000000');
}
else { // cookie does not exist or is malformed
mw.cookie.set('pekofyButtonState', true);
pekofy(document.body);
}
}
Line 23 ⟶ 26:
if (node.nodeValue.trim().length > 0) {
node.unpekofied = node.nodeValue; // store text node's original value in a new property
node.nodeValue = node.nodeValue.replaceAll(/([\.\?!\n]+)/ig, ' peko$1');
}
}
Line 49 ⟶ 52:
 
function changeImage() {
var toggleState = mw.cookie.get("'pekofyButtonState"') == "true";
if (toggleState) {
Line 60 ⟶ 63:
}
mw.cookie.set("'pekofyButtonState"', !toggleState);
}
 
// Begin here. Only run pekofy script while *viewing* (i.e. not editing) wiki subject pages and only if cookies are enabled
addButton();
if(!(document.body.classList.contains('ns-subject') && document.body.classList.contains('action-view') && navigator.cookieEnabled)) { // only run pekofy script while viewing wiki subject pages, and only if cookies are enabled
addButton();
}