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-04-20 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        bachngocsach vip chapter content copy
// @name:en     bachngocsach vip chapter content copy
// @namespace   Violentmonkey Scripts
// @match       https://bachngocsach.vip/*
// @match       https://vip.bachngocsach.com/*
// @grant       GM.setClipboard
// @version     1.0
// @author      Me
// @run-at       document-end
// @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==

function getStyle(style) {
  if (!style || typeof style !== 'string') return false;
  let result = {};
  style.split('}').forEach(e => {
    let name = e.startsWith('.') ? e.slice(1, e.indexOf('{')) : e.slice(0, e.indexOf('{'));
    let order = parseInt(e.slice(e.indexOf(':') + 1));
    result[name] = order;
  });
  return result;
}

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

async function waitForKeyElements(selector = '#content', delayTime = 300, tries = 10) {
  return new Promise(async (res) => {
    for (let i = 0; i < tries; i++) {
      let result = document.querySelectorAll(selector);
      if (result.length > 0) res(result);
      await sleep(delayTime);
    }
    res(false);
  })
}

var oldUrl = '';
async function bnsVipChapter() {
  if (oldUrl == window.location.href) return false;
  let content = await waitForKeyElements('#id_chap_content');
  if (!content) return false;

  document.querySelector('#id_chap_content p:nth-child(2)').scrollIntoView();
  let style = await waitForKeyElements('#box-chapter-content style'); //return HTMLCollection or false
  if (style) style = getStyle(style[0].textContent);

  let txt = '';
  if (content.length > 0) {
    let goodText = document.querySelector('#chapter-id>div>div:nth-child(1)').textContent;
    let badText = document.querySelector('#chapter-id>div>div:nth-child(2)');

    let way = 'new';
    if (style) way = 'old';  //old,new
    let badPara = Array(badText.children.length);

    switch (way) {
      case 'old':
        for (const eights of badText.children) {
          let className = eights.className.toLowerCase();
          let badLine = Array(eights.children.length);
          for (const sixes of eights.children) badLine[style[sixes.tagName.toLowerCase()]] = sixes.textContent;
          badPara[style[className]] = badLine.join('');
        }
        break;

      case 'new':
        style = {};
        for (let eights of badText.children) {
          let className = eights.className.toLowerCase();
          style[className] = parseInt(getComputedStyle(eights).order);
          let badLine = Array(eights.children.length);
          for (let 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('');
        }
        break;
    }//end switch  
    badText = badPara.join('\n\n');
    txt = goodText.trim() + badText.trim();
  }
  GM.setClipboard(txt, 'text/plain');
  console.log(txt);
  document.querySelector('#app').scrollIntoView();
}

let rootObserver;
let chapterObserver;

async function rootObserve() {
  if (!chapterObserver &&
    /(^http(s)?:\/\/vip.bachngocsach.com\/(dich|sang-tac)\/)|(^http(s)?:\/\/bachngocsach.vip\/(dich|sang-tac)\/)/i.test(window.location.href)) {
    chapterObserver = new MutationObserver(chapterObserve);
    let target = document.querySelector('#box-chapter-content');
    if (target) chapterObserver.observe(target, { childList: true, subtree: true });
  }

  if (chapterObserver &&
    !/(^http(s)?:\/\/vip.bachngocsach.com\/(dich|sang-tac)\/)|(^http(s)?:\/\/bachngocsach.vip\/(dich|sang-tac)\/)/i.test(window.location.href)) {
    chapterObserver.disconnect();
    chapterObserver = undefined;
  }
}

async function chapterObserve(e) {
  if (oldUrl == window.location.href) return false;
  await bnsVipChapter();
  oldUrl = window.location.href;
  return true;
}

(async function () {
  await chapterObserve();
  rootObserver = new MutationObserver(rootObserve);
  rootObserver.observe(document.querySelector('#app:last-child'), { childList: true, subtree: true });
})();