function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function getChildTextList(node) {
const nodeList = node.childNodes;
let str = [];
if (nodeList) {
for (let i = 0; i < nodeList.length; i++) {
const curNode = nodeList[i];
if (curNode.nodeName === "#text") {
const curStr = curNode.nodeValue.trim().replace(/\n/g, "");
if (curStr) {
str.push(curStr);
}
} else {
str.push(...getChildTextList(curNode));
}
}
}
return str;
}
function getChildText(node) {
return getChildTextList(node).join("\n");
}
function getSpecialSubTitle(node) {
let cur = node;
let str = "";
try {
while (cur && cur.nodeName !== "BR") {
if (cur.nodeName === "#text") {
str += cur.nodeValue.trim().replace(/\n/g, "");
} else if (cur.nodeName === "A") {
str += cur.innerText;
}
cur = cur.nextSibling;
}
} catch (err) {
console.error(err);
}
return str;
}
function getSubtitle(key) {
const list = document.querySelectorAll("#info .pl");
const node = Array.prototype.slice
.apply(list)
.find((el) => el.innerText.includes(key));
if (node) {
if (node.nextSibling?.nextSibling?.nodeName === "A") {
return getSpecialSubTitle(node.nextSibling.nextSibling);
}
return node.nextSibling.nodeValue.trim();
}
return "";
}
function getIntroContent(key, option = {}) {
const { isFull, isAll } = option;
const list = document.querySelectorAll(".related_info h2");
if (isFull) {
const node = Array.prototype.slice
.apply(list)
.find((el) => el.innerText.includes(key));
if (node && node.nextElementSibling) {
const fullNode = node.nextElementSibling.nextElementSibling;
if (
fullNode &&
fullNode.classList.contains("indent") &&
fullNode.id.includes("full")
) {
const str = getChildText(fullNode);
return str.replace("\n· · · · · · (\n收起\n)", "");
} else if (node.nextElementSibling.classList.contains("indent")) {
return getChildText(node.nextElementSibling);
}
}
} else if (isAll) {
const nodeList = Array.prototype.slice
.apply(list)
.filter(
(el) =>
el.innerText.includes(key) &&
!!el.nextElementSibling &&
el.nextElementSibling.classList.contains("indent")
);
const node = nodeList.find(
(el) => !!el.nextElementSibling.querySelector(".all .intro")
);
if (node) {
return getChildText(node.nextElementSibling.querySelector(".all .intro"));
} else if (nodeList.length > 0) {
return getChildText(
nodeList[0].nextElementSibling.querySelector(".intro")
);
}
}
return "";
}
async function work() {
await sleep(1000);
var bookFace = document.querySelector("div.book-pic > div.pic").firstElementChild.src;
var title = document.querySelector("div.book-title").firstElementChild.innerText;
var titleList = "";
var subTitle = getSubtitle("副标题");
var author = document.querySelector("div.book-details-left > table > tbody > tr:nth-child(1) > td:nth-child(2)").innerText;
var translator = getSubtitle("译者");
var press =document.querySelector("div.book-details-left > table > tbody > tr:nth-child(2) > td:nth-child(2) > a").innerText;
var pubTime = document.querySelector("body > div:nth-child(6) > div.bookdetails-left > div.book-details > table > tbody > tr:nth-child(1) > td:nth-child(4)").innerText;
var price = getSubtitle("定价");
var kaiBen = "";
var paper = "";
var pack = "";
var isTaoZhuang = "";
var charNum = "";
var pageNum = getSubtitle("页数");
var version = "";
var zhuangZhen = getSubtitle("装帧");
var series = getSubtitle("丛书");
var isbn = getSubtitle("ISBN");
var editorRec = "";
var contentIntro = getIntroContent("内容简介", { isAll: true });
var authorIntro = getIntroContent("作者简介", { isAll: true });
var mediaComment = "";
var sections = getIntroContent("目录", { isFull: true });
var businessDetails = "";
var itemUrl = location.href;
var bookClass = "";
var nationality = "";
var currencySymbol = "";
var itemKey = "";
var sourceId = 2;
var sourceName = "豆瓣读书";
var oldTitle = getSubtitle("原作名");
var showImg = "";
var illustration = "";
var weight = "";
var freeRead = "";
var bookNum = "";
var originalGraph = "";
var unClearImg = "";
var result = {
bookFace,
title,
titleList,
subTitle,
author,
translator,
press,
pubTime,
price,
kaiBen,
paper,
pack,
isTaoZhuang,
charNum,
pageNum,
version,
zhuangZhen,
series,
isbn,
editorRec,
contentIntro,
authorIntro,
mediaComment,
sections,
businessDetails,
itemUrl,
bookClass,
nationality,
currencySymbol,
itemKey,
sourceId,
sourceName,
oldTitle,
showImg,
illustration,
weight,
freeRead,
bookNum,
originalGraph,
unClearImg,
};
console.log("bookIsbnResult:", result);
window.chief.submit("bookIsbnResult", result);
window.chief.stop();
}
work();
console