Steps to replicate the issue:
wikipage.content is broadly used by user scripts so they can be called once the page is ready and again on subsequent updates to the page content. This usage is documented in various places including https://doc.wikimedia.org/mediawiki-core/master/js/Hooks.html which further states:
This includes the ready event on a page load (including post-edit loads) and when content has been previewed with LivePreview.
Unfortunately, there is a race condition that sometimes prevents registered handlers from receiving the ready event on a page load if another user script runs:
mw.hook( 'wikipage.content' ).fire( $content );
slightly before the affected user script registers with mw.hook. This can even happen if the script calling .fire() waits until it itself is triggered by the wikipage.content.
Here's how you can reproduce the issue:
- Open the JavaScript console and run:
mw.hook('wikipage.content').add(function($element) {
console.log('Element tag:', $element.prop('tagName'));
console.log('Element id:', $element.attr('id'));
});As the safemode environment doesn't run any JavaScript that fires the wikipage.content hook, the expected DIV for the page content is received:
Element tag: DIV Element id: mw-content-text
- Reload the page and run this instead:
// simulate a separate script modifying the document early and quickly
const $newParagraph = $('<p>This is a small paragraph added to the body.</p>').appendTo('body');
mw.hook('wikipage.content').fire($newParagraph);
// register our hook
mw.hook('wikipage.content').add(function($element) {
console.log('Element tag:', $element.prop('tagName'));
console.log('Element id:', $element.attr('id'));
});Now the result is this instead:
Element tag: P Element id: undefined
and the #mw-content-text hook is never sent.
What happens?:
The problem is that this means "nothing happens" for some user scripts whenever they don't receive the ready event via the #mw-content-text being fired. This issue is very easily reproduced if you have a relatively fast user script that runs .fire() early enough in your common.js. (Because mw.loader.load is asynchronous, reordering items in common.js won't necessarily avoid the issue too.)
I ended up adding a workaround to a user highlighter script that I've been developing because it was happening over 50% of the time on larger pages. The script now tracks whether it's the first time the hook has fired and the $content that is passed is simply ignored if it's the first time the hook has been called.
I suspect this issue is not seen more often because a lot of user scripts simply never run:
mw.hook( 'wikipage.content' ).fire( $content );
on smaller elements. A lot of people probably just reload the page if the issue is only intermittent for them.
What should have happened instead?:
One solution would be to make sure registered handlers always receive the page content (i.e., #mw-content-text) when they register.
(There is a potential less common use case of user scripts registering long after page ready and not necessarily caring about receiving #mw-content-text, but that might be better handled by adding an optional second parameter for .add() to indicate that the handler should only receive new events.)
Notes:
The reason this happens seems to be documented in the code.
* Handlers can be added and fired for arbitrary event names at any time. The same * event can be fired multiple times. The last run of an event is memorized * (similar to `$(document).ready` and `$.Deferred().done`). * This means if an event is fired, and a handler added afterwards, the added * function will be fired right away with the last given event data.
The problem is that behavior is not what is documented and it's not how user scripts actually use the wikipage.content hook.