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("丛书");
// isbn
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 = "";
// url
var itemUrl = location.href;
// 所属分类
var bookClass = "";
// 作者国籍
var nationality = "";
// 货币符号
var currencySymbol = "";
// 爬虫侧的key标识
var itemKey = "";
// 网站来源id:1-当当网,2-豆瓣读书,3-读书网,4-中国图书网,5-国家图书馆,6-京东,7-亚马逊中国,8-QS查询(QS),9-QS查询(SC)
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