// ==UserScript==
// @name DownloadAllContent
// @name:zh-CN 懒人小说下载器
// @name:zh-TW 懶人小説下載器
// @name:ja 怠惰者小説ダウンロードツール
// @namespace hoothin
// @version 1.25
// @description Fetch and download main content on current page, provide special support for chinese novel
// @description:zh-CN 通用网站内容抓取工具,可批量抓取小说、论坛内容等并保存为TXT文档
// @description:zh-TW 通用網站內容抓取工具,可批量抓取小說、論壇內容等並保存為TXT文檔
// @description:ja ユニバーサルサイトコンテンツクロールツール、クロール、フォーラム内容など
// @author hoothin
// @include *
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @require https://cdn.jsdelivr.net/npm/file-saver@1.3.8/FileSaver.min.js
// @license MIT License
// @compatible chrome
// @compatible firefox
// @compatible opera 未测试
// @compatible safari 未测试
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rixixi@sina.com&item_name=Greasy+Fork+donation
// @contributionAmount 1
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var lang = navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
var i18n={};
switch (lang){
case "zh-CN":
i18n={
fetch:"开始下载小说或其他【Ctrl+F9】",
info:"本文是使用懒人小说下载器(DownloadAllContent)脚本下载的",
error:"该段内容获取失败",
downloading:"已下载完成 %s 段,剩余 %s 段
正在下载 %s",
complete:"已全部下载完成,共 %s 段",
del:"设置小说干扰码的CSS选择器",
setting:"懒人小说下载设置"
};
break;
default:
i18n={
fetch:"Download All Content[Ctrl+F9]",
info:"The TXT is downloaded by 'DownloadAllContent'",
error:"Failed in downloading current chapter",
downloading:"%s pages are downloaded, there are still %s pages left
Downloading %s ......",
complete:"Completed! The pages totalled %s",
del:"Set css selectors for delete",
setting:"DownloadAllContent Setting"
};
break;
}
var firefox=navigator.userAgent.toLowerCase().indexOf('firefox')!=-1;
var rocketContent,txtDownContent,txtDownWords,txtDownQuit,txtDownDivInited=false;
function initTxtDownDiv(){
if(txtDownDivInited)return;
txtDownDivInited=true;
rocketContent=document.createElement("div");
document.body.appendChild(rocketContent);
rocketContent.outerHTML=`
`;
txtDownContent=document.querySelector("#txtDownContent");
txtDownWords=document.querySelector("#txtDownWords");
txtDownQuit=document.querySelector("#txtDownQuit");
txtDownQuit.onclick=function(){
txtDownContent.style.display="none";
txtDownContent.parentNode.removeChild(txtDownContent);
};
}
function indexDownload(aEles){
initTxtDownDiv();
var j=0,rCats=[];
function getDocEle(str){
var doc = null;
try {
doc = document.implementation.createHTMLDocument('');
doc.documentElement.innerHTML = str;
}
catch (e) {
console.log('parse error');
}
return doc;
}
function processDoc(i, aTag, doc){
j++;
rCats[i]=(aTag.innerText+"\r\n"+getPageContent(doc));
txtDownContent.style.display="block";
txtDownWords.innerHTML=getI18n("downloading",[j,(aEles.length-j),aTag.innerText]);
if(j==aEles.length){
txtDownWords.innerHTML=getI18n("complete",[j]);
var blob = new Blob([i18n.info+"\r\n"+document.title+"\r\n\r\n"+rCats.join("\r\n\r\n")], {type: "text/plain;charset=utf-8"});
saveAs(blob, document.title+".txt");
}
}
for(let i=0;i=0;j--){
item=content.childNodes[j];
if(item.nodeType==3){
if(/^\s*$/.test(item.data))
item.parentNode.removeChild(item);
else hasText=true;
}else if(/^(I|A|STRONG|B|FONT|P|DL|DD|H\d)$/.test(item.tagName))hasText=true;
}
for(j=content.childNodes.length-1;j>=0;j--){
item=content.childNodes[j];
if(item.nodeType==1 && !/^(I|A|STRONG|B|FONT|BR)$/.test(item.tagName) && /^\s*$/.test(item.innerHTML))
item.parentNode.removeChild(item);
}
if(content.childNodes.length>1){
for(j=0;jlargestNum){
largestNum=curNum;
largestContent=content;
}
}
if(!largestContent)return i18n.error;
var childlist=pageData.querySelectorAll(largestContent.tagName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):""));
function getRightStr(ele, noTextEnable){
let childNodes=ele.childNodes,cStr="\r\n",hasText=false;
for(let j=0;j/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
}
if(childNode.textContent){
cStr+=childNode.textContent.replace(/ +/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2");
}
if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT)$/.test(childNode.tagName))cStr+="\r\n";
}
if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n";
}
for(i=0;i0){
args.forEach(function(item){
resultStr=resultStr.replace(/%s/,item);
});
}
return resultStr;
}
function getDepth(dom){
var pa=dom,i=0;
while(pa.parentNode){
pa=pa.parentNode;
i++;
}
return i;
}
function fetch(){
var aEles=document.querySelectorAll("a"),list=[];
for(var i=0;i2){
if(GM_getValue("contentSort")){
list.sort(function(a,b){
return parseInt(a.innerText.replace(/[^0-9]/ig,"")) - parseInt(b.innerText.replace(/[^0-9]/ig,""));
});
}
indexDownload(list);
}else{
var blob = new Blob([i18n.info+"\r\n"+document.title+"\r\n\r\n"+getPageContent(document)], {type: "text/plain;charset=utf-8"});
saveAs(blob, document.title+".txt");
}
}
document.addEventListener("keydown", function(e) {
if(e.keyCode == 120 && e.ctrlKey) {
fetch();
}
});
function setDel(){
var selValue=GM_getValue("selectors");
var selectors=prompt(i18n.del,selValue?selValue:"");
GM_setValue("selectors",selectors);
if(window.confirm("重新排序")){
GM_setValue("contentSort", true);
}else{
GM_setValue("contentSort", false);
}
}
GM_registerMenuCommand(i18n.fetch, fetch);
GM_registerMenuCommand(i18n.setting, setDel);
})();