Edit Name Sfacg Chat Novel

Chỉnh sửa name cho truyện dạng chat của nguồn Sfacg

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Edit Name Sfacg Chat Novel
// @namespace    Hexros Raymond
// @version      1.1.3
// @description  Chỉnh sửa name cho truyện dạng chat của nguồn Sfacg
// @author       Hexros Raymond
// @license      MPL 2.0
// @match        *://*/truyen/sfacg/1/*/*/
// @grant        none
// ==/UserScript==

(function () {
    "use strict";

    function createButton(text, onClickFunction, bottomOffset) {
        var button = document.createElement("button");
        button.className = "new-btn";
        button.textContent = text;

        let styles = {
            position: "fixed",
            bottom: bottomOffset + "px",
            right: "13px",
            fontSize: "14px",
            outline: "none",
            borderRadius: "100%",
            height: "50px",
            width: "50px",
            margin: "2px",
        };

        Object.assign(button.style, styles);
        button.addEventListener("click", onClickFunction);
        document.body.appendChild(button);
        return button;
    }

    function runName() {
        document.querySelector("button[onclick='showNS()']").click();
        let copyText = document.querySelector("#namewd").value;
        document.querySelector("button[onclick='hideNS()']").click();

        const names = [];
        copyText = copyText.split("\n");
        copyText.forEach((text) => {
            if (text.startsWith("$")) {
                text = text.replace("$", "");
                text = text.split("=");
                names.push({
                    original: text[0],
                    translated: text[1],
                });
            }
        });

        const chat_novel = document.querySelectorAll(".chatnovel > div");

        for (let k = names.length - 1; k >= 0; k--) {
            chat_novel.forEach((div) => {
                const textElements = div.querySelectorAll("i");

                for (let i = 0; i < textElements.length; i++) {
                    let combinedText = "";
                    let elementsToReplace = [];

                    for (let j = i; j < textElements.length; j++) {
                        if (
                            i === j &&
                            !names[k].original.startsWith(
                                textElements[j].getAttribute("t")
                            )
                        ) {
                            break;
                        }
                        combinedText += textElements[j].getAttribute("t");
                        elementsToReplace.push(textElements[j]);
                        if (combinedText === names[k].original) {
                            const newElement = document.createElement("i");
                            let h = "";
                            for (let k = 0; k < elementsToReplace.length; k++) {
                                h +=
                                    elementsToReplace[k].getAttribute("h") +
                                    " ";
                            }
                            h = h.trim();
                            newElement.setAttribute("h", h);
                            newElement.setAttribute("t", names[k].original);
                            newElement.setAttribute("v", names[k].translated);
                            newElement.setAttribute(
                                "id",
                                elementsToReplace[0].id
                            );
                            newElement.setAttribute(
                                "p",
                                elementsToReplace[0].getAttribute("p")
                            );
                            newElement.style.color = "inherit";
                            newElement.setAttribute("isname", "true");

                            newElement.textContent = names[k].translated;

                            elementsToReplace[0].replaceWith(newElement);

                            for (let l = 1; l < elementsToReplace.length; l++) {
                                elementsToReplace[l].remove();
                            }
                        }

                        if (combinedText.length > names[k].original.length) {
                            break;
                        }
                    }
                }
            });
        }
    }
    function checkForChatNovel() {
        const chatNovelExists = document.querySelector(".chatnovel");
        if (chatNovelExists) {
            runName();
        } else {
            setTimeout(checkForChatNovel, 500);
        }
    }
    window.onload = function () {
        createButton("Run", runName, 90);
        checkForChatNovel();
    };
})();