最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add external <script> to <head> section for all mediawiki pages? - Stack Overflo

programmeradmin1浏览0评论

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('.js', 'text/javascript');
    $out->addModules( 'mw.loader' );
    return true;
};

In this function i want to add

<script type="text/javascript" src=".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

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

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Sep 18, 2014 at 8:28 AlexeyAlexey 4537 silver badges13 bronze badges 1
  • There is no way where you can load external script by ResourseLoader. I wrote my own extension to do that. Better use solution from my answer. И у тебя в коде яваскрипт вставлен в пхп. :) – Artem P Commented Oct 16, 2014 at 0:01
Add a ment  | 

1 Answer 1

Reset to default 6

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./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).

发布评论

评论列表(0)

  1. 暂无评论