diff --git a/index.html b/index.html index 6d9d2dc..afab97d 100644 --- a/index.html +++ b/index.html @@ -40,7 +40,7 @@
- +
@@ -183,7 +183,7 @@ * Wraps around the MediawikiJS library. * * @property baseUrl {string} the origin of the wiki's API - * @property apiPath {string} the path relative to the wiki's API + * @property apiPath {string} the path relative to the wiki's API; starts with a `/` */ class MediaWiki { /** @@ -206,7 +206,7 @@ * @return {Promise} the API's response */ request(params) { - console.debug(`Requesting from ${this.baseUrl}/${this.apiPath} with param`, params); + console.debug(`Requesting from ${this.baseUrl}${this.apiPath} with params`, params); return new Promise(resolve => this._mwjs.send(params, it => resolve(it))); } @@ -234,9 +234,19 @@ } /** - * Requests all interwiki abbreviations available on this wiki. + * Returns this wiki's general information. * - * @return {Promise} a mapping from + * @return {Object} this wiki's general information + */ + getGeneralInfo() { + return this.request({action: "query", meta: "siteinfo", siprop: "general"}) + .then(response => response.query.general); + } + + /** + * Requests this wiki's interwiki map. + * + * @return {Promise} this wiki's interwiki map */ getIwMap() { return this @@ -270,13 +280,16 @@ /** * Initializes this `MediaWikiManager`. * - * @param baseLang {string} the language for the `MediaWiki` that is used as a starting point * @param baseMw {MediaWiki} the `MediaWiki` that is used as a starting point * @return {MediaWikiManager} this `MediaWikiManager` */ - async init(baseLang, baseMw) { + async init(baseMw) { + const general = await baseMw.getGeneralInfo(); + + this.articlePath = "" + general.articlepath; this.apiPath = baseMw.apiPath; - this.mws[baseLang] = baseMw; + + this.mws[general.lang] = baseMw; await this._importIwMap(baseMw); return this; @@ -298,7 +311,7 @@ return undefined; const url = this._iwMap.getUrl(lang); - this.mws[lang] = new MediaWiki(url.slice(0, -7) + "/" + this.apiPath); + this.mws[lang] = new MediaWiki(url.slice(0, -this.articlePath.length) + this.apiPath); await this._importIwMap(this.mws[lang]); return this.mws[lang]; @@ -370,14 +383,21 @@ // noinspection JSUnresolvedFunction Defined in `common.js` doAfterLoad(async () => { - const url = "https://fallout.fandom.com/api.php"; - const mw = new MediaWiki(url); - const mwm = await new MediaWikiManager().init("en", mw); - + const urlInput = $("#url"); const pageInput = $("#page"); const checkButton = $("#check"); - const submit = () => { + let previousUrl = undefined; + let mwm = undefined; + + const submit = async () => { + if (urlInput.value !== previousUrl) { + console.debug("Creating new MWM for url", urlInput.value); + const mw = new MediaWiki(urlInput.value); + mwm = await new MediaWikiManager().init(mw); + previousUrl = urlInput.value; + } + InterlangNetwork.discoverNetwork(mwm, new InterlangLink("en", pageInput.value)) .then(it => console.log(it)); };