// ==UserScript==
// @name 轻小说文库下载
// @namespace http://tampermonkey.net/
// @version 0.2
// @description try to take over the world!
// @author You
// @match https://www.wenku8.net/*
// @match http://www.wenku8.net/*
// @connect dl.wenku8.com
// @require http://code.jquery.com/jquery-3.5.1.min.js
// @grant GM_xmlhttpRequest
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
//替换所有
String.prototype.replaceAll= function(exp,newStr){
return this.replace(new RegExp(exp,"gm"),newStr);
};
//标记含有html代码的行
var htmlLine = function(txt){
return 'c1ef6520'+ txt +'1a5219e9b2b0';
};
//格式化内容
var contentFormat = function(txt){
//设置章节名样式
txt = txt.replaceAll(/^ {2}\S+.*$/,function(match){
return htmlLine('
'+ match.trim() +'
');
});
txt = $('').text(txt).html();
txt = txt.replaceAll(/ /,' ');
txt = txt.replaceAll(/(c1ef6520).*(1a5219e9b2b0)/,function(match){
return $('').html(match.replaceAll(/( )/,' ').replace('c1ef6520','').replace('1a5219e9b2b0','')).text();
});
txt = txt.replaceAll(/[\r\n]+/,'
');
return txt;
};
//目录页面或内容会声明章节变量。
if('undefined' == typeof chapter_id || undefined===chapter_id) return;
//本书编号 article_id
//目录页面章节id定义为 '0'
if('0' == chapter_id)//在章节名之后添加下载链接
{
//书名
var aname = $('#title').text();
//添加全本下载链接
var allDLink = 'http://dl.wenku8.com/down.php?type=utf8&id='+article_id+'&fname='+aname;
var allaEle = ' 全本下载(utf-8)';
$('#title').append(allaEle);
$('.vcss').each(
function(){
var jqthis = $(this);
var vname = jqthis.text();
var nextChapter = jqthis.parent().next().children().first();
var vid = $('a',nextChapter).attr('href').split('.')[0];
//vid = (Number(vid)-1).toString();//部分文章无法使用章节减1来下载,直接使用章节可以下载(下载内容使用-1时章节名包含小说名,不-1不包含小说名)
var dlink = 'http://dl.wenku8.com/packtxt.php?aid='+article_id+
'&vid='+vid+'&aname='+aname+'&vname='+vname+'&charset=utf-8';
var aEle = ' 下载(utf-8)'
jqthis.append(aEle);
}
);
return;
}
if('0' != chapter_id) //内容页面
{
if($('#contentmain span').first().text().trim() == 'null')//如果包含一个内容为 'null'的span则判定为版权限制
{
//设置下一页和上一页快捷键到目录页
preview_page = next_page = index_page;
$('#content').text('正在下载,请稍候...');
var dlink = 'http://dl.wenku8.com/packtxt.php?aid='+article_id+
'&vid='+chapter_id;
GM_xmlhttpRequest({
method :'GET',
url :dlink,
onload :function(response){
if(response.statusText == "OK"){
var formatTxt = contentFormat(response.responseText);
$('#content').html(formatTxt);
//$('#content').text(response.responseText);
}else{
$('#content').text('下载失败');
}
},
});
}
}
// Your code here...
})();