Store API URL in cookie for re-use

Fixes #25.
This commit is contained in:
Florine W. Dekker 2020-04-16 10:58:05 +02:00
parent 2d05dec08d
commit 8c84d18773
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
5 changed files with 28 additions and 4 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -1,6 +1,6 @@
{
"name": "interlanguage-checker",
"version": "1.7.0",
"version": "1.8.0",
"description": "Check the consistency of MediaWiki interlanguage links in a simple overview.",
"author": "Felix W. Dekker",
"browser": "bundle.js",
@ -17,9 +17,11 @@
"dependencies": {
"fetch-jsonp": "^1.1.3",
"htm": "^3.0.3",
"js-cookie": "^2.2.1",
"preact": "^10.4.0"
},
"devDependencies": {
"@types/js-cookie": "^2.2.6",
"grunt": "^1.1.0",
"grunt-cli": "^1.3.2",
"grunt-contrib-clean": "^2.0.0",

View File

@ -79,7 +79,7 @@
API 
<i class="fa fa-question-circle-o" title="The URL to the wiki's api.php"></i>
</label>
<input id="url" type="url" value="https://fallout.fandom.com/api.php" />
<input id="url" type="url" autofocus />
<label for="article">
Article&nbsp;

View File

@ -31,6 +31,15 @@ export class ValidatableInput {
return this.input.value;
}
/**
* Sets the value of the underlying input element.
*
* @param value {string} the value to set
*/
setValue(value) {
this.input.value = value;
}
/**
* Validates the input.
*

View File

@ -1,4 +1,5 @@
import {html, render} from "htm/preact";
import Cookies from "js-cookie";
import {InterlangTable, MessageHandler, ValidatableInput} from "./DOM";
import {discoverNetwork, InterlangNetwork, MediaWiki, MediaWikiManager} from "./MediaWiki";
import {$, doAfterLoad} from "./Shared";
@ -31,6 +32,8 @@ doAfterLoad(async () => {
let mwm = undefined;
const submit = async () => {
Cookies.set("api-url", urlInput.getValue(), {expires: 10 * 365});
// Clean up
urlInput.showBlank();
articleInput.showBlank();
@ -95,8 +98,18 @@ doAfterLoad(async () => {
});
checkButton.addEventListener("click", () => submit());
// Read inputs from cookies
if (Cookies.get("api-url") !== undefined) {
urlInput.setValue(Cookies.get("api-url"));
articleInput.input.focus();
}
// Read inputs from URL
const currentParams = new URLSearchParams(window.location.search);
if (currentParams.has("api")) urlInput.input.value = currentParams.get("api");
if (currentParams.has("article")) articleInput.input.value = currentParams.get("article");
if (currentParams.has("api")) {
urlInput.setValue(currentParams.get("api"));
articleInput.input.focus();
}
if (currentParams.has("article")) articleInput.setValue(currentParams.get("article"));
if (currentParams.has("api") && currentParams.has("article")) await submit();
});