Page MenuHomePhabricator

Greasemonkey scripts no longer function on WMF wikis
Closed, InvalidPublic

Description

Very simple greasemonkey scripts no longer function. As of about 24 hours ago http://tools.wmflabs.org/betacommand-dev/greasemonkey/wikipedia.nfcc.user.js was working and now it doesnt using firefox.

Event Timeline

Betacommand raised the priority of this task from to Unbreak Now!.
Betacommand updated the task description. (Show Details)
Betacommand subscribed.
matmarex subscribed.

Probably related to recent ResourceLoader work. (Also, I'm not sure if "Unbreak now!" priority is appropriate. It likely affects few users, as most use MediaWiki-provided user script functionality.)

I'm fairly sure you could work around this by making your script execute after the page is fully loaded (not just DOM-ready, but fully loaded). Not sure if we support hooking up your scripts earlier during the page loading…

Given that there was zero advanced warning about this, and that Im not sure how to compensate for the WMF breaking stuff again the Unbreak now fits. Until today this code worked without any issues, its not like the code is really complex, its very basic for ease, so if I need to start going thru hoops to compensate for WMF breaking stuff instructions should be provided.

Shouldn't you always attach this kind of code to the document's onload event handler? I'm surprised this even worked properly before.

Until today this code worked without any issues, its not like the code is really complex, its very basic for ease, so if I need to start going thru hoops to compensate for WMF breaking stuff instructions should be provided.

It worked until last week but only by accident. The method by which the callback at http://tools.wmflabs.org/betacommand-dev/greasemonkey/wikipedia.nfcc.user.js is registered is non-standard and certainly does not originate from any Wikimedia-provided documentation. For the record, the script at time of writing is:

window.addEventListener("load", Greasemonkey_main, false);
function Greasemonkey_main() {
  if (mw.config.get('wgNamespaceNumber') == 0)
    mw.util.addPortletLink('p-cactions', '//tools.wmflabs.org/..', 'Check files');
}

Using window.onload as a handler to call mw.util was never supported. You're supposed to use mw.loader.using to indicate a dependency on a module.

For example:

mw.loader.using(['mediawiki.util']).then(function () {
  if (mw.config.get('wgNamespaceNumber') == 0)
    mw.util.addPortletLink('p-cactions', '//tools.wmflabs.org/..', 'Check files');
});

And the script itself should be guarded with a scheduler for when ResourceLoader initialises. The way to enqueue something for ResourceLoader used to be as follows (until last week):

if ( window.mw ) {
  myScript();
}

This is now:

RLQ.push(function () {
  myScript();
});

However both of these are internal mechanisms. All ways of loading code on Wikipedia (and for MediaWiki in general) abstract this, you should never write the above yourself.

Since GreaseMonkey is an unofficial way of loading additional code you are at your own risk to maintain parity with these internal mechanisms. Using RLQ should make your code work again. And remember to keep track of dependencies (such as mediawiki.util) and declare them with mw.loader.using(), else the code may break again in the future. See https://www.mediawiki.org/wiki/RL/DM for a list of modules. It probably already failed in the past under certain race conditions for some users.

For a complete example, including guarding and scheduling for GreaseMonkey, I'd recommend something like this:

window.addEventListener('load', function () {
  RLQ.push(function () {
    mw.loader.using([
      'mediawiki.util'
    ]).then(GreasemonkeyWikiMain);
  });
}, false);

function GreasemonkeyWikiMain() {
    if (mw.config.get('wgNamespaceNumber') == 0)
      mw.util.addPortletLink('p-cactions', '//tools.wmflabs.org/..', 'Check files');
}
Aklapper lowered the priority of this task from Unbreak Now! to High.Aug 18 2015, 11:51 AM

Given the last comment I'm decreasing priority of this task.
It's unclear to me what should be fixed/changed on WMF wikis and why to keep this task open currently - could someone elaborate?

matmarex claimed this task.

This task appears to be Invalid, per Krinkle's comment. @Betacommand, you should be easily able to update your scripts using it for guidance.