Topic on Project:Village Pump

Enable TemplateScripts on MediaWiki.org

9
Sophivorus (talkcontribs)

Hi! For some time now, I've been slowly developing c:Help:TemplateScripts, a method for extending the functionality of templates with JavaScript. For obvious reasons, template scripts must be located at the MediaWiki namespace (and have the "TemplateScript-" prefix, see the code below) so they require community consensus and an authorized user to get there. Template scripts are currently available at the Spanish Wikipedia, Commons, and the Spanish and English Wikiversity. I'd like to propose enabling them here at MediaWiki.org too, for the following reasons:

  • I'd like to turn the Synchronizer tool into a template script so that it loads automatically.
  • I'd like to migrate and centralize here at MediaWiki.org the existing template scripts and documentation, per being more adequate than Commons.
  • To attract more interest and developers, since MediaWiki.org is probably the most technically oriented of the Wikimedia wikis.

To enable TemplateScripts, the following code should be added to MediaWiki:Common.js and MediaWiki:Mobile.js:

// TemplateScripts are JavaScript scripts that extend the functionality of templates
var templatescripts = [];
$( '[data-templatescript]' ).each( function () {
	var script = $( this ).data( 'templatescript' );
	if ( script && !templatescripts.includes( script ) && /^[^&<>=%#]*\.js$/.test( script ) ) {
		templatescripts.push( script );
		script = encodeURIComponent( script );
		mw.loader.load( '/wiki/MediaWiki:TemplateScript-' + script + '?action=raw&ctype=text/javascript' );
	}
} );

Thoughts? Support? Objections? Cheers! Sophivorus (talk) 01:27, 12 July 2023 (UTC)

Tacsipacsi (talkcontribs)

I’d like to rather have an official solution, e.g. phab:T241524. I’m not strongly opposed to using this script in the meantime, but I have the feeling that the more temporary solutions we have, the less likely is that someone takes the time and effort to create an official one.

By the way, I couldn’t find traces of this on Commons except for the help page, are you sure it works there?

Sophivorus (talkcontribs)

Hi! You're right, it's not enabled in Commons (I think at some point it was). As to an official solution, I'd love to see one too! However, that may still require several years (or never come to be, like with global modules and now graphs). Also, by the looks of it, migrating from TemplateScripts to an official solution with a parser function that loads gadgets (as suggested by phab:T241524) seems rather easy. Finally, enabling and developing TemplateScripts now may actually help an official solution come about, by building a small community of involved developers and giving WMF developers some real world examples of its potential. Cheers!

Tacsipacsi (talkcontribs)

If you want to pave the path for the official solution (and also in order to make template scripts more efficient by taking advantage of minifying), it’d be helpful if TemplateScript was also based on gadgets rather than random JS pages:

$( '[data-templatescript]' ).each( function () {
    var script = $( this ).data( 'templatescript' );
    if ( script ) {
        mw.loader.load( 'ext.gadget.templatescript.' + script );
    }
} );

Also, to make it compatible with VisualEditor, live preview etc., it should use the wikipage.content hook:

mw.hook( 'wikipage.content' ).add( function ( $content ) {
    $content.find( '[data-templatescript]' ).each( function () {
        var script = $( this ).data( 'templatescript' );
        if ( script ) {
            mw.loader.load( 'ext.gadget.templatescript.' + script );
        }
    } );
} );

(This doesn’t unload no longer necessary template scripts, but at least loads those that become necessary after the initial page load.)

Sophivorus (talkcontribs)

Excellent ideas! I just implemented them at the Spanish Wikiversity to test them out, see es:v:Special:Gadgets, es:v:MediaWiki:Common.js and the [add objection] buttons at es:v:Wikidebate/Cannabis for the end result. I'll implement them at the English Wikiversity and Spanish Wikipedia soon, if no issues, concerns or further changes follow. These are exactly the kind of ideas for which I think bringing TemplateScripts to MediaWiki.org is the way to go.

Sophivorus (talkcontribs)
Pppery (talkcontribs)

Not involved enough here to have an opinion, so if nobody else objects then feel free to go ahead.

Ciencia Al Poder (talkcontribs)

No objections.

I'd prefer using tracking categories for those templates. However, category information in JavaScript is missing from Minerva due to phab:T206337, and it's going to be removed from core too (phab:T206250), which means we're left with using inefficient DOM searches...

Sophivorus (talkcontribs)

Done, see MediaWiki:Common.js, MediaWiki:Mobile.js and Synchronizer!

As to the idea suggested by Tacsipacsi, I implemented it at the Spanish Wikiversity but found it a bit cumbersome, since it requires the addition of each script to MediaWiki:Gadgets-definition, an extra prefix to each script (MediaWiki:Gadget-TemplateScript-Example.js) and an extra description page per script (MediaWiki:Gadget-TemplateScript-Example). Furthermore, it makes explaining the whole idea more complicated: "TemplateScripts are gadgets that can be loaded from templates"? Perhaps they should be renamed to TemplateGadgets then? Despite these difficulties, Tacsipacsi's idea may still be the way forward, but I'd like to hear some thoughts on these difficulties before fully committing. Cheers!

Reply to "Enable TemplateScripts on MediaWiki.org"