Greasy Fork

Greasy Fork is available in English.

claude当前对话字数统计

统计 claude ai当前对话字数 (包括粘贴、上传、article)。

当前为 2024-08-07 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         claude当前对话字数统计
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  统计 claude ai当前对话字数 (包括粘贴、上传、article)。
// @author       Yearly
// @match        https://claude.ai/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=claude.ai
// @license      AGPL-v3.0
// @grant        none
// ==/UserScript==

(function() {
  var last_length = 0;
  function msg_counter_main() {
    let fieldset = document.querySelector("body > div.flex.min-h-screen.w-full fieldset");
    if (fieldset) {
      let mainScreen = document.querySelector("body > div.flex.min-h-screen.w-full > div > div.flex.h-screen") ;

      if(mainScreen){
        let tx_counts = 0, tx_length = 0;
        let rx_counts = 0, rx_length = 0;

        let txfile_cnts = 0, txfile_size = 0;
        let i = 0;
        let fiberKey = Object.keys(mainScreen).find(key => key.startsWith('__reactProps$'));
        if (fiberKey) {
          let fiber = mainScreen[fiberKey];
          let Msgs = (fiber.children[0]?.props?.messages);

          Msgs.forEach(function(msg){
            if(msg.sender == "human") {
              tx_counts +=1;
              tx_length += msg.text.length;
              for(i = 0; i < msg.attachments.length; i++) {
                tx_length += msg.attachments[i].file_size;
                txfile_cnts += 1;
                txfile_size += msg.attachments[i].file_size;;
              }

            } else if(msg.sender == "assistant") {
              rx_counts +=1;
              rx_length += msg.text.length;
            }

          });
        }

        let count_result = document.querySelector("#claude-msg-counter")
        if(!count_result) {
          count_result = document.createElement("pre");
          count_result.id = "claude-msg-counter";
          count_result.className="border-0.5 relative z-[5] text-text-200 border-accent-pro-100/20 bg-accent-pro-900 rounded-t-xl border-b-0"
          count_result.style = "font-size:12px; padding: 5px 7px 14px; margin:-12px 0";

          if (fieldset.querySelector("div.flex.md\\:px-2.flex-col-reverse > div") ){
            fieldset.querySelector("div.flex.md\\:px-2.flex-col-reverse > div").remove();
          }
          fieldset.querySelector("div.flex.md\\:px-2.flex-col-reverse").append(count_result);
        }

        let all_length = tx_length + rx_length ;
        let file_info=""
        if (txfile_cnts) file_info = ` (包含${txfile_cnts}次上传或粘贴,${txfile_size}字节)`

        if (last_length != all_length) {
          last_length = all_length;
          count_result.innerText = `【当前会话统计】已发出:${tx_counts}条,${tx_length}字${file_info}; 已回复:${rx_counts}条,${rx_length}字; 总计:${all_length}字。`;
        }
      }
    }
    setTimeout(msg_counter_main, 1300);
  }
  msg_counter_main();
})();