User:Erutuon/scripts/GadgetsDefinitionParser.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

// Could use this in the gadgets-definition script to grab list of dependencies
// and then generate an explanation of how to copy the gadget to your userspace
// and make changes, with mw.loader.using(dependencies, function() { /* gadget text here */ });
// or mw.loader.using(dependencies, function() { importScript("the userspace page where you put your copy of the gadget"); });.
new mw.Api()
	.get({
		"action": "expandtemplates",
		"title": "MediaWiki:Gadgets-definition",
		"prop": "wikitext",
		"text": "{{MediaWiki:Gadgets-definition}}"
	})
	.then(obj => obj.expandtemplates.wikitext
		.split("\n")
		.filter(line => line.startsWith("*"))
		.map(line => {
			const splitTrim = (list, sep) => list.split(sep).map(s => s.trim());
			const [name, rest] = splitTrim(line.replace(/^\*\s*/, ""), "[");
			const [options, pages] = splitTrim(rest, "]");
			const optionObj = {};
			for (const option of splitTrim(options, "|")) {
				const keyValue = splitTrim(option, "=");
				const value = keyValue[1];
				if (value) {
					const key = keyValue[0];
					// See [[mw:Extension:Gadgets#Options]].
					// "type" and "supportsUrlLoad" have single values,
					// while others have an array with comma separators.
					optionObj[key] =
						key === "type" || key === "supportsUrlLoad" ?
						value :
						splitTrim(value);
				} else {
					optionObj[keyValue[0]] = true;
				}
			}
			return {
				name,
				options: optionObj,
				pages: pages.split("|").filter(page => page.trim() !== "").map(page => "MediaWiki:Gadget-" + page)
			};
		}))
		// .then(console.log).fail(console.error)
		;