Delete Kimi Messages

一键删除 Kimi 聊天记录

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Delete Kimi Messages
// @namespace    http://github.com/dbk6028
// @version      0.2
// @description  一键删除 Kimi 聊天记录
// @author       CBK
// @license     GPLv3
// @match        https://kimi.moonshot.cn/*
// @icon         https://statics.moonshot.cn/kimi-chat/favicon.ico
// @grant        none
// ==/UserScript==



let timer = setInterval(() => {
    if (document.querySelector('[class="historyModal___qTQKs"]') !== null) {

        listen_history_page();
        clearInterval(timer);
    }
}, 1000);


function listen_history_page() {
    const observer = new MutationObserver((mutations) => {

        add_delete_button();
    });

    let history_page = document.querySelector('[class="historyModal___qTQKs"]');
    is_exist(history_page);

    observer.observe(history_page, { attributes: true });
}

function add_delete_button() {
    const title = document.querySelector('[class="historyTitle___F_iam"]');
    is_exist(title);
    const button = document.createElement('button');
    button.innerHTML = '删除所有历史会话';
    title.appendChild(button);

    button.addEventListener('click', function () {



        const __tea_cache_tokens_20001731 = localStorage.getItem('__tea_cache_tokens_20001731');
        const __tea_cache_tokens_20001731_o = JSON.parse(__tea_cache_tokens_20001731);

        let headers = {
            'Content-Type': 'application/json',
            'X-MSH-Device-ID': __tea_cache_tokens_20001731_o.web_id,
            'Authorization': 'Bearer ' + localStorage.getItem('access_token'),
            'X-MSH-Platform': 'web',
            'R-Timezone': 'Asia/Shanghai',
            'X-Traffic-Id': __tea_cache_tokens_20001731_o.user_unique_id,
            'Priority': 'u=4',
        };
        logger(JSON.stringify(headers));
        fetch('https://kimi.moonshot.cn/api/chat/list', {
            method: 'POST',
            headers: headers,
            body: JSON.stringify({ offset: 0, size: 30 })
        })
            .then(response => response.json())
            .then(data => {




                let chats = data.items;
                chats.forEach(chat => {


                    headers.Priority = 'u=0';
                    fetch(`https://kimi.moonshot.cn/api/chat/${chat.id}`, {
                        method: 'DELETE',
                        headers: headers
                    }).then(response => {
                        if (response.ok) {
                            logger(`${chat.id} 删除成功`);
                        } else {
                            logger(`${chat.id} 删除失败`);
                        }
                    });
                });

            });
    });

    button.style.backgroundColor = '#007bff';
    button.style.color = '#fff';
    button.style.border = 'none';
    button.style.padding = '10px 20px';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
}


function logger(message) {
    console.log(`[${new Date().toLocaleString()}] ${message}`);
}

function is_exist(elem) {
    if (elem) {
        logger(`${elem} 存在`);
    } else {
        logger(`元素不存在`);
    }
}