User:Nardog/sandbox.js

// jshint maxerr:999,strict:implied,shadow:outer,undef:true,unused:true /* globals $,mw */

/*mw.hook('wikipage.content').add(function catChangeLinker($content) {	if (!($content.hasClass('mw-changeslist') || $content.find('.mw-changeslist').length)) return;	mw.loader.using('mediawiki.util', => { if ($content[0].querySelector('.mw-enhanced-rc')) { $content[0].querySelectorAll('.mw-changeslist-src-mw-categorize td.mw-enhanced-rc-nested').forEach(td => {				if (td.querySelector('.mw-changeslist-diff')) return;				let title = (td.querySelector('.comment > a') || {}).textContent;				let timeLink = td.querySelector('.mw-enhanced-rc-time > a');				if (!title || !timeLink) return;				let newUrl = timeLink.getAttribute('href').replace(/title=[^&]+/, 'title=' + mw.util.wikiUrlencode(title)).replace(/&curid=[^&]+/, '');				timeLink.href = newUrl;				timeLink.title = title;				$(document.createTextNode(' (')).add('', {					class: 'mw-changeslist-diff-cur',					href: newUrl.replace('&oldid=', '&diff=0&oldid='),					text: 'cur'				}).add(document.createTextNode(' | ')).add('', {					class: 'mw-changeslist-diff',					href: newUrl.replace('&oldid=', '&diff='),					title: title,					text: 'prev'				}).add(document.createTextNode(')')).insertAfter(timeLink.parentElement); $(' ').addClass('mw-changeslist-links').append(					$('', { class: 'mw-redirect mw-changeslist-history', href: newUrl.replace(/&oldid=[^&]+/, '&action=history'), title: title, text: 'hist' }).wrap(' ').parent				).appendTo(td).before(' '); });			return;		}		let cats = {};		$content[0].querySelectorAll('.mw-changeslist-src-mw-categorize').forEach(li => { let cat = (li.querySelector('.mw-title') || {}).textContent; let title = (li.querySelector('.comment > a') || {}).textContent; let date = li.dataset.mwTs; let diff = li.querySelector('.mw-changeslist-links > span'); if (!cat || !title || !date || !diff || diff.querySelector('a')) return; let hist = diff.nextElementSibling; let histLink = hist.querySelector('a'); if (!histLink) { histLink = $(hist).wrapInner($('', { class: 'mw-changeslist-history', href: mw.util.getUrl(title, { action: 'history' }), title: title })).children[0]; }			if (!cats[cat]) cats[cat] = { start: date }; cats[cat].end = date; cats[cat][date + title] = { diff: diff, histLink: histLink }; });		mw.loader.using('mediawiki.api', => { let api = new mw.Api; Object.keys(cats).slice(0, window.catchangelinkerCatLimit || 15).forEach(cat => {				api.get({ action: 'query', list: 'recentchanges', rctype: 'categorize', rctitle: cat, rcstart: cats[cat].start, rcend: cats[cat].end, rcprop: 'ids|timestamp|comment', rclimit: window.catchangelinkerRevLimit || 50, formatversion: 2 }).done(response => { (((response || {}).query || {}).recentchanges || []).forEach(rc => {						let title = rc.comment.match(/\[\[:?([^\]]+)\]\]/);						if (!title) return;						title = title[1];						let edit = cats[cat][rc.timestamp.replace(/\D/g, '') + title];						if (!edit) return;						$(edit.diff).wrapInner($('', {							class: 'mw-changeslist-diff',							href: mw.util.getUrl(title, { curid: rc.pageid, diff: rc.revid, oldid: rc.old_revid }),							title: title						}));						edit.histLink.href = mw.util.getUrl(title, { curid: rc.pageid, action: 'history' });					}); });			});		});	}); });*/

mw.config.get('wgAction') === 'view' && $( => {	let ns = mw.config.get('wgNamespaceNumber');	if (ns < 0 || !(ns & 1 || ns === 4)) return;	let parserOutput = document.querySelector('.mw-parser-output');	let dates = parserOutput && parserOutput.textContent.match(/\d\d:\d\d, \d\d? [A-Z][a-z]+ \d\d\d\d \(UTC\)$/gm);	if (!dates || dates.length < 2) return;	let today = Math.floor(Date.now / 86400000);	dates = dates.map(d => Math.floor(Date.parse(d.replace(/[,\(\)]/g, '')) / 86400000)).filter(d => d <= today);	let last = Math.max(...dates);	let span = last - Math.min(...dates) + 1;	let count = dates.length;	let ratio = (count / span).toFixed(1).toLocaleString;	count = count.toLocaleString;	span = span.toLocaleString;	last = (today - last).toLocaleString;	$('#siteSub').append(`; ${count} comments in ${span} days (${last} days ago, ${ratio} per)`).show; });

if (mw.config.get('wgNamespaceNumber') === 6 &&	mw.config.get('wgAction') === 'view' && !mw.config.get('wgRevisionId') ) { let commonsLink = document.querySelector('#ca-view-foreign > a'); if (commonsLink) location.href = commonsLink.href; }

/*mw.config.get('wgAction') === 'view' && $( => {	let prevPopup;	mw.loader.using('oojs-ui').done( => { $('.IPA.nopopups.noexcerpt').addClass('IPAc-en'); let $parserOutput = $('.mw-parser-output'); $('.IPAc-en > a').each(function {			let $link = $(this);			// let $spans = $link.find('> .IPAc-en-tooltip > span');			let $spans = $link.find('span[title]');			if ($spans.length) {				let $table = $('  ').addClass('wikitable IPAc-en-popup-table');				$spans.each(function  { let $span = $(this); $table.append($(' ').append( $(' ').addClass('IPA').text($span.text), $(' ').text($span.attr('title').match(/^(\/[^\/]+\/: )?(.+)$/)[2]) ));					$span.removeAttr('title'); });				$link.removeAttr('title');				let popup = new OO.ui.PopupWidget({ $autoCloseIgnore: $link, // $container: $parserOutput, // $content: $table, $content: $($table[0]), $floatableContainer: $link, align: 'forwards', autoClose: true, classes: [ 'IPAc-en-popup' ], // containerPadding: 0, position: 'above', width: 'auto' });				popup.on('ready', => { if (prevPopup && prevPopup !== popup) prevPopup.toggle(false); prevPopup = popup; });				$parserOutput.append(popup.$element);				// $parserOutput.append($table);				$link.hover( => popup.toggle(true), => popup.toggle(false));				$link.on('touchstart',  => popup.toggle(true));			}		}); }); });*/

/*document.querySelector('.mw-changeslist') && mw.hook('wikipage.content').add( => {	let wlinfo = document.querySelector('.wlinfo');	if (wlinfo) {		let toHours = date => Date.parse(date.replace(/(....)(..)(..)(..)(..)(..)/, '$1-$2-$3T$4:$5:$6Z')) / 3600000;		wlinfo.querySelector('strong:last-child').textContent = Math.round( toHours(JSON.parse(wlinfo.dataset.params).from) - toHours([...document.querySelectorAll('[data-mw-ts]')].pop.dataset.mwTs) );	} });*/

// let monthRes; // let parseDate = str => { // 	let matches = str.match(/^(.*?)(\d+):(\d+)(.*?)$/); // 	if (!matches) return; // 	let h = matches[2]; // 	let m = matches[3]; // 	str = matches[1] + ' ' + matches[4]; // 	if (!monthRes) { // 		monthRes = mw.config.get('wgMonthNames').slice(1).reverse // 			.map(mn => new RegExp(`^(.*?)${mn}(.*?)$`)); // 	}	// 	let mo = monthRes.findIndex(re => {	// 		matches = str.match(re);	// 		if (!matches) return;	// 		str = matches[1] + ' ' + matches [2];	// 		return true;	// 	}); // 	if (mo === -1) return; // 	mo = 11 - mo; // 	matches = str.match(/^(.*?)(\d{4})(.*?)$/); // 	if (!matches) return; // 	let y = matches[2]; // 	str = matches[1] + ' ' + matches[3]; // 	matches = str.match(/\d+/); // 	if (!matches) return; // 	let d = matches[0]; // 	return new Date(y, mo, d, h, m).getTime; // };		// let date; // if (isHist || isContribs) { // 	date = li.querySelector('.mw-changeslist-date').textContent; // 	date = Date.parse(date) || Date.parse(date.replace(',', '')) || // 		parseDate(date); // } else { // 	date = Date.parse(li.dataset.mwTs.replace(/(....)(..)(..)(..)(..)(..)/, '$1-$2-$3T$4:$5:$6Z')); // }		// if (date) date = date / 60000;