about summary refs log tree commit diff stats
path: root/src/pages/options/wikipedia.js
blob: f7ff15fa7c22986b6b40bae198b3d18c20969a53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js";
import commonHelper from "../../assets/javascripts/helpers/common.js";
import shared from "./shared.js";


const wikilessInstances = wikipediaHelper.redirects;

let wikipediaInstanceElement = document.getElementById("wikipedia-instance");
let disableWikipediaElement = document.getElementById("disable-wikipedia");
let wikilessRandomPoolElement = document.getElementById("wikiless-random-pool");

browser.storage.sync.get(
    [
        "wikipediaInstance",
        "disableWikipedia",
        "wikilessRandomPool"
    ],
    (result) => {
        wikipediaInstanceElement.value = result.wikipediaInstance || "";
        disableWikipediaElement.checked = !result.disableWikipedia;
        wikilessRandomPoolElement.value = (result.wikilessRandomPool || commonHelper.filterInstances(wikilessInstances)).join("\n")
        let id = "wikipedia-instance";
        let instances = wikilessInstances;
        shared.autocompletes.push({ id: id, instances: instances })
        shared.autocomplete(document.getElementById(id), instances);
    }
)

wikipediaInstanceElement.addEventListener("input", commonHelper.debounce(() => {
    if (wikipediaInstanceElement.checkValidity()) {
        browser.storage.sync.set({
            wikipediaInstance: shared.parseURL(wikipediaInstanceElement.value),
        });
    }
}, 500));

disableWikipediaElement.addEventListener("change", (event) => {
    browser.storage.sync.set({ disableWikipedia: !event.target.checked });
});

browser.storage.onChanged.addListener((changes) => {
    if ("wikilessRandomPool" in changes)
        wikilessRandomPoolElement.value = changes.wikilessRandomPool.newValue.join("\n");
})