User:Jack who built the house/getUrlFromInterwikiLink

  (source) returns a promise for a URL (absolute or relative) with interwiki prefixes expanded into URL schemas.


 * Works on all WMF wikis, understanding the relativity of interwiki links (e.g.,  on Wiktionaries means en.wiktionary.org,   on Wikipedias means en.wikipedia.org).
 * As compared to using the site's interwiki map, handles all WMF-internal prefixes locally, requesting the interwiki map only for WMF-external prefixes (e.g. ).
 * Handles most of the tricky cases the same way MediaWiki does.
 * The exception is namespace names coinciding with prefixes: due to local processing, the script has access only to local namespaces. As a result, it will handle such collisions correctly only for the current domain (e.g.  for en.wikipedia.org, like NPOV). For the rest of domains, it will assume that strings in lowercase that are present in the interwiki map are prefixes (unless the string concides with the current wiki prefix, like   on Commons – there is a namespace with that name!), while strings in other cases are namespaces (e.g. if   were an interwiki prefix,   would be treated as a prefix while   – as a namespace).
 * Due to this being a marginal case, I didn't implement the correct differentiation between namespaces and prefixes, but if there is a use case for it, it is possible to implement it.
 * Another exception is highly strange cases like  (doesn't turn into a link on en.wikipedia.org) vs   (does turn into a link on en.wikipedia.org).
 * If some prefix is added to the interwiki map and the script is not yet updated, the function will request the interwiki map. If there is some other change to the interwiki map for WMF-internal prefixes, the script will return the outdated data until it is updated. (The interwiki data can be updated by executing User:Jack who built the house/getInterwikiData.js in the browser console and copypasting the resulting value into the script.)
 * You can run the tests on the function's return value by executing User:Jack who built the house/getUrlFromInterwikiLink-tests.js in the browser console (make sure you're on en.wikipedia.org).

If you report a bug, ping.

Get an interwiki prefix for a hostname
  returns a promise for an interwiki prefix for, provided that the user is on.

Caveats:
 * It only supports WMF wikis currently (e.g., it won't return a prefix for translatewiki.net).
 * There are hostnames with multiple prefixes for different URLs: for example, Wikidata has not only  for entities, but also   for lexemes at  https://www.wikidata.org/w/index.php?search=$1&ns146=1 . This function is intended to be used for wiki pages or whatever is in under the   path.
 * Be sure to specify the canonical hostname, with  where needed and without   on mobile.

You can also use a synchronous version of this function after accepting it from a hook: