gilbert189 @gilbert189
{ // in case something uses jQuery for some reason
    let $ = function (name) {
        let result = document.createElement(name);
        result.set = function (attr, value) {
            result.setAttribute(attr, value);
            return result;
        };
        result.setAttr = function(attr, value) {
            result[attr] = value;
            return result;
        }
        result.add = function (...elements) {
            for (let elm of elements) {
                result.append(elm);
            }
            return result;
        };
        result.text = function (text) {
            result.innerText = text;
            return result;
        };
        return result;
    }
    // add description lists
    let dl = $("dl").add(
        $("dt").add(
            $("label")
            .set("for", "recallChatSetting")
            .text("Recalling chat options:"),
        ),
        $("dd").add(
            $("button").setAttr("onclick", function deleteAll () {
                if (!confirm("Delete all of the chat history?")) return; // negative, do not delete everything
                localStorage.setItem(PREFIX + "messages", "{}");
                window.location.reload();
            }).text("Clear all history"),
            $("button").setAttr("onclick", function () {
                let amount = prompt("How many topmost messages should I keep?", "100");
                if (amount === null) return; // whoops, accidentally pressed it
                amount = Number(amount);
                let messageIDs = Object.keys(messages).sort((a, b) => a - b).slice(-amount);
                let newMessages = {};
                for (let msgID in messageIDs) {
                    newMessages[msgID] = messages[msgID];
                };
                localStorage.setItem(PREFIX + "messages", JSON.stringify(newMessages));
                window.location.reload();
            }).text("Clear some history"),
        )
    );
    document.getElementById("settingsList").append(dl);
}

this is on my userscript right now

I wish this was easier

Sep 15, 2024, 6:09 PM
1

comments