如何添加外部< script>到< head>所有mediawiki页面的部分? [英] How to add external <script> to <head> section for all mediawiki pages?

查看:104
本文介绍了如何添加外部< script>到< head>所有mediawiki页面的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want add external script to head section for all pages in mediawiki.

函数onBeforePageDisplay回调: >

Function onBeforePageDisplay callback from BeforePageDisplay hook:

//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>部分.

to <head> section for all pages in wiki.

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

For old versions of mediawiki used addScript method of OutputPage object:

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

但现在

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

For MediaWiki 1.17 and above, use ResourceLoader modules.

$ out-> addModules(array(/模块/));

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

我无法使其正常工作,也找不到任何示例.

I could not make it work and don't find any examples of this.

ResourceLoader描述

默认模块说明

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

Maybe I have to use mw.loader.load module, but I have no idea how to do it. Help me, please, and sorry for my english.

P.s. 解决方案有效,但不正确.需要使用过的ResourseLoader的解决方案. (c)恕我直言

P.s. this solution work, but is not right. Need solution with used ResourseLoader. (c)IMHO

推荐答案

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

Solution was simple (it looks like 2nd solution):

//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一起使用(解析后).

This way look better then this, because it work with OutputPage directly (after parsing).

这篇关于如何添加外部&lt; script&gt;到&lt; head&gt;所有mediawiki页面的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆