除了编辑之外,有没有办法在页眉中修改 MediaWiki 搜索表单Vector.php
?
基本上,我想更改/扩展 HTML 表单的标记并为 Ajax 调用添加一个 JavaScript 侦听器。
不幸的是,我似乎无法找到合适的钩子。
除了编辑之外,有没有办法在页眉中修改 MediaWiki 搜索表单Vector.php
?
基本上,我想更改/扩展 HTML 表单的标记并为 Ajax 调用添加一个 JavaScript 侦听器。
不幸的是,我似乎无法找到合适的钩子。
如果您想更改 HTML,这并不容易。但是要添加 JavaScript 侦听器,您通常不需要直接向要侦听事件的输入添加内容。
例如,您可以使用 jQuery 向搜索输入添加侦听器。为此,您可以创建一个新的扩展(阅读本手册以获得快速入门)。在您的扩展中,您创建一个新的资源模块:
{
"@comment": "Other configuration options may follow here"
"ResourceFileModulePaths": {
"localBasePath": "",
"remoteSkinPath": "ExampleExt"
},
"ResourceModules": {
"ext.ExampleExt.js": {
"scripts": [
"resources/ext.ExampleExt.js/script.js"
]
}
},
"@comment": "Other configuration options may follow here"
}
现在,您可以添加在模块中定义的脚本文件:
( function ( $ ) {
$( '#searchInput' ).on( 'change', function () {
// do whatever you want when the input
// value changed
}
}( jQuery ) );
只要搜索输入的值发生变化,函数中的代码(在 on() 函数的第二个参数中)就会运行。
现在,您只需要在 MediaWiki 输出页面时加载您的模块。最简单的方法是使用BeforePageDisplay
钩子:
注册钩子处理程序:
{
"@comment": "Other configuration options may follow here"
"Hooks": {
"BeforePageDisplay": [
"ExampleExtHooks::onBeforePageDisplay"
],
},
"@comment": "Other configuration options may follow here"
}
处理钩子(在ExampleExtHooks
类中,需要创建并添加到 Autoload 类中):
public static function onBeforePageDisplay( OutputPage &$output, Skin &$skin ) {
$output->addModules( array(
'ext.ExampleExt.js',
) );
return true;
}
首先,我添加了一个钩子:
$wgHooks['BeforePageDisplay'][] = 'MyNamespace\Hooks::onBeforePageDisplay';
钩子很简单:
public static function onBeforePageDisplay( \OutputPage &$out, \Skin &$skin ) {
$skin->template = '\MyNamespace\Template';
}
最后,Template
该类覆盖了renderNavigation()
呈现搜索表单的方法:
<?php
namespace XtxSearch;
class Template extends \VectorTemplate {
protected function renderNavigation( $elements ) {
...
}
}