如何在MediaWiki中添加HTML到head标签而不使用扩展?

4

我希望在我拥有的MediaWiki (1.33)网站的所有网页的head标签中添加HTML。

我没有找到任何关于如何执行此操作的文档(或者QA会话中没有清晰的答案)。

这促使我尝试通过JavaScript进行如下添加:

var templateLiteral = [`
    <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({
        google_ad_client: "ca-pub-VALUE",
        enable_page_level_ads: true
    });
    </script>
`]

var head = document.querySelector("head");
head.innerHTML += templateLiteral;

这种方法似乎行得通,但因为通常情况下搜索引擎爬虫不会运行 <script> 标签中的脚本(虽然谷歌好像可以执行),所以被认为是非传统的。

我没有找到 HTML 和 PHP 的模板 PHP 文件,因为 MediaWiki 没有模板引擎(例如 Twig)。

如何在 MediaWiki 中添加 HTML 到 head 标签而不使用扩展?
我只有一个扩展(ContactPage),并且我想保持这种方式...


我知道你不想要扩展,但以防你改变主意:https://www.mediawiki.org/wiki/Extension:HeadScript - Jon P
你可以将通常存在于扩展中的代码转储到LocalSettings.php文件中(在这种情况下,类似于$wgHooks ['BeforePageDisplay'] [] = function(OutputPage $out) {$out->addScript('<script...');};)。请注意,这并没有比使用实际的扩展有任何优势。 - Tgr
1个回答

0

我知道我回答有点晚了,但对于寻找同样答案的人来说,可以使用LocalSettings.php来实现,只需包含以下代码:

// Define a function to append the HTML code to the <head> tag
function add_html_to_head(&$out, &$skin) {
    $out->addHeadItem('my_html_code', 'add_your_script_or_html_here');
}
// Register the function to the BeforePageDisplay hook
$wgHooks['BeforePageDisplay'][] = 'add_html_to_head';

以上代码将直接将您添加在add_your_script_or_html_here中的任何内容添加到您的MediaWiki的head标签中。

希望这能帮到您 : )


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接