Jump to content

MediaWiki:Common.js: Difference between revisions

Created page with "→Any JavaScript here will be loaded for all users on every page load.: // favicon var link = document.querySelector("link[rel~='icon']"); if (!link) { link = document.createElement('link'); link.rel = 'icon'; document.getElementsByTagName('head')[0].appendChild(link); } link.href = '//chabadpedia.co.il/images/e/e6/%D7%A1%D7%9E%D7%9C_%D7%97%D7%91%D7%93%D7%A4%D7%93%D7%99%D7%94.png';"
 
GA: skip missing pages; add site_check parameter to filter fake hits
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/**
// favicon
* Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
var link = document.querySelector("link[rel~='icon']");
* loaded for all users on every wiki page. If possible create a gadget that is
if (!link) {
* enabled by default instead of adding it here (since gadgets are fully
    link = document.createElement('link');
* optimized ResourceLoader modules with possibility to add dependencies etc.)
    link.rel = 'icon';
*
    document.getElementsByTagName('head')[0].appendChild(link);
* Since Common.js isn't a gadget, there is no place to declare its
}
* dependencies, so we have to lazy load them with mw.loader.using on demand and
link.href = '//chabadpedia.co.il/images/e/e6/%D7%A1%D7%9E%D7%9C_%D7%97%D7%91%D7%93%D7%A4%D7%93%D7%99%D7%94.png';
* then execute the rest in the callback. In most cases these dependencies will
* be loaded (or loading) already and the callback will not be delayed. In case a
* dependency hasn't arrived yet it'll make sure those are loaded before this.
*/
 
/* global mw, $ */
/* jshint strict:false, browser:true */
 
mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
/* Begin of mw.loader.using callback */
 
/**
* Map addPortletLink to mw.util
* @deprecated: Use mw.util.addPortletLink instead.
*/
mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );
 
/**
* @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
* @rev 6
*/
var extraCSS = mw.util.getParamValue( 'withCSS' ),
extraJS = mw.util.getParamValue( 'withJS' );
 
if ( extraCSS ) {
if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
mw.loader.load( '/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
}
}
 
if ( extraJS ) {
if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
mw.loader.load( '/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
} else {
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
}
}
 
/**
* Collapsible tables; reimplemented with mw-collapsible
* Styling is also in place to avoid FOUC
*
* Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
* @version 3.0.0 (2018-05-20)
* @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
* @author [[User:R. Koot]]
* @author [[User:Krinkle]]
* @author [[User:TheDJ]]
* @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
* is supported in MediaWiki core. Shimmable since MediaWiki 1.32
*
* @param {jQuery} $content
*/
function makeCollapsibleMwCollapsible( $content ) {
var $tables = $content
.find( 'table.collapsible:not(.mw-collapsible)' )
.addClass( 'mw-collapsible' );
 
$.each( $tables, function ( index, table ) {
// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
if ( $( table ).hasClass( 'collapsed' ) ) {
$( table ).addClass( 'mw-collapsed' );
// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
}
} );
if ( $tables.length > 0 ) {
mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
$tables.makeCollapsible();
} );
}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );
 
/**
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
*
* Maintainers: TheDJ
*/
function mwCollapsibleSetup( $collapsibleContent ) {
var $element,
$toggle,
autoCollapseThreshold = 2;
$.each( $collapsibleContent, function ( index, element ) {
$element = $( element );
if ( $element.hasClass( 'collapsible' ) ) {
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
}
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
$element.data( 'mw-collapsible' ).collapse();
} else if ( $element.hasClass( 'innercollapse' ) ) {
if ( $element.parents( '.outercollapse' ).length > 0 ) {
$element.data( 'mw-collapsible' ).collapse();
}
}
// because of colored backgrounds, style the link in the text color
// to ensure accessible contrast
$toggle = $element.find( '.mw-collapsible-toggle' );
if ( $toggle.length ) {
// Make the toggle inherit text color (Updated for T333357 2023-04-29)
if ( $toggle.parent()[ 0 ].style.color ) {
$toggle.css( 'color', 'inherit' );
$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
}
}
} );
}
 
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );
 
/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */
 
/* Main Page: show the language button at the top (in the title bar), as on all other pages */
$( function () {
if ( mw.config.get( 'wgIsMainPage' ) ) {
var $btn = $( '#p-lang-btn' ), $bar = $( '.vector-page-titlebar' );
if ( $btn.length && $bar.length && !$btn.closest( '.vector-page-titlebar' ).length ) {
$btn.addClass( 'mw-portlet-lang-heading-0' ).appendTo( $bar );
}
}
} );
 
/* Google Analytics 4 (property "Chabadpedia English - GA4")
* Counted only: visitors who are not logged in, viewing a page for reading
* (articles, portals, categories, project pages; not history, diffs, edits or special pages),
* and only after a real action on the page (mouse, scroll, touch or key), which most bots never do. */
( function () {
if ( mw.config.get( 'wgUserName' ) !== null || mw.config.get( 'wgAction' ) !== 'view' ) {
return;
}
var ns = mw.config.get( 'wgNamespaceNumber' );
if ( [ 0, 4, 14, 3000 ].indexOf( ns ) === -1 ) {
return;
}
/* Pages that do not exist (404) are not counted; category pages without text are still counted */
if ( mw.config.get( 'wgArticleId' ) === 0 && ns !== 14 ) {
return;
}
if ( /[?&](diff|oldid|curid|printable|redirect)=/.test( location.search ) ) {
return;
}
var id = 'G-V6K7LP5ZTY';
var events = [ 'mousemove', 'scroll', 'touchstart', 'keydown', 'click' ];
var done = false;
function start() {
if ( done ) {
return;
}
done = true;
events.forEach( function ( ev ) {
window.removeEventListener( ev, start, true );
} );
var s = document.createElement( 'script' );
s.async = true;
s.src = 'https://www.googletagmanager.com/gtag/js?id=' + id;
document.head.appendChild( s );
window.dataLayer = window.dataLayer || [];
window.gtag = function () { window.dataLayer.push( arguments ); };
window.gtag( 'js', new Date() );
/* site_check marks hits sent by this code; reports can be filtered on it to drop fake hits sent straight to Google */
window.gtag( 'set', { site_check: 'cp1' } );
window.gtag( 'config', id, { site_check: 'cp1' } );
}
events.forEach( function ( ev ) {
window.addEventListener( ev, start, { capture: true, passive: true } );
} );
}() );
 
/* Related Articles box at the bottom of articles: see [[MediaWiki:RelatedArticles.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:RelatedArticles.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
/* "Read in Hebrew" button at the top of articles: see [[MediaWiki:ReadInHebrew.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ReadInHebrew.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
/* Short description under the article title: see [[MediaWiki:ShortDescription.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ShortDescription.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );

Latest revision as of 17:02, 22 November 2026

/**
 * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally
 * loaded for all users on every wiki page. If possible create a gadget that is
 * enabled by default instead of adding it here (since gadgets are fully
 * optimized ResourceLoader modules with possibility to add dependencies etc.)
 *
 * Since Common.js isn't a gadget, there is no place to declare its
 * dependencies, so we have to lazy load them with mw.loader.using on demand and
 * then execute the rest in the callback. In most cases these dependencies will
 * be loaded (or loading) already and the callback will not be delayed. In case a
 * dependency hasn't arrived yet it'll make sure those are loaded before this.
 */

/* global mw, $ */
/* jshint strict:false, browser:true */

mw.loader.using( [ 'mediawiki.util' ] ).done( function () {
	/* Begin of mw.loader.using callback */

	/**
	 * Map addPortletLink to mw.util
	 * @deprecated: Use mw.util.addPortletLink instead.
	 */
	mw.log.deprecate( window, 'addPortletLink', mw.util.addPortletLink, 'Use mw.util.addPortletLink instead' );

	/**
	 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
	 * @rev 6
	 */
	var extraCSS = mw.util.getParamValue( 'withCSS' ),
		extraJS = mw.util.getParamValue( 'withJS' );

	if ( extraCSS ) {
		if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
			mw.loader.load( '/index.php?title=' + extraCSS + '&action=raw&ctype=text/css', 'text/css' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withCSS value' } );
		}
	}

	if ( extraJS ) {
		if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
			mw.loader.load( '/index.php?title=' + extraJS + '&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
		} else {
			mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
		}
	}

	/**
	 * Collapsible tables; reimplemented with mw-collapsible
	 * Styling is also in place to avoid FOUC
	 *
	 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
	 * @version 3.0.0 (2018-05-20)
	 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
	 * @author [[User:R. Koot]]
	 * @author [[User:Krinkle]]
	 * @author [[User:TheDJ]]
	 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
	 * is supported in MediaWiki core. Shimmable since MediaWiki 1.32
	 *
	 * @param {jQuery} $content
	 */
	function makeCollapsibleMwCollapsible( $content ) {
		var $tables = $content
			.find( 'table.collapsible:not(.mw-collapsible)' )
			.addClass( 'mw-collapsible' );

		$.each( $tables, function ( index, table ) {
			// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
			if ( $( table ).hasClass( 'collapsed' ) ) {
				$( table ).addClass( 'mw-collapsed' );
				// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
			}
		} );
		if ( $tables.length > 0 ) {
			mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
				$tables.makeCollapsible();
			} );
		}
	}
	mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );

	/**
	 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
	 *
	 * Maintainers: TheDJ
	 */
	function mwCollapsibleSetup( $collapsibleContent ) {
		var $element,
			$toggle,
			autoCollapseThreshold = 2;
		$.each( $collapsibleContent, function ( index, element ) {
			$element = $( element );
			if ( $element.hasClass( 'collapsible' ) ) {
				$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
			}
			if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
				$element.data( 'mw-collapsible' ).collapse();
			} else if ( $element.hasClass( 'innercollapse' ) ) {
				if ( $element.parents( '.outercollapse' ).length > 0 ) {
					$element.data( 'mw-collapsible' ).collapse();
				}
			}
			// because of colored backgrounds, style the link in the text color
			// to ensure accessible contrast
			$toggle = $element.find( '.mw-collapsible-toggle' );
			if ( $toggle.length ) {
				// Make the toggle inherit text color (Updated for T333357 2023-04-29)
				if ( $toggle.parent()[ 0 ].style.color ) {
					$toggle.css( 'color', 'inherit' );
					$toggle.find( '.mw-collapsible-text' ).css( 'color', 'inherit' );
				}
			}
		} );
	}

	mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );

	/* End of mw.loader.using callback */
} );
/* DO NOT ADD CODE BELOW THIS LINE */

/* Main Page: show the language button at the top (in the title bar), as on all other pages */
$( function () {
	if ( mw.config.get( 'wgIsMainPage' ) ) {
		var $btn = $( '#p-lang-btn' ), $bar = $( '.vector-page-titlebar' );
		if ( $btn.length && $bar.length && !$btn.closest( '.vector-page-titlebar' ).length ) {
			$btn.addClass( 'mw-portlet-lang-heading-0' ).appendTo( $bar );
		}
	}
} );

/* Google Analytics 4 (property "Chabadpedia English - GA4")
 * Counted only: visitors who are not logged in, viewing a page for reading
 * (articles, portals, categories, project pages; not history, diffs, edits or special pages),
 * and only after a real action on the page (mouse, scroll, touch or key), which most bots never do. */
( function () {
	if ( mw.config.get( 'wgUserName' ) !== null || mw.config.get( 'wgAction' ) !== 'view' ) {
		return;
	}
	var ns = mw.config.get( 'wgNamespaceNumber' );
	if ( [ 0, 4, 14, 3000 ].indexOf( ns ) === -1 ) {
		return;
	}
	/* Pages that do not exist (404) are not counted; category pages without text are still counted */
	if ( mw.config.get( 'wgArticleId' ) === 0 && ns !== 14 ) {
		return;
	}
	if ( /[?&](diff|oldid|curid|printable|redirect)=/.test( location.search ) ) {
		return;
	}
	var id = 'G-V6K7LP5ZTY';
	var events = [ 'mousemove', 'scroll', 'touchstart', 'keydown', 'click' ];
	var done = false;
	function start() {
		if ( done ) {
			return;
		}
		done = true;
		events.forEach( function ( ev ) {
			window.removeEventListener( ev, start, true );
		} );
		var s = document.createElement( 'script' );
		s.async = true;
		s.src = 'https://www.googletagmanager.com/gtag/js?id=' + id;
		document.head.appendChild( s );
		window.dataLayer = window.dataLayer || [];
		window.gtag = function () { window.dataLayer.push( arguments ); };
		window.gtag( 'js', new Date() );
		/* site_check marks hits sent by this code; reports can be filtered on it to drop fake hits sent straight to Google */
		window.gtag( 'set', { site_check: 'cp1' } );
		window.gtag( 'config', id, { site_check: 'cp1' } );
	}
	events.forEach( function ( ev ) {
		window.addEventListener( ev, start, { capture: true, passive: true } );
	} );
}() );

/* Related Articles box at the bottom of articles: see [[MediaWiki:RelatedArticles.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:RelatedArticles.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
/* "Read in Hebrew" button at the top of articles: see [[MediaWiki:ReadInHebrew.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ReadInHebrew.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );
/* Short description under the article title: see [[MediaWiki:ShortDescription.js]] */
mw.loader.load( mw.config.get( 'wgScript' ) + '?title=MediaWiki:ShortDescription.js&action=raw&ctype=text/javascript&maxage=300&smaxage=300' );