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

馋奶兔 提交于 2019-12-22 05:45:09

问题


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

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

In this function i want to add

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

to <head> section for all pages in wiki.

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>'

but now

For MediaWiki 1.17 and above, use ResourceLoader modules.

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

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

ResourceLoader description

Default_modules description

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. this solution work, but is not right. Need solution with used ResourseLoader. (c)IMHO


回答1:


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

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



来源:https://stackoverflow.com/questions/25907743/how-to-add-external-script-to-head-section-for-all-mediawiki-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!