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

I want to add an external script to the main section for all pages in mediawiki.

Function onBeforePageDisplay callback 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> for all pages in the wiki.

For older versions of mediawiki, the addScript OutputPage object method is used :

 $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 higher, use the ResourceLoader modules.

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

I could not get it to work and did not find examples of this.

Description of ResourceLoader

Description of Default_modules

Maybe I need to use the mw.loader.load module, but I have no idea how to do this. Please help me and sorry for my English.

Ps this one works, but it’s not right. Need a solution using ResourseLoader. (C) IMHO

+6
source share
1 answer

The solution was simple (it looks like 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 method looks better than this , because it works with OutputPage directly (after parsing).

+6
source

Source: https://habr.com/ru/post/975474/


All Articles