Greasy Fork 还支持 简体中文。

OpenAI Token Counter for Selected Text

Automatically count tokens of selected text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         OpenAI Token Counter for Selected Text
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Automatically count tokens of selected text
// @author       ChatGPT 4
// @match        *://*/*
// @grant        none
// @require      https://unpkg.com/gpt-tokenizer/dist/cl100k_base.js
// ==/UserScript==

(function() {
    'use strict';

    // 创建弹窗
    const popup = document.createElement('div');
    popup.style.display = 'none';
    popup.style.position = 'fixed';
    popup.style.right = '20px';
    popup.style.bottom = '300px';
    popup.style.backgroundColor = '#333';
    popup.style.color = '#fff';
    popup.style.padding = '10px';
    popup.style.borderRadius = '5px';
    popup.style.zIndex = '1001';
    document.body.appendChild(popup);

    // 显示token计数结果的函数
    function showTokenCount(tokens) {
        popup.textContent = `Token count: ${tokens.length}`;
        popup.style.display = 'block';
    }

    // 计算并显示选中文本的token数量
    function countAndShowTokens() {
        const text = window.getSelection().toString();
        if (text) {
            const tokens = GPTTokenizer_cl100k_base.encode(text);
            showTokenCount(tokens);
            checkSelection(); // 检查文本是否仍被选中
        } else {
            popup.style.display = 'none'; // 如果没有选中文本,则不显示弹窗
        }
    }

    // 检查是否还有文本被选中
    function checkSelection() {
        const checkInterval = 500; // 每500ms检查一次
        const interval = setInterval(() => {
            const currentSelection = window.getSelection().toString();
            if (!currentSelection) {
                popup.style.display = 'none';
                clearInterval(interval); // 停止检查
            }
        }, checkInterval);
    }

    // 为文档添加事件监听器以检测文本选择
    document.addEventListener('selectionchange', function() {
        countAndShowTokens(); // 当文本被选中时调用该函数
    });
})();