Claude Usage Overlay

Displays Claude.ai session and weekly usage stats as a floating overlay while you chat.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Claude Usage Overlay
// @namespace    https://greasyfork.org/users/tac-tac-go
// @version      1.0.0
// @description  Displays Claude.ai session and weekly usage stats as a floating overlay while you chat.
// @author       tac-tac-go
// @match        https://claude.ai/*
// @grant        none
// @license      MIT
// @homepageURL  https://greasyfork.org/users/tac-tac-go
// @supportURL   https://greasyfork.org/users/tac-tac-go
// ==/UserScript==

(function () {
  'use strict';

  function loadUsageViaIframe() {
    return new Promise((resolve) => {
      document.getElementById('usage-iframe')?.remove();

      const iframe = document.createElement('iframe');
      iframe.id = 'usage-iframe';
      iframe.src = '/settings/usage';
      iframe.style.cssText = 'position:fixed;top:-9999px;left:-9999px;width:1px;height:1px;';
      document.body.appendChild(iframe);

      iframe.onload = () => {
        let attempts = 0;
        const timer = setInterval(() => {
          attempts++;
          const iDoc = iframe.contentDocument;
          const bars = iDoc?.querySelectorAll('[role="progressbar"]');

          if (bars?.length >= 1) {
            clearInterval(timer);
            iframe.remove();

            const sessionPct = bars[0]?.getAttribute('aria-valuenow') ?? '?';
            const weeklyPct  = bars[1]?.getAttribute('aria-valuenow') ?? '?';
            const allP = Array.from(iDoc.querySelectorAll('p'));
            const resetTexts = allP.filter(p => p.textContent.includes('にリセット'));

            resolve({
              sessionPct,
              weeklyPct,
              sessionReset: resetTexts[0]?.textContent.trim() ?? '',
              weeklyReset:  resetTexts[1]?.textContent.trim() ?? '',
            });
          }

          if (attempts > 20) {
            clearInterval(timer);
            iframe.remove();
            resolve(null);
          }
        }, 200);
      };
    });
  }

  async function updateUsage() {
    const el = document.getElementById('claude-usage-overlay');
    el.textContent = 'Updating...';

    const data = await loadUsageViaIframe();

    if (!data) {
      el.textContent = 'Failed to load';
      return;
    }

    el.innerHTML = `
      <div style="font-weight:bold;margin-bottom:6px">📊 Claude Usage</div>
      <div>🕐 Session: <b>${data.sessionPct}%</b></div>
      <div style="font-size:10px;color:#aaa;margin-bottom:4px">${data.sessionReset}</div>
      <div>📅 Weekly: <b>${data.weeklyPct}%</b></div>
      <div style="font-size:10px;color:#aaa">${data.weeklyReset}</div>
      <div style="font-size:10px;color:#555;margin-top:6px">Click to refresh</div>
    `;
  }

  const overlay = document.createElement('div');
  overlay.id = 'claude-usage-overlay';
  overlay.style.cssText = `
    position:fixed; bottom:80px; right:16px; z-index:9999;
    background:#1e1e1e; color:#e0e0e0; padding:10px 14px;
    border-radius:8px; font-size:12px; border:1px solid #444;
    min-width:160px; line-height:1.6; cursor:pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  `;
  overlay.textContent = 'Loading...';
  overlay.title = 'Click to refresh';
  overlay.onclick = updateUsage;
  document.body.appendChild(overlay);

  updateUsage();
  setInterval(updateUsage, 5 * 60 * 1000);
})();