Jump to content

Template:Load user script/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 18:44, 4 December 2023 (importScript() vs mw.loader.load(): new section). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This template is used to install user scripts that reside on the English Wikipedia. It is to be used primarily on Special:MyPage/common.js or Special:MyPage/skin.js. It adds the necessary mw.loader.load line along with a backlink.

Usage

In order to install a user script for your Wikipedia account, add the following line to Special:MyPage/common.js or Special:MyPage/skin.js:

{{subst:lusc|script_path}}

  • Replace script_path with the full .js page name of the user script to be installed.
  • The template must be substituted (subst:), or else it won't work.
  • Bypass your cache after saving the page.

Example

{{subst:lusc|User:Example/script.js}}

  • Produces: mw.loader.load( '/w/index.php?title=User:Example/script.js&action=raw&ctype=text/javascript' ); // Backlink: [[User:Example/script.js]]

{{subst:lusc|User:Example/script.js|User:Example/script}}

  • Produces: mw.loader.load( '/w/index.php?title=User:Example/script.js&action=raw&ctype=text/javascript' ); // Backlink: [[User:Example/script]]

importScript() vs mw.loader.load()

{{Install user script}} uses importScript(), and {{Load user script}} uses mw.loader.load().

importScript():

  • Is less verbose, only needing the string of the the page you're loading.
  • Is more restrictive, only allowing the loading of Wikipedia pages in the user or MediaWiki namespaces.
  • Can load from other Wikimedia wikis, using a prefix such as "c" for "commons". For example, importScript('c:User:YourName/test.js');

mw.loader.load():

  • Is more verbose, needing a long URL string. Note that the URL string has to be correctly escaped.
  • Is less restrictive. Will load any URL from anywhere. So for example is good for loading from a local dev environment such as localhost.
  • Can load from other Wikimedia wikis by adjusting the URL.

See also