Topic on Extension talk:Widgets

Widgets loaded before mw.hook is available

3
PetraMagna (talkcontribs)

I ported some functional widgets from a different wiki running MW 1.31 and Widgets v1.3.1 to mine, which is running MW 1.38 and Widgets v1.4.1. However, some of these ported widgets are not working. In particular, I see errors about mw.hook and mw.loader.using not being a function.

I tried to log them in the console as the widget loads, mw shows up as an object with all the expected properties, but mw.hook and mw.loader.using are both undefined.

A simple script to reproduce the issue is as follows:

window.RLQ.push(async () => {
    console.log(mw); // this shows up as a regular object
    console.log(mw.loader.using); // this shows up as undefined
    console.log(mw.hook); // this shows up as undefined
});

I typed the names of all these packages in the console after the web page loaded. Everything shows up normally, implying that the widget is perhaps loaded before core mw packages are available.

I feel like I'm missing something very obvious, but I don't know much about javascript and cannot find any migration guides/tutorials that may explain this phenomenon. Everything I saw suggested that mw.hook and mw.loader.using are loaded before everything else and that I don't need to do anything special to use them. Any help is appreciated. Thank you!

Cavila (talkcontribs)

Hi, I honestly don't know but you're relying on a number of ResourceLoader's core modules (mw.loader.using, etc.). As far as I know, the Widgets extension has no support for them out of the box, so I wonder how that issue was solved on the older wiki.

One hack you could try, though it really is a stab in the dark, is to edit extension.json and load the required modules as dependencies, which may or may not also include jQuery.

Another route you might consider is move your JavaScript to MediaWiki:Common.js instead, which comes with built-in support for ResourceLoader although you will probably have to rewrite some of your code (or have it rewritten).

PetraMagna (talkcontribs)

@Cavila Thanks for the reply! I saw in another thread called "JQuery access" that upgrading from MW 1.31 to 1.34 breaks jquery, so the issues with mw.hook and mw.loader.using might have a similar cause.

I thought extension.json configures the dependencies of an extension, so it shouldn't affect the order in which js scripts are loaded?

Reply to "Widgets loaded before mw.hook is available"