如何为所有mediawiki页面的<head>部分添加外部<script>?

Ale*_*xey 6 javascript php mediawiki

我想为mediawiki中的所有页面添加外部脚本到head部分.

钩子的函数onBeforePageDisplay回调BeforePageDisplay:

//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;
};
Run Code Online (Sandbox Code Playgroud)

在这个功能我想添加

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

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

对于旧版本的mediawiki使用addScriptOutputPage对象的方法:

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

但现在

对于MediaWiki 1.17及更高版本,请使用ResourceLoader模块.

$ out-> addModules(array(/ modules /));

我无法使它工作,也没有找到任何这方面的例子.

ResourceLoader描述

Default_modules描述

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

PS 这个解决方案的工作,但不正确的.需要使用ResourseLoader的解决方案.(c)中IMHO

Ale*_*xey 6

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

//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;
};
Run Code Online (Sandbox Code Playgroud)

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