|
4 | 4 | // @name:zh-TW 懶人小説下載器 |
5 | 5 | // @name:ja 怠惰者小説ダウンロードツール |
6 | 6 | // @namespace hoothin |
7 | | -// @version 1.03 |
| 7 | +// @version 1.05 |
8 | 8 | // @description Fetch and download main content on current page |
9 | 9 | // @description:zh-CN 通用网站内容抓取工具,可批量抓取小说、论坛内容等并保存为TXT文档 |
10 | 10 | // @description:zh-TW 通用網站內容抓取工具,可批量抓取小說、論壇內容等並保存為TXT文檔 |
|
108 | 108 | [].forEach.call(delList,function(item){item.parentNode.removeChild(item);}); |
109 | 109 | var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td"); |
110 | 110 | for(i=0;i<contents.length;i++){ |
111 | | - let content=contents[i],hasText=false; |
| 111 | + let content=contents[i],hasText=false,allSingle=true,item; |
| 112 | + for(j=content.childNodes.length-1;j>=0;j--){ |
| 113 | + item=content.childNodes[j]; |
| 114 | + if(item.nodeType==3 && /^\s*$/.test(item.data)) |
| 115 | + item.parentNode.removeChild(item); |
| 116 | + } |
112 | 117 | [].forEach.call(content.childNodes,function(item){ |
113 | 118 | if(item.nodeType==3 && item.data && !/^\s*$/.test(item.data)) |
114 | 119 | hasText=true; |
115 | 120 | }); |
116 | | - if(!hasText)continue; |
117 | | - if(content.childNodes){ |
118 | | - for(j=content.childNodes.length-1;j>=0;j--){ |
119 | | - var item=content.childNodes[j]; |
120 | | - if(item.nodeType==3 && /^\s*$/.test(item.data)) |
121 | | - item.parentNode.removeChild(item); |
| 121 | + if(content.childNodes.length>1){ |
| 122 | + for(j=0;j<content.childNodes.length;j++){ |
| 123 | + item=content.childNodes[j]; |
| 124 | + if(item.nodeType==1 && item.firstChild && item.firstChild.nodeType!=3){ |
| 125 | + allSingle=false; |
| 126 | + break; |
| 127 | + } |
122 | 128 | } |
| 129 | + }else{ |
| 130 | + allSingle=false; |
| 131 | + } |
| 132 | + if(!allSingle){ |
| 133 | + if(!hasText)continue; |
| 134 | + if(content.firstChild && ( |
| 135 | + (content.firstChild.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(content.firstChild.tagName)) || |
| 136 | + (content.firstChild.nodeType==3 && /^\s*$/.test(content.firstChild.data) && |
| 137 | + (!content.childNodes[1] || !(content.childNodes[1].nodeType==3 || /^(I|A|STRONG|B|FONT|BR)$/.test(content.childNodes[1].tagName)))) |
| 138 | + )) |
| 139 | + continue; |
| 140 | + if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0) |
| 141 | + continue; |
123 | 142 | } |
124 | | - if(content.firstChild && ( |
125 | | - (content.firstChild.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(content.firstChild.tagName)) || |
126 | | - (content.firstChild.nodeType==3 && /^\s*$/.test(content.firstChild.data) && |
127 | | - (!content.childNodes[1] || !(content.childNodes[1].nodeType==3 || /^(I|A|STRONG|B|FONT|BR)$/.test(content.childNodes[1].tagName)))) |
128 | | - )) |
129 | | - continue; |
130 | | - if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0) |
131 | | - continue; |
132 | 143 | if(navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){ |
133 | 144 | if(!largestContent || largestContent.textContent.length<content.textContent.length){ |
134 | 145 | largestContent=content; |
|
146 | 157 | let childNode=childNodes[j]; |
147 | 158 | if(childNode.nodeType==3 && childNode.data && !/^\s*$/.test(childNode.data))hasText=true; |
148 | 159 | if(childNode.tagName=="BR")cStr+="\r\n"; |
149 | | - else if(childNode.textContent)cStr+=childNode.textContent.replace(/\s*/," "); |
| 160 | + else if(childNode.textContent)cStr+=childNode.textContent.replace(/ +/g," ").replace(/([^\r]|^)\n([^\r]|$)/g,"$1\r\n$2"); |
150 | 161 | } |
151 | 162 | if(hasText || noTextEnable)rStr+=cStr+"\r\n"; |
152 | 163 | } |
|
176 | 187 | var aEles=document.querySelectorAll("a"),list=[]; |
177 | 188 | for(var i=0;i<aEles.length;i++){ |
178 | 189 | var aEle=aEles[i]; |
179 | | - if(aEle.href && /第[\d|〇|零|一|二|三|四|五|六|七|八|九|十|百|千|万|萬]+[章|节|回|卷|折|篇|幕|集]|序|序\s*言|序\s*章|前\s*言|引\s*言|引\s*子|摘\s*要|楔\s*子|后\s*记|附\s*言|结\s*语|[\d|〇|零|一|二|三|四|五|六|七|八|九|十|百|千|万|萬]+、/.test(aEle.innerHTML)){ |
| 190 | + if(aEle.href && /第.+[章|节|回|卷|折|篇|幕|集]|序|序\s*言|序\s*章|前\s*言|引\s*言|引\s*子|摘\s*要|楔\s*子|后\s*记|附\s*言|结\s*语|[\d|〇|零|一|二|三|四|五|六|七|八|九|十|百|千|万|萬|-]+(、|))/.test(aEle.innerHTML)){ |
180 | 191 | list.push(aEle); |
181 | 192 | } |
182 | 193 | } |
|
0 commit comments