[[Template:catfix]] gadget improvements
I made the change but it seems like it only works sometimes. Other times I get the error "mw.Title is not a constructor".
You need to declare a dependency: use mw.loader.using(['mediawiki.Title'], function () { /* ... */ })
. Use it as late as possible, but not later.
Also, too much Python? In catch(e)
, the e
is a variable holding the error. For checking the class, you use e instanceof SyntaxError
or similar. Though it tends not to be very useful. (mw.Title
can throw when given invalid titles, so you better check for errors, if only to swallow them.)
Also, use A.substr(0, B.length) === B
instead of A.indexOf(B) === 0
. The latter is quite wasteful. (ECMAScript 6 has A.startsWith(B)
, but it is not supported everywhere yet, so better not to rely on it.) Similarly, this.textContent || this.innerText
is superior to $(this).text()
.
Now excuse me, I need to get back to being retired.
I made the changes you suggested, but I'm not sure I understand how declaring dependencies works. Am I supposed to wrap the whole jQuery(document).ready(function($){ ... }
in yet another function? Like this?
mw.loader.using(['mediawiki.Title'], function () { jQuery(document).ready(function($){ ... } }
I guess I just don't know enough about JS stuff to understand what I am doing. I understand the language, but not the framework.
Exactly. Though I would do it like:
if (mw.config.get('wgNamespaceNumber') === 14) // or mw.config.get('wgNamespaceIds').category for extra readability
jQuery(document).ready(function () {
var wrapper;
if (!(wrapper = document.getElementById("catfix")))
return;
mw.loader.using(['mediawiki.Title'], function () {
// ...
});
});