【问题标题】:How to add external <script> to <head> section for all mediawiki pages?如何将外部 <script> 添加到所有 mediawiki 页面的 <head> 部分?
【发布时间】:2014-09-18 08:28:25
【问题描述】:

我想将外部脚本添加到 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;
};

在这个函数中我想添加

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

&lt;head&gt; wiki 中所有页面的部分。

对于旧版本的 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(array(/modules/));

我无法让它工作,也没有找到任何例子。

ResourceLoader description

Default_modules description

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

附: this 解决方案工作,但不正确。需要使用 ResourceLoader 的解决方案。 (c)恕我直言

【问题讨论】:

  • ResourseLoader 无法加载外部脚本。我写了我自己的扩展来做到这一点。更好地使用我的答案中的解决方案。 И у тебя в коде яваскрипт вставлен в пхп。 :)

标签: javascript php mediawiki


【解决方案1】:

解决方案很简单(看起来像2nd 解决方案):

//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;
};

这种方式比this 更好看,因为它直接与OutputPage 一起工作(在解析之后)。

【讨论】:

猜你喜欢
  • 2017-03-18
  • 2014-07-12
  • 2012-03-03
  • 2017-12-27
  • 2022-10-24
  • 2022-08-15
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多