1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
tools/zhihu_book/index.bookmark.js

35 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

javascript: (function () {
/* 保存知乎电子书 - 浏览器书签 */
/* 适用页面https://www.zhihu.com/pub/reader/120208917 */
/* 编写时间2023-07-13 */
let title = document.querySelector('.is-current').querySelector('.chapter-hover-title').childNodes[0].textContent;
let html = document.querySelector('.ZhihuEPub')?.innerHTML;
if (!html) {
html = document.querySelector('.PubHTML')?.childNodes[0]?.childNodes[0]?.innerHTML;
}
let cssPath = "main.css";
/* let level2 = document.querySelector('.is-current').classList.contains('level-2') */
const level = Number(document.querySelector('.is-current').classList[0].replace('level-', ''));
for (let i = 1; i < level; i++) {
cssPath = '../' + cssPath
}
let saveHTML = [
`<html>`,
`<head><meta charset="UTF-8"><title>${title}</title><link rel="stylesheet" href="${cssPath}"></head>`,
`<body class="ZhihuEPub">`,
html,
`</body>`,
`</html>`
].join('\n');
console.log({ title, html, saveHTML });
const blob = new Blob([saveHTML], { type: 'text/html' });
const objectURL = URL.createObjectURL(blob);
const aTag = document.createElement('a');
aTag.href = objectURL;
aTag.download = title + '.html';
aTag.click();
URL.revokeObjectURL(objectURL);
document.querySelector('.reader-buttons').innerText = '成功';
})()