MediaWiki 上的用户自定义 JavaScript 可以调用 Lua 模块吗?

Can user's custom JavaScript on MediaWiki call a Lua module?

在 MediaWiki wiki 上,每个用户都有一个用户 JavaScript 页面,他们可以将代码放入其中,很像 GreaseMonkey,但没有扩展。比如在 User:YourUsername/vector.js

MediaWiki 也有一个嵌入的 Lua,称为 Scribunto,现在已经有一段时间了。

我知道 Lua 模块可以从 MediaWiki 模板调用,我想这是它们的主要用途。但是谷歌搜索和搜索 MediWiki 文档我无法找到是否有一种方法可以从您的用户 JavaScript.

调用 Lua 模块

(我需要将语言名称映射到我的 JS 中的语言代码,并且有一个 Lua 模块可以做到这一点,而无需我用第二种语言复制代码(主要是数据)。)

你不能直接这样做,因为JS在客户端运行,Lua在服务器端运行。你可以做的是使用 the MediaWiki API from JS to invoke the module. Specifically using the expandtemplates API module.

例如,如果您想使用参数 FF(维基文本中的 {{#invoke:hex|h2d|FF}})和 alertModule:Hex 调用函数 h2d结果,那么 JS 将如下所示:

var api = new mw.Api();
api.get( {
    action: 'expandtemplates',
    text: '{{#invoke:hex|h2d|FF}}'
} ).done ( function ( data ) {
    alert(data.expandtemplates['*']);
} );

对于 OP 的具体情况,运行 在英语维基词典上:

var langName = 'Esperanto';
(new mw.Api()).get({
  action: 'expandtemplates',
  format: 'json',
  prop: 'wikitext',
  text: '{{#invoke:languages/templates|getByCanonicalName|' + langName + '|getCode}}'
}).done(function(data) {
  alert('Language name: ' + langName + '\nLanguage code: ' + data.expandtemplates.wikitext);
});

prop: 'wikitext' 避免了来自 API 的警告,并允许您以 data.expandtemplates.wikitext 的形式访问结果,而不是有点神秘的 data.expandtemplates['*']。否则没有区别。)