Split Long Paragraphs

Splits long paragraphs at the period nearest to the split point.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Split Long Paragraphs
// @namespace    http://tampermonkey.net/
// @version      3
// @description  Splits long paragraphs at the period nearest to the split point.
// @match        *://ranobes.top/*
// @match        *://ranobes.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ranobes.top
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function ellipsize(s) {
        return s.substring(0, 64).trim().replace(/[^\w\s\d]+$/, "") + "...";
    }

    function log(text) {
        console.log(`Split Long Paragraphs: ${text}`);
    }

    const MAX_WORD_COUNT = 80;
    const section = document.querySelector(".story #arrticle");

    if (!section) {
        return;
    }

    const nodes = Array.from(section.querySelectorAll('p'));

    section.childNodes.forEach(child => {
        if (child.nodeType === Node.TEXT_NODE) {
            nodes.push(child);
        }
    })

    for (let i = 0; i < nodes.length; i++) {
        const node = nodes[i];
        const text = node.textContent.trim();
        const words = text.split(/\s+/);

        // Max word count before splitting
        if (words.length > MAX_WORD_COUNT + Math.floor(MAX_WORD_COUNT / 2)) {
            // Find closest split point to MAX_WORD_COUNT at a period
            const sentences = text.split(/(?<=[^\.]\.)\s+/);
            let sentenceIndex = 0;
            let wordCount = 0;

            for (const sentence of sentences) {
                wordCount += sentence.split(/\s+/).length;
                // log(`${ellipsize(sentence)} totalWordCount:${wordCount}`);

                if (wordCount >= MAX_WORD_COUNT) {
                    break;
                }

                sentenceIndex += 1;
            }

            if (wordCount !== 0) {
                log(`Splitting... \`${ellipsize(text)}\` wordCount:${words.length} sentenceIndex:${sentenceIndex} totalSentences:${sentences.length}`);

                node.textContent = '';

                let p = document.createElement('p');
                p.textContent = sentences.slice(0, sentenceIndex + 1).join(" ").trim();
                node.parentNode.insertBefore(p, node);

                p = document.createElement('p');
                p.textContent = sentences.slice(sentenceIndex + 1).join(" ").trim();
                node.parentNode.insertBefore(p, node);

                nodes.push(p);
                node.remove();
            }
        }
    }
})();