使用方法/页面工具/工具扩展:修订间差异
来自康健生活
| 第2行: | 第2行: | ||
=== Basic setup === | === Basic setup === | ||
=== Extension JavaScript === | === Extension JavaScript === | ||
在配置文件里面添加'''$wgAllowUserJs''',之后在特殊页面(e.g.:[[http://kangkk.cn/index.php/用户:Kangkk/common.js| | 在配置文件里面添加'''$wgAllowUserJs''',之后在特殊页面(e.g.:[[http://kangkk.cn/index.php/用户:Kangkk/common.js | Kangkk/common.js]])添加JS代码。 | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
$wgAllowUserJs = true; | $wgAllowUserJs = true; | ||
2021年4月3日 (六) 16:49的版本
工具扩展
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
参考
No categories