MediaWiki:Gadget-wikibugs.js

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
JS-код ниже относится к скрытому гаджету: Включить возможность отправлять сообщения об ошибках на странице через форму (править описание). Он включён по умолчанию.

После сохранения или недавних изменений очистите кэш браузера.

/*
 * Delayed script load for [[MediaWiki:Gadget-wikibugs-core.js]]
 */
mw.loader.using( 'mediawiki.util', () => {
	//'use strict';
	// Works around https://phabricator.wikimedia.org/T238386
	mw.messages.set( 'bug_in_article', 'Сообщить об ошибке' );
	
	// To define this variable in one place
	window.wb$bugsPage = 'Википедия:Сообщения об ошибках';
	
	var wb$ignoredPageIds = [
		275424, // YouTube
		639373, // VK
		932117, // Odnoklassniki
		919912, // Facebook
		1297302, // Pavel Durov
		
		1534053, // Википедия:Сообщения об ошибках
	];
	if ( wb$ignoredPageIds.includes( mw.config.get( 'wgArticleId' ) ) ) {
		return;
	}
	
	// Load the gadget code on click
	function loadOnClick( e ) {
		e.preventDefault();
		if ( typeof window.wb$runWikibug === 'undefined' ) {
			mw.loader.using( 'ext.gadget.wikibugs-core' ).done( () => {
				window.wb$runWikibug();
			} ).catch( () => {
				// Go to the page in case the gadget doesn’t load 
				window.location = mw.util.getUrl( wb$bugsPage );
			} );
			return;
		}
		window.wb$runWikibug();
	}
	
	// Load on Space and Enter
	function clickOnKeydown( e ) {
		if ( [ 'Space', 'Enter' ].includes( e.code ) ) {
			e.preventDefault();
			this.click();
		}
	}
	
	// Set up button from provided link
	function setupButton( link ) {
		if ( !link ) return;
		
		link.setAttribute( 'role', 'button' );
		
		link.addEventListener( 'click', loadOnClick );
		link.addEventListener( 'keydown', clickOnKeydown );
	}
	
	$( () => {
		var link = document.querySelector( '#n-bug_in_article a' );
		setupButton( link );
		
		// If the link hasn't been added in the sidebar on the existing/non-special page, then create a new link
		// For discoverability also add a toolbox link to new Vector
		if ( mw.config.get( 'wgArticleId' ) !== 0 && ( !link || mw.config.get( 'skin' ) === 'vector-2022' ) ) {
			var id = 'n-bug_in_article';
			var href = mw.util.getUrl( wb$bugsPage );
			var textContent = mw.msg( 'bug_in_article' );
			var isTalkPage = mw.config.get( 'wgCanonicalNamespace' ).toLowerCase().includes( 'talk' );
			// prefer the toolbox (available to logged in users)
			link = mw.util.addPortletLink( 'p-tb', href, textContent, id );
			if ( link ) {
				link = link.querySelector( 'a' );
			} else if ( !link && !isTalkPage ) {
				// but if no toolbox (e.g. anonymous user, add to end of article)
				link = document.createElement( 'a' );
				link.textContent = textContent;
				link.href = href;
				var content = document.getElementById( 'mw-content-text' );
				if ( content ) {
					content.appendChild( link );
				}
			}
			setupButton( link );
		}
	} );
} );