【油猴脚本】 某电子书网站,电子书打包下载脚本
脚本用途:合并一部电子书多个章节为一个html文件 ,并清理网页中的html代码, 生成的html 适合转换为 kindle 的 mobi 等格式使用方法:打开电子书的章节索引页面,会自动生成一个 “点击下载电子书”的链接,点击即可下载。
避免侵权,网站名称已隐藏。 代码仅供参考
// ==UserScript==
// @name **文学电子书下载
// @namespace http://tampermonkey.net/
// @version 2024-01-31
// @description打包下载**文学电子书下载!
// @AuThor You
// @match https://www.*ing*ue*oke.com/*/
// @Icon https://www.*ing*ue*oke.com/favicon.ico
// @grant none
// ==/UserScript==
(function() {
'use strict';
var contentList = document.getElementById('content-list')
if(contentList === null){
return;
}
var bookDescribe = contentList.querySelector('.book-describe').innerText;
var metas = bookDescribe.split('\n\n');
var title = metas;
var author = metas.split(':');
var guojia = metas.split(':');
var niandai = metas.split(':');
var description= metas;
var booklist = document.querySelector('.book-list');
var sections = [];
var tasks = [];
booklist.querySelectorAll('a').forEach(function(a,i){
sections.push({title:a.title,content:i});
var t = fetch(a.href).then(function(response){
return response.text()}
).then(function (text) {
var div = document.createElement('div');
div.innerHTML = text;
var nr1=div.querySelector('#nr1 div');
sections.content = nr1.innerHTML;
});
tasks.push(t);
});
Promise.all(tasks).then(function(){
var html = ['<!DOCTYPE html>',
'<html lang="zh-CN">',
'<head>',
'<meta charset="utf-8">',
'<meta name="author" content="' + author + '">',
'<meta name="description" content="'+ description +'">',
'<title>' + title + '</title>',
'</head>',
'<body>',
'<article>',
'<h1>' + title + ' <small> '+ author +'</small></h1>'];
sections.forEach(function(section){
html.push('<section>');
html.push('<h2>' + section.title + '</h2>');
html.push(section.content);
html.push('</section>');
});
html.push('</article>');
html.push('</body>');
html.push('</html>');
contentList.querySelector('.book-describe').innerHTML = '<p><a id="download">点击下载电子书</a></p>' + contentList.querySelector('.book-describe').innerHTML;
var a = document.getElementById('download');
a.href = 'data:text/plain;charset=utf-8,'+html.join('\n');
a.download = title + '.html';
});
})();
// ==UserScript==
// @name 星月文学电子书下载
// @namespace http://tampermonkey.net/
// @version 2024-01-31
// @description打包下载星月文学电子书下载!
// @author You
// @match https://www.xingyueboke.com/*/
// @icon https://www.xingyueboke.com/favicon.ico
// @grant none
// ==/UserScript==
本帖最后由 塞北的雪 于 2024-1-31 19:05 编辑
狗尾续貂,改进了样式和下载过大文件可能下载不全的问题。
// ==UserScript==
// @name 星月文学电子书下载
// @namespace https://www.52pojie.cn/thread-1886476-1-1.html
// @version 2024-01-31
// @description打包下载星月文学电子书下载!
// @author bh4ger
// @match https://www.xingyueboke.com/*/
// @icon https://www.xingyueboke.com/favicon.ico
// @grant none
// ==/UserScript==
(function() {
'use strict';
var contentList = document.getElementById('content-list')
if(contentList === null){
return;
}
var bookDescribe = contentList.querySelector('.book-describe').innerText;
var metas = bookDescribe.split('\n\n');
var title = metas;
var author = metas.split(':');
var guojia = metas.split(':');
var niandai = metas.split(':');
var description= metas;
var booklist = document.querySelector('.book-list');
var sections = [];
var tasks = [];
booklist.querySelectorAll('a').forEach(function(a,i){
sections.push({title:a.title,content:i});
var t = fetch(a.href)
.then(function(response){
return response.text()
})
.then(function (text) {
var div = document.createElement('div');
div.innerHTML = text;
var nr1=div.querySelector('#nr1 div');
sections.content = nr1.innerHTML;
});
tasks.push(t);
});
Promise.all(tasks).then(function(){
var html = ['<!DOCTYPE html>',
'<html lang="zh-CN">',
'<head>',
'<meta charset="utf-8">',
'<meta name="author" content="' + author + '">',
'<meta name="description" content="'+ description +'">',
'<title>' + title + '</title>',
'</head>',
'<body>',
'<article>',
'<h1>' + title + ' <small> '+ author +'</small></h1>'];
sections.forEach(function(section){
html.push('<section>');
html.push('<h2>' + section.title + '</h2>');
html.push(section.content);
html.push('</section>');
});
html.push('</article>');
html.push('</body>');
html.push('</html>');
contentList.querySelector('.book-describe').innerHTML = '<div class="scrolltobt"><a id="download">下载电子书</a><br /></br /><a id="singlePage">单页浏览</a></div>' + contentList.querySelector('.book-describe').innerHTML;
let a = document.getElementById('download');
a.href = 'data:text/plain;charset=utf-8,'+html.join('\n').replaceAll('#','@');
a.download = title + '.html';
let b=document.getElementById('singlePage');
b.href='javascript:void(0);';
b.addEventListener("click",function(){
let win=window.open("about:blank");
win.document.write(html.join('\n'));
});
});})();
哪家呢,我很像知道,否则没有办法下载 能给点提示说下是哪家的吗? 哪家呢,我很像知道,否则没有办法下载
有没有提示,我也想知道 感谢分享 对于电子书一党确实是一个福利脚本 试过了,确实挺厉害的,可以下载使用。 牛🐮🐮🐮🐮