Jump to content

User:Qwerfjkl/scripts/CFDlister.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Qwerfjkl (talk | contribs) at 09:41, 25 June 2022 (Created page with 'mw.config.get('wgNamespaceNumber') >= 0 && $.when($.ready, mw.loader.using('mediawiki.util')).then(function copySectLink() { let handler = e => { e.preventDefault(); let text = (e.data ? '#' + e.data.replace( /[\[\]\{\|\}]/g, s => '&#' + s.codePointAt(0) + ';' ) : '').replace(/_/g, ' '); text = text.replace(/#/, ''); listCFD() }; let addButton = function () { let isFirst = this.tagName === 'H1'; let hn = isFirst ? this : this.parent...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.config.get('wgNamespaceNumber') >= 0 &&
$.when($.ready, mw.loader.using('mediawiki.util')).then(function copySectLink() {
	let handler = e => {
		e.preventDefault();
		let text = (e.data ? '#' + e.data.replace(
			/[\[\]\{\|\}]/g,
			s => '&#' + s.codePointAt(0) + ';'
		) : '').replace(/_/g, ' ');
		text = text.replace(/#/, '');
		listCFD()
	};
	
	let addButton = function () {
		let isFirst = this.tagName === 'H1';
		let hn = isFirst ? this : this.parentElement;
		if (hn.querySelector('.listcfdlink')) return;
		let $button = $('<a>', {
			class: 'listcfdlink',
			href: mw.util.getUrl() + (isFirst ? '' : '#' + this.id),
			role: 'button',
			text: 'list'
		}).click(!isFirst && this.id, handler);
		if (isMobile) {
			$button.addClass('mw-ui-icon mw-ui-icon-element').attr('title', 'List');
			let wrapper = hn.querySelector('.mw-editsection');
			if (wrapper) {
				$button.prependTo(wrapper);
			} else {
				$button.appendTo(hn);
			}
		} else {
			let bracket = hn.querySelector('.mw-editsection-bracket:last-child');
			if (bracket) {
				bracket.insertAdjacentText('beforebegin', ' | ');
			} else {
				bracket = $('<span>').addClass('mw-editsection').append(
					$('<span>', { class: 'mw-editsection-bracket', text: '[' }),
					$('<span>', { class: 'mw-editsection-bracket', text: ']' })
				).appendTo(hn).children()[1];
			}
			$button.insertBefore(bracket);
		}
	};
	addButton.call(document.querySelector('h1'));
	if (mw.config.get('wgAction') !== 'view') return;
	mw.hook('wikipage.content').add($content => {
		$content.find('.mw-headline').each(addButton);
	});
});