User:Aesulus/common.js

Revision as of 09:51, 17 March 2022 by Aesulus (talk | contribs) (add initialization logic; remove newline characters from list of punctuation)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
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();
}