Word and Character Counts for Selected Text.

Display word and character count for all selected text using icons, including and excluding whitespace in character count

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Word and Character Counts for Selected Text.
// @namespace    https://github.com/Iquaridys
// @version      1.0.7
// @description  Display word and character count for all selected text using icons, including and excluding whitespace in character count
// @author       Iquaridys
// @license      GNU GPLv3
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Create a floating display element for the count
    const countDiv = document.createElement('div');
    Object.assign(countDiv.style, {
        position: 'fixed',
        bottom: '5px',
        right: '5px',
        backgroundColor: 'rgba(0, 0, 0, 0.7)',
        color: 'white',
        padding: '8px 12px',
        borderRadius: '5px',
        zIndex: '10000',
        display: 'none',
        fontSize: '14px',
        fontFamily: 'Arial, sans-serif',
        boxShadow: '0 2px 6px rgba(0, 0, 0, 0.3)',
        whiteSpace: 'nowrap',
    });
    document.body.appendChild(countDiv);

    // Count words, characters (with and without whitespace)
    function getTextCount(text) {
        const characterCountWithWhitespace = text.length; // Includes all characters, even spaces
        const characterCountWithoutWhitespace = text.replace(/\s+/g, '').length; // Removes spaces
        const wordCount = text.trim().split(/\s+/).filter(Boolean).length;
        return { characterCountWithWhitespace, characterCountWithoutWhitespace, wordCount };
    }

    // Event listener for text selection
    document.addEventListener('mouseup', () => {
        const selectedText = window.getSelection().toString();
        if (selectedText) {
            const { characterCountWithWhitespace, characterCountWithoutWhitespace, wordCount } = getTextCount(selectedText);

            // Use icons for word and character count
            const charIcon = '🅰️';
            const wordIcon = '🔤';

            countDiv.innerHTML = `${wordIcon} ${wordCount} | ${charIcon} ${characterCountWithoutWhitespace} ~ ${characterCountWithWhitespace} `;
            countDiv.style.display = 'block';
        } else {
            countDiv.style.display = 'none';
        }
    });

    // Hide the countDiv if the user clicks outside of selected text
    document.addEventListener('mousedown', () => {
        countDiv.style.display = 'none';
    });
})();