Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Other] Title: Chatbox Emergency Backup Solution 标题:Chatbox 紧急备份方案 #1701

Open
alem0n opened this issue Dec 21, 2024 · 0 comments

Comments

@alem0n
Copy link

alem0n commented Dec 21, 2024

English Statement:
This script was written by me as a temporary solution to address an urgent issue. Please use it with caution.

中文声明:
这个脚本是我为解决突发问题临时编写的,请谨慎使用。

Normally, Chatbox provides official backup and restore options in the settings. However, sometimes intermittent bugs may prevent the official export files from working properly. In such cases, you can export all data by running the script below in the browser console of the current page. If the issue is caused by a bug in chat history parsing, you can delete the problematic chat history from the generated JSON file, clear the webpage data, and then re-import the JSON file to restore normal functionality.

正常情况下chatbox在设置中提供官方的备份与恢复。但是有时,会出现偶发性bug导致无法正常使用官方的导出文件,所以这里通过在当前页面的浏览器控制台运行下面脚本就可以导出所有数据。如果是因为聊天记录解析导致的bug,可以在生成的json文件就删除对应聊天记录,然后清空网页数据,重新导入json就可以恢复正常使用了。

image

// 打开数据库
const request = indexedDB.open("chatboxstore", 2);

request.onsuccess = function(event) {
  const db = event.target.result;


  const transaction = db.transaction(["keyvaluepairs"], "readonly");
  const objectStore = transaction.objectStore("keyvaluepairs");

  // 创建游标来遍历对象存储
  const cursorRequest = objectStore.openCursor();
  // 存储解析出的所有键值对
  const allKeyValuePairs = {};

  // 存储解析出的所有键对
  const allKeyPairs = [];
  cursorRequest.onsuccess = function(event) {
    const cursor = event.target.result;

    // 如果游标指向数据项
    if (cursor) {

      try {
        // 将键值对添加到对象中
        allKeyValuePairs[cursor.key] = JSON.parse(cursor.value);
        if(cursor.key === "chat-sessions"){
          allKeyPairs.push("conversations");
        }

        if(cursor.key === "settings"){
          allKeyPairs.push("setting");

        }

        if(cursor.key === "configs"){
          allKeyPairs.push("key");

        }

        if(cursor.key === "myCopilots"){
          allKeyPairs.push("copilot");

        }


      } catch (e) {
        console.error("Failed to parse JSON:", e);
      }

      // 继续遍历下一个项
      cursor.continue();

    } else {
      // 当遍历完成时,创建导出的 JSON 对象
      const exportedData = {
        ...allKeyValuePairs,  // 合并所有键值对
        "__exported_items": allKeyPairs,  // 添加导出项
        "__exported_at": new Date().toISOString()  // 添加导出时间
      };

      // 打印最终的 JSON 数据
      //console.log("Exported Data:", JSON.stringify(exportedData, null, 2));
      // 生成时间戳作为文件名的一部分
      const timestamp = new Date().toISOString().replace(/[:.-]/g, "_"); // 替换冒号和点号,避免文件名不合法
      const fileName = `chat_backup_${timestamp}.json`;
      // 可选:将 JSON 数据保存为文件
      const jsonBlob = new Blob([JSON.stringify(exportedData, null, 2)], { type: 'application/json' });
      const url = URL.createObjectURL(jsonBlob);
      const a = document.createElement('a');
      a.href = url;
      a.download = fileName;  // 指定文件名
      a.click();
      URL.revokeObjectURL(url);
    }
  };

  cursorRequest.onerror = function(event) {
    console.error("Error reading from object store:", event);
  };

};

request.onerror = function(event) {
    console.error("Error opening database:", event);
};
@alem0n alem0n changed the title [Other] Title: Chatbox Emergency Backup 标题:Chatbox 紧急备份 [Other] Title: Chatbox Emergency Backup Solution 标题:Chatbox 紧急备份方案 Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant