bachngocsach vip chapter content copy

This script let you save the content of the reading chapter on bachngocsach.vip (vip.bachngocsach.com) from clipboard

目前为 2024-06-02 提交的版本。查看 最新版本

// ==UserScript==
// @name        bachngocsach vip chapter content copy
// @name:en     bachngocsach vip chapter content copy
// @namespace   Violentmonkey Scripts
// @match       https://bachngocsach.net.vn/*
// @grant       GM.setClipboard
// @version     1.0.1
// @author      Me
// @run-at       document-idle
// @description This script let you save the content of the reading chapter on bachngocsach.vip (vip.bachngocsach.com) from clipboard
// @description:en This script let you save the content of the reading chapter on bachngocsach.vip (vip.bachngocsach.com) from clipboard
// ==/UserScript==

let observeTimes = 1;
let oldURL = '';

(async function () {
    let observer = new MutationObserver(async (mList) => {
        mList.forEach(async (m) => {
            if (m.target.className?.includes('published-content') && window.location.href != oldURL) {
                oldURL = window.location.href; ///?? xac định ddee ơ dau.
                await getChapterContent();
            }
        });
    });
    observer.observe(document.querySelector('body'), { childList: true, subtree: true });
})();

async function getChapterContent() {
    let arrEl = document.querySelectorAll('div.published-content');
    let goodText = arrEl[0].previousElementSibling?.innerText;
    let bad = arrEl[0];
    let badPara = Array(bad.children.length);
    let style = {};

    for (const eights of bad.children) {
        let className = eights.className.toLowerCase();
        style[className] = parseInt(getComputedStyle(eights).order);
        let badLine = Array(eights.children.length);
        for (const sixes of eights.children) {
            let tagName = sixes.tagName.toLowerCase();
            if (!style[tagName]) style[tagName] = parseInt(getComputedStyle(sixes).order);
            badLine[style[tagName]] = sixes.textContent;
        }
        badPara[style[className]] = badLine.join('');
    }
    let txt = (goodText.trim() + badPara.join('\n\n')).replaceAll(/\n{3,}/g, '\n\n');
    GM.setClipboard(txt);
    console.log(txt);
    return txt;
}

async function startDownload() {
    GM.setValue('downloading', true);
    if (/^https:\/\/bachngocsach\.net\.vn\/truyen\/[a-z\-\d]+\/?$/.test(window.location.href)) window.location.assign(window.location.href + '/chuong-1');
    else window.location.reload();
}

function sleep(ms = 10) { return new Promise(rs => setTimeout(rs, ms)) }

function waitForKeyElement(sel = 'body', ms = 2000) {
    const delay = 100;
    const tries = Math.floor(ms / delay) + 1;
    return new Promise(async (rs, rj) => {
        let start = (new Date()).getTime();
        for await (const i of Array(tries)) {
            let result = document.querySelectorAll(sel);
            if (result.length > 0) rs(result);
            await sleep(delay);
        }
        rj('Khong có element');
    });
}