$apiURL = "https://jle.xyz/user/api";

async function shortURL() {
    const urlField = document.getElementById("shortUrl");
    const [tab] = await browser.tabs.query({ currentWindow: true, active: true });
    const url = tab.url;
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = () => {
        if(xhr.readyState == 4){
            if(xhr.status == 200){
                const response = JSON.parse(xhr.responseText);
                urlField.value = response.url;
                urlField.focus();
                urlField.select();
            } else {
                alert("Error: " + xhr.status + "\n" + xhr.responseText);
            }
        }
    };
    xhr.open("POST", $apiURL, true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.send(JSON.stringify({ "long": url }));
}

shortURL();