MediaWiki:Gadget-findstubs.js

Fra Wikipedia, den frie encyklopedi

Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.

  • Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
  • Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
  • Internet Explorer / Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
  • Opera: Ttrykk Ctrl+F5.
/**
 * Add "find stubs" button to categories
 *
 * This gadget adds a button to category pages that links
 * to search results for stubs in the current category.
 *
 * @author Jon Harald Søby
 * @version 1.1.1 (2023-12-05)
 */
( function() {
	'use strict';

	if ( !$( '.mw-category-generated' ).find( 'h2' ).length ) {
		// Return early if the category has no content.
		return;
	}

	const config = {
		stubTemplate: 'Spire', // Name of the stub template
		hideInfoButton: mw.storage.get( 'userjs-findstubs-hidebutton' )
	};

	const messages = {
		'findstubs-label': 'Finn spirer i denne kategorien',
		'findstubs-title': 'Finn spirer',
		'findstubs-info': 'Om denne funksjonen',
		'findstubs-about': 'Knappen for å finne spirer er en tilleggsfunksjon som er slått på som standard. Du kan slå den av med knappen nedenfor.',
		'findstubs-disable': 'Slå av',
		'findstubs-removebutton': 'Fjern infoknappen',
		'findstubs-reenable': 'Takk for at du prøvde tilleggsfunksjonen! Du kan alltids slå den på igjen i [[$1|innstillingene]].'
	};
	mw.messages.set( messages );

	mw.util.addCSS( '\
	.userjs-findstubs-container {\n\
		float: inline-end;\n\
		margin-inline: 1em 0;\n\
		margin-block: 0 1em;\n\
	}' );

	const findStubButton = new OO.ui.ButtonWidget( {
		label: mw.msg( 'findstubs-label' ),
		icon: 'articlesSearch',
		title: mw.msg( 'findstubs-title' ),
		href: mw.util.getUrl( 'Special:Search', {
			search: 'deepcategory:"' + mw.config.get( 'wgTitle' ) + '" hastemplate:"' + config.stubTemplate + '"',
			ns0: 1,
			limit: 100
		} ),
		target: '_blank'
	} );

	const removeButtonButton = new OO.ui.ButtonWidget( {
		label: mw.msg( 'findstubs-removebutton' ),
		flags: 'progressive'
	} ).on( 'click', function() {
		mw.storage.set( 'userjs-findstubs-hidebutton' );
		infoButton.toggle( false );
	} );

	const disableButton = new OO.ui.ButtonWidget( {
		label: mw.msg( 'findstubs-disable' ),
		flags: [ 'destructive', 'primary' ],
	} ).on( 'click', function() {
		$infoText.html( mw.message( 'findstubs-reenable', 'Special:Preferences#mw-prefsection-gadgets' ).parse() );
		new mw.Api().postWithEditToken( {
			action: 'options',
			optionname: 'gadget-findstubs',
			optionvalue: 0
		} );
	} );

	const infoButtons = new OO.ui.HorizontalLayout( {
		items: [
			disableButton,
			removeButtonButton
		]
	} );

	const $infoText = $( '<div>' )
		.append( '<p>' + mw.msg( 'findstubs-about' ) + '</p>' )
		.append( infoButtons.$element );

	const infoButton = new OO.ui.PopupButtonWidget( {
		icon: 'info',
		framed: false,
		invisibleLabel: true,
		label: mw.msg( 'findstubs-info' ),
		popup: {
			head: true,
			icon: 'infoFilled',
			label: mw.msg( 'findstubs-info' ),
			$content: $infoText,
			padded: true
		}
	} ).toggle( !config.hideInfoButton );

	const buttonLayout = new OO.ui.HorizontalLayout( {
		items: [
			findStubButton,
			infoButton
		],
		classes: [
			'userjs-findstubs-container'
		]
	} );

	$( '.mw-category-generated' ).prepend( buttonLayout.$element );
} )();