MediaWiki talk:Gadget-libLua.js
Wiktionary
[edit]I'm trying to use this script on Wiktionary. I would like to be able to, for instance, use a module to return the language name associated with a language code. I was using a test module, wikt:Module:User:Erutuon/languages. The code I placed in the script sandbox was the following:
if ( namespace === 14 )
{
var catfix = $("#catfix");
var lang;
var langName;
if ( catfix.length )
{
lang = catfix.children().first().attr("lang");
mw.loader.using(
[ 'ext.gadget.libLua' ],
function ()
{
mw.libs.lua
.call( {
module: 'User:Erutuon/languages',
func: 'getCanonicalName',
args: [ lang ]
} )
.then(
function ( result )
{
langName = result;
mw.notify("This is the user script sandbox speaking. There is a catfix on this page, and it has the language code " + lang + " in it, representing the " + result + " language.")
}
);
},
function()
{
mw.notify("Error!!!")
}
);
mw.notify("langName is " + langName + ".")
}
}
I also added mw.loader.load("ext.gadget.libLua");
to my common.js. (Then I did a more explicit call: mw.loader.load("//en.wiki.x.io/w/index.php?title=MediaWiki:Gadget-libLua.js&action=raw&ctype=text/javascript");
. No difference.)
But, when loading a category page containing a catfix with a language code, I consistently get the notifications "Error!!!" and "langName is undefined." There are no console messages from the script sandbox script that I can see, though there is a message [Violation] Forced reflow while executing JavaScript took 64ms
that I don't understand.
What's going wrong? Is this script not currently working or not supposed to work on Wiktionary, or is my code faulty? — Eru·tuon 21:04, 22 May 2017 (UTC)
- @Erutuon: For you to be able to load this script with
ext.gadget.libLua
from Wiktionary, it needs to be made into a gadget there. That involves loading it from a page in Wiktionary's MediaWiki namespace, and then giving it an entry in wikt:MediaWiki:Gadgets-definition. If you load it directly withmw.loader.load("//en.wiki.x.io/w/index.php?title=MediaWiki:Gadget-libLua.js&action=raw&ctype=text/javascript");
then you can avoid fiddling around with gadget definitions, but then you need to load the script's dependencies manually. libLua depends on themediawiki.api
module, so you need to load that as well, either in your script or in your common.js. If that doesn't fix the problem, let me know and I'll take a look at it. — Mr. Stradivarius ♪ talk ♪ 08:26, 23 May 2017 (UTC)- @Mr. Stradivarius: Okay, I went back to the raw loading of this gadget, since it isn't installed on Wiktionary as far as I know. I'm sorry for such a basic question, but how do I load
mediawiki.api
? I triedmw.loader.load("mediawiki.api");
andmw.loader.load("mw.api");
, but neither seems to makeliblua
work. [Edit: if it helps,mw.notify(mw.config.values.wgEnableAPI)
istrue
.] — Eru·tuon 18:23, 23 May 2017 (UTC)- @Erutuon:
mw.loader.load("mediawiki.api");
is the correct syntax, so I'm not sure why it wasn't working for you. I was able to get it to work on Wiktionary in my browser console by entering the following (one after the other, as the scripts need time to load):
- @Erutuon:
- @Mr. Stradivarius: Okay, I went back to the raw loading of this gadget, since it isn't installed on Wiktionary as far as I know. I'm sorry for such a basic question, but how do I load
mw.loader.load("mediawiki.api");
mw.loader.load("//en.wiki.x.io/w/index.php?title=MediaWiki:Gadget-libLua.js&action=raw&ctype=text/javascript");
mw.libs.lua.call({module: 'User:Erutuon/languages', func: 'getCanonicalName', args: [ 'en' ]}).then(function (result) { console.log(result); }); // logs "English"
- Give that a try and see if it works for you. Best — Mr. Stradivarius ♪ talk ♪ 14:09, 24 May 2017 (UTC)
- @Mr. Stradivarius: That's odd! When I try it in the script sandbox, it gives an error –
Cannot read property 'call' of undefined
– but when I try it in the console, it logs "English" just like it does for you. — Eru·tuon 17:34, 24 May 2017 (UTC)- @Erutuon: That probably means that you are trying to use the library before it has finished loading. For example, if you copy all three lines above and execute them all at once in the browser console, you will get the same error (Firefox gives me "TypeError: mw.libs.lua is undefined"). However, if you try it a second time, then it will probably work, as your first request to load the library will probably have completed by then. If you want to make sure the library is loaded before your code is run, you can use
mw.loader.using
for mw.Api, and $.get to load mw.libs.lua. (mw.loader.load doesn't have any way of signalling when the load is complete, at least last time I checked, but $.get has a callback function that you can define for when the request completes.) — Mr. Stradivarius ♪ talk ♪ 09:02, 25 May 2017 (UTC)
- @Erutuon: That probably means that you are trying to use the library before it has finished loading. For example, if you copy all three lines above and execute them all at once in the browser console, you will get the same error (Firefox gives me "TypeError: mw.libs.lua is undefined"). However, if you try it a second time, then it will probably work, as your first request to load the library will probably have completed by then. If you want to make sure the library is loaded before your code is run, you can use
- @Mr. Stradivarius: That's odd! When I try it in the script sandbox, it gives an error –
- Give that a try and see if it works for you. Best — Mr. Stradivarius ♪ talk ♪ 14:09, 24 May 2017 (UTC)