如何添加外部<;脚本>;至<;头部>;部分


How to add external <script> to <head> section for all mediawiki pages?

我想将外部脚本添加到mediawiki中所有页面的标题部分。

函数onBeforePageDisplayBeforePageDisplay挂钩回调:

//LocalSettings.php
...
# Assign my functions to hook
$wgHooks['BeforePageDisplay'][] ='onBeforePageDisplay';
function onBeforePageDisplay( OutputPage &$out, Skin &$skin )
{
    mw.loader.load('http://static.wowhead.com/widgets/power.js', 'text/javascript');
    $out->addModules( 'mw.loader' );
    return true;
};

在这个功能中,我想添加

<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script>
<script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }</script>

到wiki中所有页面的<head>部分。

对于旧版本的mediawiki,使用OutputPage对象的addScript方法:

$out->addScript( $html )
// Add a JS file. $html is a full script tag: '<script type="text/javascript" src="..."></script>'

但现在

对于MediaWiki 1.17及以上版本,请使用ResourceLoader模块。

$out->addModules(数组(/modules/));

我无法让它发挥作用,也找不到任何这样的例子。

ResourceLoader描述

默认模块描述

也许我必须使用mw.loader.load模块,但我不知道该怎么做。请帮帮我,对不起我的英语。

附言,这个解决方案有效,但不对。需要使用已使用的资源加载器的解决方案。(c) IMHO

解决方案很简单(看起来像第二个解决方案):

//LocalSettings.php
...
# Assign my functions to hook
$wgHooks['BeforePageDisplay'][] ='onBeforePageDisplay';
function onBeforePageDisplay( OutputPage &$out, Skin &$skin )
{
    $script = '<script type="text/javascript" src="http://static.wowhead.com/widgets/power.js"></script><script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }</script>';
    $out->addHeadItem("wowhead script", $script);
    return true;
};

这种方式看起来比这种方式更好,因为它直接使用OutputPage(解析后)。