Last active
May 16, 2024 09:29
-
-
Save francistm/252eff810e87ffbab79b48d74fb9a6ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Kimi 会话清空 | |
// @version 1.0.1 | |
// @description 一键删除全部对话记录 | |
// @author francis | |
// @homepage https://blog.francistm.com | |
// @match https://kimi.moonshot.cn/* | |
// @icon https://statics.moonshot.cn/kimi-chat/favicon.ico | |
// @updateURL https://gist.github.com/francistm/252eff810e87ffbab79b48d74fb9a6ec/raw/kimi_clear_all_histories.user.js | |
// @downloadURL https://gist.github.com/francistm/252eff810e87ffbab79b48d74fb9a6ec/raw/kimi_clear_all_histories.user.js | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const findNodeRecursive = function(root, classNamePrefix) { | |
let depth = 0; | |
let currentElem = root; | |
outer: while (depth < classNamePrefix.length) { | |
for (let i = 0; i < currentElem.children.length; i++) { | |
const child = currentElem.children[i]; | |
if (child.className.startsWith(classNamePrefix[depth])) { | |
depth += 1 | |
currentElem = child | |
continue outer; | |
} | |
} | |
return null; | |
} | |
return currentElem; | |
} | |
const addIconToSidebar = function() { | |
const mountRoot = document.getElementById("root"); | |
const observer = new MutationObserver(mutations => { | |
for(let i = 0; i < mutations.length; i++) { | |
const mutation = mutations[i]; | |
if (mutation.addedNodes.length === 0) { | |
continue; | |
} | |
const agentClassNamePrefixPath = ['mainContent', 'layoutNav', 'layoutNavMove', 'myAgent']; | |
const agentElem = findNodeRecursive(mutation.addedNodes[0], agentClassNamePrefixPath); | |
if (agentElem === null) { | |
console.debug("myAgent elem not found"); | |
continue; | |
} | |
const clearButtonElem = document.createElement("div"); | |
const clearButtonInnerElem = document.createElement("div"); | |
const clearButtonContentElem = document.createTextNode("清空") | |
clearButtonElem.appendChild(clearButtonInnerElem); | |
clearButtonInnerElem.appendChild(clearButtonContentElem); | |
clearButtonElem.style.width = '40px'; | |
clearButtonElem.style.height = '40px'; | |
clearButtonElem.style.borderRadius = '12px'; | |
clearButtonElem.style.marginBottom = '10px'; | |
clearButtonInnerElem.style.width = '40px'; | |
clearButtonInnerElem.style.height = '40px'; | |
clearButtonInnerElem.style.position = 'relative'; | |
clearButtonInnerElem.style.cursor = 'pointer'; | |
clearButtonInnerElem.style.lineHeight = '40px'; | |
clearButtonInnerElem.style.textAlign = 'center'; | |
agentElem.appendChild(clearButtonElem); | |
clearButtonElem.addEventListener("click", clearAllChats); | |
} | |
}); | |
observer.observe(mountRoot, { | |
childList: true | |
}); | |
} | |
const clearAllChats = async function(e) { | |
const accessToken = localStorage.getItem("access_token"); | |
if (typeof accessToken !== 'string') { | |
console.warn('no access token found, halt the script'); | |
return | |
} | |
const chatIdList = await fetch("https://kimi.moonshot.cn/api/chat/list", { | |
"method": "post", | |
"body": JSON.stringify({ | |
"kimiplus_id": "", | |
"offset": 0, | |
"size": 100 | |
}), | |
"headers": { | |
"Authorization": `Bearer ${accessToken}`, | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then(i => i.json()) | |
.then(i => i.items.map(j => j.id)); | |
if (chatIdList.length == 0) { | |
return; | |
} | |
for (let i = 0; i < chatIdList.length; i++) { | |
await fetch(`https://kimi.moonshot.cn/api/chat/${chatIdList[i]}`, { | |
method: "delete", | |
"headers": { | |
"Authorization": `Bearer ${accessToken}`, | |
"Content-Type": "application/json", | |
}, | |
}) | |
} | |
window.location = "https://kimi.moonshot.cn/" | |
} | |
addIconToSidebar(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment