帮助:扩展:GlobalCssJs

This page is a translated version of the page Help:Extension:GlobalCssJs and the translation is 67% complete.
Outdated translations are marked like this.

The GlobalCssJs extension allow users to create a JavaScript page and a CSS page to be loaded in every wiki of a wiki farm. If you go to 参数设置 > 外观 on any wiki where the extension is enabled you'll find links to your global scripts and style sheets:

所有wiki共用的CSS/JavaScript(更多信息):

On Wikimedia wikis, these global customizations are hosted on Meta-Wiki.

全域脚本(JavaScript)

变量

When adding scripts to your global.js, be aware that, as with gadgets, variables declared with "var example" are not attached to the window object: they are local variables whose scope is a wrapper function inserted by ResourceLoader to implement the global module (which is called "ext.globalcssjs.user"). Therefore, if you plan to move a local script to the global module, and it needs to define global variables, make sure you use the syntax "window.example" to declare them.

例子

/* 任何加入此页的JavaScript将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */
window.myConfig = true;
// [[wikipedia:User:Lupin/popups]]
window.popupAdminLinks = true;
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Lupin/popups.js&action=raw&ctype=text/javascript' );

Explicit URLs

Functions like importScript will not work because those use the local wiki; they do not use a page with this name on the global wiki. Therefore you will need to pass a full URL to load the script.

例子

/* 任何加入此页的JavaScript将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */

// If you want: importScript( 'User:Lupin/popups.js' ); from en.wikipedia.org, actually write:
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Lupin/popups.js&action=raw&ctype=text/javascript' );

Per-wiki customization

例子

/* 任何加入此页的JavaScript将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */
// Define a few functions
function onMultiLingualWikis(){
	// ...
}
function onWikibooks(){
	// ...
}
function onFrench(){
	// ...
}
function onRuWikisource(){
	// ...
}
function onEveryWiki(){
	mw.loader.load( '//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-UTCLiveClock.js&action=raw&ctype=text/javascript' );
}
onEveryWiki();
if ( /^(mediawiki|wikidata|meta|commons)wiki$/.test( mw.config.get( 'wgDBname' ) ) ) {
	onMultiLingualWikis();
} else if ( /wikibooks$/.test( mw.config.get( 'wgDBname' ) ) ) {
	onWikibooks();
} else if ( mw.config.get( 'wgContentLanguage' ) === 'fr' ) {
	onFrench();
} else if ( mw.config.get( 'wgDBname' ) === 'ruwikisource' ) {
	onRuWikisource();
}

排除某个维基

If you want to exclude a specific wiki, E.g. English Wikisource, you can wrap all or part of your global.js with:

if ( mw.config.get("wgDBname") !== "enwikisource" ) {
	// Whatever JavaScript you want to run on all wikis but enwikisource, goes here
}

例子:设置一个全维基的界面语言

Note: Extension:GlobalPreferences (if present) provides this functionality more conveniently.

/* Change language to German */
mw.loader.using( 'mediawiki.user', function() {
	if ( mw.user.options.get( 'language' ) !== 'de' ) {
		( new mw.Api() ).postWithToken( 'csrf', {
			action: 'options',
			change: 'language=de'
		} ).then( function() {
			mw.notify( 'Language has been changed to German. Please refresh the page.' );
		} );
	} else {
		console.log( 'Language already set to German!' );
	}
} );

全域样式表(CSS)

Note: Any @import ... lines must be at the top.

例子

/* 任何加入此页的CSS将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */

/* Hide a few elements of the interface */
#n-help,
#footer {
	display: none !important;
}

Per-skin customization

Currently the extension does not provide global CSS/JS for specific skins, but it is possible to customize CSS and JS for a skin globally. For CSS, you can edit the appearance of a specific skin by using classes such as "skin-vector" and "skin-monobook", which are added to the body element automatically by MediaWiki. You can use the :not() selector to skip just one skin, e.g. :not(.skin-minerva) to not apply the rule to the mobile skin.

例子

/* 任何加入此页的CSS将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */

/* Hide a few elements of the interface on vector skin */
.skin-vector #n-help,
.skin-vector #footer {
	display: none !important;
}

/* Bold the sidebar interwiki links to Enwiki/Simple, and interproject links to Commons/WikiVoyage */
.interwiki-en, .interwiki-simple, .wb-otherproject-commons, .wb-otherproject-wikivoyage {
        font-weight: bold;
}

Example (JS)

/* 任何加入此页的JavaScript将在所有您拥有账户的wiki载入(参见[[mw:Special:MyLanguage/Help:Extension:GlobalCssJs|文档]])。 */
//Load JWB globally when using the Vector skin

if ( mw.config.get( 'skin' ) === 'vector' ) {
	mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript');
}