User:Nardog/FriendlyTagFilter.js

/* (mw.config.get('wgAction') === 'history' || ['Contributions', 'Log', 'Newpages'].includes(mw.config.get('wgCanonicalSpecialPageName')) || ['Recentchanges', 'Recentchangeslinked'].includes(mw.config.get('wgCanonicalSpecialPageName')) && !document.body.classList.contains('mw-rcfilters-enabled')) && mw.loader.using([	'mediawiki.api', 'mediawiki.storage', 'oojs-ui-core' ], function friendlyTagFilter {	mw.loader.addStyleTag('.friendlytagfilter-hidden::after{content:" (hidden)";color:#72777d} .friendlytagfilter-inactive::after{content:" (inactive)";color:#72777d} .friendlytagfilter-hidden.friendlytagfilter-inactive::after{content:" (hidden, inactive)"');	let replace = ($input, options) => {		let items = options.map(o => { let config; if (typeof o === 'string') { config = { data: o, label: o }; } else { config = { $label: $(' '), data: o[0], label: o[1] === 1 ? $(' ').text(o[0]) : o[1] || o[0] };				if (o[1] === 1) config.$label.addClass('friendlytagfilter-hidden'); if (o[2]) config.$label.addClass('friendlytagfilter-inactive'); }			return new OO.ui.MenuOptionWidget(config); });		let inputId = $input.prop('id');		let widget = new OO.ui.ComboBoxInputWidget({ $overlay: true, id: inputId !== 'tagfilter' && 'tagfilter', inputId: inputId, menu: { items: items }, name: $input.prop('name'), value: $input.val });		widget.$input.addClass('friendlytagfilter');		$input.closest('#tagfilter').replaceWith(widget.$element);		widget.focus.getMenu.toggle(true);	};	$(document.body).on('focus', '#tagfilter > input:not(.friendlytagfilter), input#tagfilter:not(.friendlytagfilter)', function { let $input = $(this).addClass('friendlytagfilter'); let options = mw.storage.session.getObject('friendlytagfilter'); if (options) { replace($input, options); return; }		$input.addClass('oo-ui-pendingElement-pending'); new mw.Api.get({			action: 'query',			list: 'tags',			tglimit: 'max',			tgprop: 'active|displayname|hitcount',			formatversion: 2		}).always(response => {			let tags = ((response || {}).query || {}).tags;			if (!tags || !tags.length) {				$input.removeClass('friendlytagfilter oo-ui-pendingElement-pending');				mw.notify('Couldn\'t load tags.', { tag: 'friendlytagfilter', type: 'warn' });				return;			}			options = tags				.filter(tag => tag.hitcount)				.sort((a, b) => b.hitcount - a.hitcount)				.map(tag => { let label; if (tag.displayname) { label = $($.parseHTML(tag.displayname)).text; if (label === tag.name) { if (tag.active) return label; label = 0; }					} else { label = 1; }					let arr = [tag.name, label]; if (!tag.active) arr.push(1); return arr; });			replace($input, options);			mw.storage.session.setObject('friendlytagfilter', options);		}); }); }); */