Greasy Fork

Greasy Fork is available in English.

Claude - Clear chat history

Adds a clear history button

当前为 2025-02-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Claude - Clear chat history
// @description  Adds a clear history button
// @version      0.4
// @license MIT
// @match        https://claude.ai/new
// @icon         https://www.google.com/s2/favicons?sz=64&domain=claude.ai
// @run-at       document-end
// @grant        unsafeWindow
// @namespace http://greasyfork.icu/users/1286140
// ==/UserScript==

(() => {
  let org = "";

  function getCookie() {
    return new Promise((resolve, reject) => {
      const cookies = document.cookie.split(';');
      const lastActiveOrg = cookies.find(cookie => cookie.trim().startsWith('lastActiveOrg='));

      if (lastActiveOrg) {
        org = lastActiveOrg.split('=')[1].trim();
        console.log('Organization ID found:', org);
        resolve(org);
      } else {
        console.warn('lastActiveOrg cookie not found');
        reject("Cookie not found");
      }
    });
  }

  unsafeWindow.clearHistory = () => {
    if (!org) {
      getCookie().then(() => {
        if (!org) {
          console.error("Could not retrieve current organization id");
          alert("Error: Could not retrieve current organization id");
        } else {
          clearChats();
        }
      }).catch((error) => {
        console.error("Error retrieving cookie:", error);
        alert("Error retrieving cookie: " + error);
      });
    } else {
      clearChats();
    }
  };

  function clearChats() {
    console.log('Clearing chats for organization:', org);
    fetch(`https://claude.ai/api/organizations/${org}/chat_conversations`, {
      method: "GET",
      credentials: "include",
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return response.json();
      })
      .then((data) => {
        console.log('Chats retrieved:', data);
        return Promise.all(
          data.map((chat) =>
            fetch(
              `https://claude.ai/api/organizations/${org}/chat_conversations/${chat.uuid}`,
              {
                method: "DELETE",
                credentials: "include",
              }
            ).then((response) => {
              if (!response.ok) {
                throw new Error(`Failed to delete chat ${chat.uuid}. Status: ${response.status}`);
              }
              console.log(`Chat ${chat.uuid} deleted successfully`);
            })
          )
        );
      })
      .then(() => {
        console.log('All chats deleted successfully');
        alert('All chats deleted successfully');
        unsafeWindow.location.reload();
      })
      .catch((error) => {
        console.error('Error in clearChats:', error);
        alert('Error clearing chats: ' + error.message);
      });
  }

  window.addEventListener("load", () => {
    setTimeout(() => {
      const viewAllLinks = Array.from(document.getElementsByTagName('a'))
        .filter(a => a.textContent.trim() === 'View all');
      console.log('View all links found:', viewAllLinks.length);
      if (viewAllLinks.length > 0) {
        const viewAllLink = viewAllLinks[0];
        const button = document.createElement("a");
        button.textContent = "Clear History";
        button.href = "#";
        button.classList.add("text-text-300", "hover:text-text-200", "inline-flex", "items-center", "gap-1", "text-sm", "transition", "hover:underline");
        button.style.marginRight = "16px"; // Add margin to the right of the button
        button.addEventListener("click", (e) => {
          e.preventDefault();
          if (confirm('Clear all chats from history?')) {
            unsafeWindow.clearHistory();
          }
        });
        viewAllLink.parentNode.insertBefore(button, viewAllLink);
        console.log('Clear History button added');
      } else {
        console.warn('Could not find appropriate location to add Clear History button');
      }
    }, 2000); // 2000 milliseconds = 2 seconds
  });

  // Initial cookie retrieval
  getCookie().catch((error) => {
    console.error("Error retrieving initial cookie:", error);
  });
})();