This script let you save the content of the reading chapter on bachngocsach.vip (vip.bachngocsach.com) from clipboard
当前为
// ==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==
// ==/UserScript==
function getStyle(style) {
if (!style||typeof style!=='string') return false;
console.log('Style: ',style);
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);
})
}
let oldUrl='';
async function bnsVipChapter() {
console.log('Then go to here');
if (oldUrl==window.location.href) return false;
//let content = await waitForKeyElements('#chapter-id');
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
console.log(style);
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
console.log(style);
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;
}
console.log(badLine);
badPara[style[className]] = badLine.join('');
}
break;
}//end switch
console.log(style);
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(newChapterLoaded);
//let target=document.querySelector('#box-chapter-content')??document.querySelector('#section-detail-story');
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 newChapterLoaded(e) {
if (oldUrl==window.location.href) return false;
await bnsVipChapter();
oldurl=window.location.href;
return true;
}
(async function () {
await newChapterLoaded();
rootObserver=new MutationObserver(rootObserve);
rootObserver.observe(document.querySelector('#app:last-child'),{childList: true, subtree: true });
})();