使用方法/页面工具/工具扩展:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 Kangkk移动页面使用方法/工具扩展至使用方法/页面工具/工具扩展,不留重定向
(没有差异)

2026年9月7日 (一) 07:28的版本

工具扩展

Basic setup

Extension JavaScript

在配置文件里面添加$wgAllowUserJs,之后在特殊页面(e.g.:Kangkk/common.js)添加JS代码。

$wgAllowUserJs = true;

缺点:

  • 频繁更改common.js,造成过多页面历史
  • JS代码在所有页面都会加载执行

定义资源加载程序包

定义资源加载程序包分为三步,

$wgResourceModules['ext.MyExtension']['scripts'][] = 'extensions/MyExtension/js/MyExtension.js';
$wgHooks['EditPage::showEditForm:initial'][] = 'addModule';
function addModule(EditPage $editPage, OutputPage $output ) {
    $output->addModules( 'ext.MyExtension' );
}

这里需要注意的是在加载JS文件时,会发生JS代码加载延迟,所以尽量将代码放在下列代码中。

var customizeToolbar = function () {
	/* Your code goes here */
};

/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
	mw.loader.using( 'user.options' ).then( function () {
		// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
		if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
			$.when(
				mw.loader.using( 'ext.wikiEditor' ), $.ready
			).then( customizeToolbar );
		}
	} );
}

Content

→ Content

参考