claude当前对话字数统计

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

Od 07.08.2024.. Pogledajte najnovija verzija.

// ==UserScript==
// @name         claude当前对话字数统计
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  统计 claude ai当前对话字数 (包括粘贴、上传、article)。
// @author       Yearly
// @match        https://claude.ai/*
// @include      https://*.*claude.com/*
// @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();
})();