如何为所有mediawiki页面的<head>部分添加外部<script>?

 ShenTing止想念 发布于 2022-12-20 18:34

我想为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;
};

在这个功能我想添加



wiki中所有页面的部分.

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

$out->addScript( $html )
// Add a JS file. $html is a full script tag: ''

但现在

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

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

我无法使它工作,也没有找到任何这方面的例子.

ResourceLoader描述

Default_modules描述

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

PS 这个解决方案的工作,但不正确的.需要使用ResourseLoader的解决方案.(c)中IMHO

1 个回答
  • 解决方案很简单(看起来像第二个解决方案):

    //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(解析后).

    2022-12-20 18:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有