~glacambre/firenvim

ref: 6c5ec95986171814433de7a7396a5ab757f53381 firenvim/src/browserAction.ts -rw-r--r-- 1.7 KiB
6c5ec959glacambre package-lock.json: version bump Firenvim 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
45
46
47
48
49
import * as browser from "webextension-polyfill";

function displayMessages(func: "getError" | "getWarning", id: "errors" | "warnings") {
    function insertMessage(msg: any) {
        console.log(msg);
        document.getElementById(id).innerText = msg;
    }
    return browser.runtime.sendMessage({ funcName: [func] })
        .then(insertMessage)
        .catch(insertMessage);
}

function displayErrorsAndWarnings() {
    return Promise.all([displayMessages("getWarning", "warnings"), displayMessages("getError", "errors")]);
}

async function updateDisableButton() {
    const tabId = (await browser.runtime.sendMessage({
        args: {
            args: [{ active: true }],
            funcName: [ "browser", "tabs", "query" ],
        },
        funcName: ["exec"],
    }))[0].id;
    const disabled = (await browser.runtime.sendMessage({
        args: [tabId, "disabled"],
        funcName: ["getTabValueFor"],
    }));
    const button = document.getElementById("disableFirenvim");
    if (disabled === "true") {
        button.innerText = "Enable in this tab";
    } else {
        button.innerText = "Disable in this tab";
    }
}

addEventListener("DOMContentLoaded", () => {
    document.getElementById("reloadSettings").addEventListener("click", () => {
        browser.runtime.sendMessage( { funcName: ["updateSettings"] })
            .then(displayErrorsAndWarnings)
            .catch(displayErrorsAndWarnings);
    });
    document.getElementById("disableFirenvim").addEventListener("click", () => {
        browser.runtime.sendMessage( { funcName: ["toggleDisabled"] })
            .then(updateDisableButton);
    });
    displayErrorsAndWarnings();
    updateDisableButton();
});