Word/Character Count for Selected Text 字数统计器

Display word count for English or character count for Chinese in the selected text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Word/Character Count for Selected Text 字数统计器
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Display word count for English or character count for Chinese in the selected text
// @author       Welcome21984
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建显示字数的元素
    var countDiv = document.createElement('div');
    countDiv.style.position = 'fixed';
    countDiv.style.bottom = '20px';
    countDiv.style.right = '20px';
    countDiv.style.backgroundColor = 'rgba(0,0,0,0.5)';
    countDiv.style.color = 'white';
    countDiv.style.padding = '5px';
    countDiv.style.borderRadius = '5px';
    countDiv.style.zIndex = '1000';
    countDiv.style.display = 'none';
    document.body.appendChild(countDiv);

    // 检查是否包含中文字符
    function containsChinese(text) {
        return /[\u4e00-\u9fa5]/.test(text);
    }

    // 获取字数
    function getWordCount(text) {
        if (containsChinese(text)) {
            // 中文字符计数
            return text.length;
        } else {
            // 英文单词计数
            return text.trim().split(/\s+/).length;
        }
    }

    document.addEventListener('mouseup', function() {
        var selectedText = window.getSelection().toString();
        if (selectedText.length > 0) {
            var count = getWordCount(selectedText);
            countDiv.textContent = containsChinese(selectedText) ? "Character count: " + count : "Word count: " + count;
            countDiv.style.display = 'block';
        } else {
            countDiv.style.display = 'none';
        }
    });
})();