Lute: Word Count

Add word count functionality to Lute's New Book page

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Lute: Word Count
// @version      20240825
// @description  Add word count functionality to Lute's New Book page
// @match        http://localhost:500*/book/new
// @grant        none
// @namespace https://greasyfork.org/users/242246
// ==/UserScript==

(function() {
    'use strict';

    const button = document.createElement('button');
    button.textContent = 'Count Words';
    button.style.marginTop = '5px';
    button.style.padding = "0.1em 0.2em";
    button.style.fontSize = '0.8em';
    button.type = 'button';

    const countDisplay = document.createElement('span');
    countDisplay.style.marginLeft = '5px';
    countDisplay.style.fontSize = '0.9em';

    function countWords(e) {
        e.preventDefault();
        const text = document.getElementById('text').value;
        const wordCount = text.trim().split(/\s+/).filter(word => word.length > 0).length;
        countDisplay.textContent = `${wordCount}`;
    }

    button.addEventListener('click', countWords);

    const textarea = document.getElementById('text');
    textarea.parentNode.insertBefore(button, textarea.nextSibling);
    button.parentNode.insertBefore(countDisplay, button.nextSibling);
})();