User:Aesulus/common.js: Difference between revisions
Content added Content deleted
(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: | Line 1: | ||
function addButton() { |
function addButton() { |
||
⚫ | |||
return; |
|||
⚫ | |||
⚫ | |||
// create the button |
// create the button |
||
const pekofyButton = document.createElement('svg'); |
const pekofyButton = document.createElement('svg'); |
||
Line 11: | Line 7: | ||
document.body.appendChild(pekofyButton); |
document.body.appendChild(pekofyButton); |
||
// initialize based on value of cookie |
|||
⚫ | |||
var pekofyButtonState = mw.cookie.get('pekofyButtonState'); |
|||
⚫ | |||
⚫ | |||
pekofy(document.body); |
pekofy(document.body); |
||
} |
} |
||
else if (pekofyButtonState == "false") { |
|||
else { |
|||
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#000000'); |
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#000000'); |
||
⚫ | |||
else { // cookie does not exist or is malformed |
|||
mw.cookie.set('pekofyButtonState', true); |
|||
pekofy(document.body); |
|||
} |
} |
||
} |
} |
||
Line 23: | Line 26: | ||
if (node.nodeValue.trim().length > 0) { |
if (node.nodeValue.trim().length > 0) { |
||
node.unpekofied = node.nodeValue; // store text node's original value in a new property |
node.unpekofied = node.nodeValue; // store text node's original value in a new property |
||
node.nodeValue = node.nodeValue.replaceAll(/([\.\?! |
node.nodeValue = node.nodeValue.replaceAll(/([\.\?!]+)/ig, ' peko$1'); |
||
} |
} |
||
} |
} |
||
Line 49: | Line 52: | ||
function changeImage() { |
function changeImage() { |
||
var toggleState = mw.cookie.get( |
var toggleState = mw.cookie.get('pekofyButtonState') == "true"; |
||
if (toggleState) { |
if (toggleState) { |
||
Line 60: | Line 63: | ||
} |
} |
||
mw.cookie.set( |
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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
Revision as of 09:51, 17 March 2022
function addButton() {
// 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" cy="117" r="117.5" fill="#7dc4fc"/><g fill="none" stroke="#fff" stroke-linecap="round"><path stroke-width="26.296" d="M116.8 42.409v75.292"/><path stroke-width="30" d="M62.7 69.001c-11.6 12.9-18.7 30-18.7 48.7 0 40.1 32.5 72.7 72.7 72.7 40.1 0 72.7-32.5 72.7-72.7 0-18.7-7.1-35.8-18.7-48.7"/></g></svg>';
pekofyButton.setAttribute('style', 'width: 72px; z-index: 100; position: fixed; bottom: 0px; right: 0px; margin: 10px;');
pekofyButton.setAttribute('onclick', 'changeImage()');
document.body.appendChild(pekofyButton);
// initialize based on value of cookie
var pekofyButtonState = mw.cookie.get('pekofyButtonState');
if (pekofyButtonState == "true") {
pekofy(document.body);
}
else if (pekofyButtonState == "false") {
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#000000');
}
else { // cookie does not exist or is malformed
mw.cookie.set('pekofyButtonState', true);
pekofy(document.body);
}
}
function pekofy(node) {
if (node.nodeType === Node.TEXT_NODE) {
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(/([\.\?!]+)/ig, ' peko$1');
}
}
else if (node.nodeType === Node.ELEMENT_NODE) {
var children = node.childNodes;
for (var i = children.length; i--;) {
pekofy(children[i]);
}
}
}
function unpekofy(node) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.hasOwnProperty('unpekofied')) {
node.nodeValue = node.unpekofied;
}
}
else if(node.nodeType === Node.ELEMENT_NODE) {
var children = node.childNodes;
for (var i = children.length; i--;) {
unpekofy(children[i]);
}
}
}
function changeImage() {
var toggleState = mw.cookie.get('pekofyButtonState') == "true";
if (toggleState) {
unpekofy(document.body);
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#000000');
}
else {
pekofy(document.body);
document.getElementById('pekofyButtonCircle').setAttribute('fill', '#7dc4fc');
}
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
if(document.body.classList.contains('ns-subject') && document.body.classList.contains('action-view') && navigator.cookieEnabled) {
addButton();
}