~glacambre/firenvim

ref: 4907b8d3554aae7f7f2c298a73f7f512f0ead2ed firenvim/src/utils/configuration.ts -rw-r--r-- 1.4 KiB
4907b8d3glacambre Improve testsuite 2 years ago
                                                                                
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 * as browser from "webextension-polyfill"; //lgtm [js/unused-local-variable]

export interface ISiteConfig {
    selector: string;
    priority: number;
    takeover: "always" | "once" | "empty" | "nonempty";
    cmdline: "neovim" | "firenvim";
}

let conf: {[key: string]: ISiteConfig} = {};

export const confReady = new Promise(resolve => {
    browser.storage.local.get().then((obj: any) => {
        conf = obj;
        resolve(true);
    });
});

browser.storage.onChanged.addListener((changes: any) => {
    Object
        .entries(changes)
        .forEach(([key, value]: [string, any]) => conf[key] = value.newValue);
});

export function getConf() {
    return getConfForUrl(document.location.href);
}

export function getConfForUrl(url: string) {
    const localSettings = conf.localSettings;
    function or1(val: number) {
        if (val === undefined) {
            return 1;
        }
        return val;
    }
    if (localSettings === undefined) {
        throw new Error("Error: your settings are undefined. Try reloading the page. If this error persists, try the troubleshooting guide: https://github.com/glacambre/firenvim/blob/master/TROUBLESHOOTING.md");
    }
    return Array.from(Object.entries(localSettings))
        .filter(([pat, sel]) => (new RegExp(pat)).test(url))
        .sort((e1, e2) => (or1(e1[1].priority) - or1(e2[1].priority)))
        .reduce((acc, [_, cur]) => Object.assign(acc, cur), {});
}