Topic on Project:Support desk

User script interdependencies

7
Martijn Hoekstra (talkcontribs)

I ask this question specifically in the context of the English Wikipedia, but I suppose it's equally valid for any MediaWiki installation. I originally asked these questions here, and copy them here in the hope of more support.

I'm starting to try and work with userscripts a little, and, probably due to my inexperience with both javascript and with mediawiki, I'm running in to a couple of questions. The questions all have to do with loading and dependencies.

  • I would like to use some functions/objects in multiple scripts. What is the suggested way to inject the dependencies into the executing script? There are the functions importScript, mw.loader.load and mw.loader.using(). I can't figure out how to inject the dependencies in to the executing script however, other than injecting them into the global namespace and picking them up from there, which can't be the intention. mw.loader.using takes a callback as an argument, but that callback doesn't have parameters for the dependencies. It returns a promise, but I can't find what the promise contains (it looks like nothing).
  • Scripts that actually do something other than providing functions tend to depend on the DOM, which may be built asynchronously. The mediawiki guidelines advice to use the mw.hook events rather than document.ready, which seems reasonable, but the three listed events there seem to be rather sparse. My specific usecase for now is adding a portlet link. I'd like it to be general, but I'd settle for it only working on Vector. On which event should I add that?
Ciencia Al Poder (talkcontribs)

I wasn't aware of the mw.hook thing, but for things like portlet links that are not in article content you can use document.ready events without problems.

Example code with dependencies:

$(function() {
	// Code that will run when the page has finished loading
	mw.loader.using(['mediawiki.api', 'jquery.ui.dialog']).done(function() {
		// Code that will run when all dependencies have been loaded and are available on the page
	});
});
Martijn Hoekstra (talkcontribs)

Well, that at least solves the document ready problem; it's not a problem. But I still don't get how I can depend on my own userscripts. For example, if I have User:Martijn Hoekstra/somedependency.js

  (function(){
    var myutilities = {
      do_my_thing: function(){
         // code that will do my thing
      }
    };
    //I want to expose myutilities to other scripts somehow now, preferably not through the global namespace but I don't know how
  })();

Then when I have User:Martijn Hoekstra/mydependingscript.js

  mw.loader.using(['some way to refer to User:Martijn Hoekstra/somedependency.js, what goes here?']).done(function(mydependency) {
    mydependency.do_my_thing();
  });

how do I get a hold of the dependency in User:Martijn Hoekstra/somedependency.js?

Ciencia Al Poder (talkcontribs)

You'll need to use $.getScript for that, passing the complete URL of the script (if it's a wiki page, with the &action=raw&ctype=text/javascript query string):

  $.getScript('/w/index.php?title=User:Martijn_Hoekstra/somedependency.js&action=raw&ctype=text%2Fjavascript').done(function() {
    mydependency.do_my_thing();
  });

Note that it doesn't allow cross-domain requests, so the script must be on the same wiki. Otherwise, there's no way to do that, except if you save your function in a sort of global variable and make the script attempt to read that variable to execute the function upon being loaded.

Martijn Hoekstra (talkcontribs)

Thank you for helping me further. Where does the variable mydependency come from in that example? Do I have to return it from the dependency? Or do I have to attach it to the window object? Also, wouldn't this load and execute the dependency every time it is depended upon?

121.214.190.213 (talkcontribs)

There's no actual dependency handling for user scripts. If you want that you'll need to write an extension for it, or wait for gadgets 3.0 (don't hold your breath).

Martijn Hoekstra (talkcontribs)

Oh, that's a shame. I'll see if I can update the documentation to reflect that.

Reply to "User script interdependencies"