AO3 Word Count Script

Adds word counts to chapter links on AO3 Chapter Index pages.

18.03.2023 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name            AO3 Word Count Script
// @name:ru         Скрипт подсчета слов на AO3
// @version         2
// @description     Adds word counts to chapter links on AO3 Chapter Index pages.
// @description:ru  Добавляет количество слов к ссылкам на главы на страницах индекса глав AO3.
// @author          Anton Dumov
// @license         MIT
// @match           https://archiveofourown.org/*/navigate
// @grant           none
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    const wordCountRegex = /\s+/g;
    const cacheKeyPrefix = "ao3-word-count-cache-";
    const cacheDurationMs = 24 * 60 * 60 * 1000; // 24 hours in milliseconds

    const getCachedWordCount = link => {
        const cacheKey = cacheKeyPrefix + link.href;
        const cachedValue = localStorage.getItem(cacheKey);
        if (cachedValue) {
            const { timestamp, wordCount } = JSON.parse(cachedValue);
            if (Date.now() - timestamp < cacheDurationMs) {
                return wordCount;
            } else {
                localStorage.removeItem(cacheKey);
            }
        }
        return null;
    };

    const setCachedWordCount = (link, wordCount) => {
        const cacheKey = cacheKeyPrefix + link.href;
        const cacheValue = JSON.stringify({ timestamp: Date.now(), wordCount });
        localStorage.setItem(cacheKey, cacheValue);
    };

    const chapterLinks = document.querySelectorAll("ol.chapter.index.group li a");

    chapterLinks.forEach(link => {
        const cachedWordCount = getCachedWordCount(link);
        if (cachedWordCount) {
            const spanElement = link.parentElement.querySelector('span.datetime');
            const wordCountElement = document.createElement("span");
            wordCountElement.textContent = ` (${cachedWordCount} words)`;
            spanElement.parentNode.insertBefore(wordCountElement, spanElement.nextSibling);
        } else {
            fetch(link.href).then(response => response.text())
                .then(text => {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(text, "text/html");
                    const article = doc.querySelector("div[role=article]");
                    const wordCount = article ? article.textContent.trim().split(wordCountRegex).length : 0;
                    setCachedWordCount(link, wordCount);

                    const spanElement = link.parentElement.querySelector('span.datetime');
                    const wordCountElement = document.createElement("span");
                    wordCountElement.textContent = ` (${wordCount} words)`;
                    spanElement.parentNode.insertBefore(wordCountElement, spanElement.nextSibling);
                })
                .catch(error => console.log(error));
        }
    });

})();