Skip to content

Commit 8ebe754

Browse files
committed
fix
1 parent 2250575 commit 8ebe754

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

DownloadAllContent/DownloadAllContent.user.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @name:zh-TW 懶人小説下載器
55
// @name:ja 怠惰者小説ダウンロードツール
66
// @namespace hoothin
7-
// @version 0.8
7+
// @version 1.0
88
// @description Fetch and download main content on current page
99
// @description:zh-CN 通用网站内容抓取工具,可批量抓取小说、论坛内容等并保存为TXT文档
1010
// @description:zh-TW 通用網站內容抓取工具,可批量抓取小說、論壇內容等並保存為TXT文檔
@@ -30,14 +30,14 @@
3030
switch (lang){
3131
case "zh-CN":
3232
i18n={
33-
fetch:"开始下载小说或其他【F9】",
34-
info:"本文使用DownloadAllContent脚本下载"
33+
fetch:"开始下载小说或其他【Ctrl+F9】",
34+
info:"本文是使用懒人小说下载器(DownloadAllContent)脚本下载的"
3535
};
3636
break;
3737
default:
3838
i18n={
39-
fetch:"Download All Content[F9]",
40-
info:"The TXT is downloaded with 'DownloadAllContent'"
39+
fetch:"Download All Content[Ctrl+F9]",
40+
info:"The TXT is downloaded by 'DownloadAllContent'"
4141
};
4242
break;
4343
}
@@ -100,15 +100,29 @@
100100
}
101101
}
102102

103-
function getPageContent(pageData){
104-
var i,rStr="";
105-
var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td");
103+
function getPageContent(doc){
104+
var i,j,rStr="",pageData=(doc.body?doc.body:doc).cloneNode(true),delList=[];
105+
[].forEach.call(pageData.querySelectorAll("script,style,link"),function(item){delList.push(item);});
106+
[].forEach.call(delList,function(item){item.parentNode.removeChild(item);});
107+
var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td,strong,font");
106108
for(i=0;i<contents.length;i++){
107-
var content=contents[i];
109+
let content=contents[i],hasText=false;
110+
[].forEach.call(content.childNodes,function(item){
111+
if(item.nodeType==3 && item.data && !/^\s*$/.test(item.data))
112+
hasText=true;
113+
});
114+
if(!hasText)continue;
115+
if(content.childNodes){
116+
for(j=content.childNodes.length-1;j>=0;j--){
117+
var item=content.childNodes[j];
118+
if(item.nodeType==3 && /^\s*$/.test(item.data))
119+
item.parentNode.removeChild(item);
120+
}
121+
}
108122
if(content.firstChild && (
109-
(content.firstChild.nodeType!=3 && !/^[I|A]$/.test(content.firstChild.tagName)) ||
110-
(/^\s*$/.test(content.firstChild.data) &&
111-
(!content.childNodes[1] || !/^[I|A]$/.test(content.childNodes[1].tagName)))
123+
(content.firstChild.nodeType!=3 && !/^(I|A|STRONG|FONT|BR)$/.test(content.firstChild.tagName)) ||
124+
(content.firstChild.nodeType==3 && /^\s*$/.test(content.firstChild.data) &&
125+
(!content.childNodes[1] || !(content.childNodes[1].nodeType==3 || /^(I|A|STRONG|FONT|BR)$/.test(content.childNodes[1].tagName))))
112126
))
113127
continue;
114128
if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0)
@@ -130,9 +144,9 @@
130144
let childNode=childNodes[j];
131145
if(childNode.nodeType==3 && childNode.data && !/^\s*$/.test(childNode.data))hasText=true;
132146
if(childNode.tagName=="BR")cStr+="\r\n";
133-
else if(!/SCRIPT|STYLE/.test(childNode.tagName) && childNode.textContent)cStr+=childNode.textContent.replace(/\s*/," ");
147+
else if(childNode.textContent)cStr+=childNode.textContent.replace(/\s*/," ");
134148
}
135-
if(hasText || noTextEnable)rStr+=cStr;
149+
if(hasText || noTextEnable)rStr+=cStr+"\r\n";
136150
}
137151
for(i=0;i<childlist.length;i++){
138152
var child=childlist[i];
@@ -160,7 +174,7 @@
160174
var aEles=document.querySelectorAll("a"),list=[];
161175
for(var i=0;i<aEles.length;i++){
162176
var aEle=aEles[i];
163-
if(/[\d||||||||||||||||]+[|||||||]||\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*/.test(aEle.innerHTML)){
177+
if(aEle.href && /[\d||||||||||||||||]+[|||||||]||\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*|\s*/.test(aEle.innerHTML)){
164178
list.push(aEle);
165179
}
166180
}

0 commit comments

Comments
 (0)