在不创建隐藏页面元素的情况下构建 JavaScript 表单

Constructing a JavaScript form without creating hidden page elements

我正在尝试自动批量删除我的 Google 语音历史记录。到目前为止,我有:

// Code to be run at Legacy Google Voice: https://www.google.com/voice/b/0#history
function sleep(ms) { // courtesy: 
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function deleteMessages() {
    do {
        var msgs = document.getElementsByClassName('gc-message'),
            msgids = [];
        itemcount = msgs.length;
        for (var i = 0; i < itemcount; ++i) {
            msgids.push(msgs[i].id);
        };
        // the following based off 
        var form = document.createElement("form");
        form.setAttribute("method", "post");
        form.setAttribute("action", "https://www.google.com/voice/b/0/inbox/deleteMessages/");
        for (var i in msgids) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "messages");
            hiddenField.setAttribute("value", msgids[i]);
            form.appendChild(hiddenField);
        }
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "trash");
        hiddenField.setAttribute("value", 1);
        form.appendChild(hiddenField);
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "_rnr_se");
        hiddenField.setAttribute("value", _gcData['_rnr_se']);
        form.appendChild(hiddenField);
        document.body.appendChild(form);
        form.submit();
        await sleep(1000);
        document.getElementById('gc-inbox-next').click();
    } while (itemcount != 0);
}
deleteMessages();

如何在不实际创建隐藏页面元素的情况下执行此操作(例如使用 FormData?)?

此外,我的代码的另一个问题是我的浏览器将对 POST 请求的响应下载为文件。有没有办法让它忽略响应?

使用 FormData 对象更简洁,而且现在浏览器不会将 POST 响应下载为文件:

// Code to be run at Legacy Google Voice
//  Run at: https://www.google.com/voice/b/0#history
//          https://www.google.com/voice/b/0#spam
//          https://www.google.com/voice/b/0#trash
function sleep(ms) { // courtesy: 
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function deleteMessages() {
    do {
        // Get message IDs.
        var msgs = document.getElementsByClassName("gc-message"),
            msgids = [];
        itemcount = msgs.length;
        for (var i = 0; i < itemcount; ++i) {
            msgids.push(msgs[i].id);
        };
        // Construct form. courtesy: https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects#Creating_a_FormData_object_from_scratch
        try {
            var formData = new FormData();
            for (var i in msgids) {
                formData.append("messages", msgids[i]);
            }
            formData.append("_rnr_se", _gcData["_rnr_se"]);
            var request = new XMLHttpRequest();
            request.open("POST", "https://www.google.com/voice/b/0/inbox/deleteForeverMessages/");
            request.send(formData);
        } catch (a) {
            return;
        }
        await sleep(1000);
        try {
            document.getElementById("gc-inbox-next").click();
        } catch (b) {
            try {
                document.getElementById("gc-inbox-prev").click();
            } catch (c) {
                continue;
            }
        }
        await sleep(1000);
    } while (itemcount != 0);
}
deleteMessages();