|
4 | 4 | // @name:zh-TW 懶人小説下載器 |
5 | 5 | // @name:ja 怠惰者小説ダウンロードツール |
6 | 6 | // @namespace hoothin |
7 | | -// @version 1.13 |
| 7 | +// @version 1.15 |
8 | 8 | // @description Fetch and download main content on current page, provide special support for chinese novel |
9 | 9 | // @description:zh-CN 通用网站内容抓取工具,可批量抓取小说、论坛内容等并保存为TXT文档 |
10 | 10 | // @description:zh-TW 通用網站內容抓取工具,可批量抓取小說、論壇內容等並保存為TXT文檔 |
|
108 | 108 | var i,j,k,rStr="",pageData=(doc.body?doc.body:doc).cloneNode(true),delList=[]; |
109 | 109 | [].forEach.call(pageData.querySelectorAll("script,style,link,img"),function(item){delList.push(item);}); |
110 | 110 | [].forEach.call(delList,function(item){item.parentNode.removeChild(item);}); |
111 | | - var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td"); |
| 111 | + [].forEach.call(pageData.querySelectorAll("font.jammer"),function(item){ |
| 112 | + item.parentNode.removeChild(item); |
| 113 | + }); |
| 114 | + [].forEach.call(pageData.querySelectorAll("span"),function(item){ |
| 115 | + if(item.style && item.style.display=="none") |
| 116 | + item.parentNode.removeChild(item); |
| 117 | + }); |
| 118 | + var largestContent,contents=pageData.querySelectorAll("span,div,article,p,td"),largestNum=0; |
112 | 119 | for(i=0;i<contents.length;i++){ |
113 | | - let content=contents[i],hasText=false,allSingle=true,item; |
| 120 | + let content=contents[i],hasText=false,allSingle=true,item,curNum=0; |
114 | 121 | for(j=content.childNodes.length-1;j>=0;j--){ |
115 | 122 | item=content.childNodes[j]; |
116 | | - if(item.nodeType==3 && /^\s*$/.test(item.data)) |
117 | | - item.parentNode.removeChild(item); |
| 123 | + if(item.nodeType==3){ |
| 124 | + if(/^\s*$/.test(item.data)) |
| 125 | + item.parentNode.removeChild(item); |
| 126 | + else hasText=true; |
| 127 | + |
| 128 | + } |
118 | 129 | } |
119 | 130 | for(j=content.childNodes.length-1;j>=0;j--){ |
120 | 131 | item=content.childNodes[j]; |
121 | 132 | if(item.nodeType==1 && !/^(I|A|STRONG|B|FONT|BR)$/.test(item.tagName) && /^\s*$/.test(item.innerHTML)) |
122 | 133 | item.parentNode.removeChild(item); |
123 | 134 | } |
124 | | - [].forEach.call(content.querySelectorAll("font.jammer"),function(item){ |
125 | | - item.parentNode.removeChild(item); |
126 | | - }); |
127 | | - [].forEach.call(content.querySelectorAll("span"),function(item){ |
128 | | - if(item.style && item.style.display=="none") |
129 | | - item.parentNode.removeChild(item); |
130 | | - }); |
131 | | - [].forEach.call(content.childNodes,function(item){ |
132 | | - if(item.nodeType==3 && item.data && !/^\s*$/.test(item.data)) |
133 | | - hasText=true; |
134 | | - }); |
135 | 135 | if(content.childNodes.length>1){ |
136 | 136 | for(j=0;j<content.childNodes.length;j++){ |
137 | 137 | item=content.childNodes[j]; |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | if(!allSingle)break; |
147 | | - delList=[]; |
148 | | - [].forEach.call(item.childNodes,function(n){if((n.nodeType==3 && /^\s*$/.test(n.data)) || (n.nodeType==1 && /^\s*$/.test(n.textContent)))delList.push(n);}); |
149 | | - [].forEach.call(delList,function(n){n.parentNode.removeChild(n);}); |
150 | 147 | } |
151 | 148 | } |
152 | 149 | }else{ |
153 | 150 | allSingle=false; |
154 | 151 | } |
155 | | - if(!allSingle){ |
| 152 | + if(allSingle){ |
| 153 | + curNum=(navigator.userAgent.toLowerCase().indexOf('firefox')==-1?content.innerText.length:content.textContent.length); |
| 154 | + }else { |
156 | 155 | if(!hasText)continue; |
157 | | - if(content.firstChild && ( |
158 | | - (content.firstChild.nodeType!=3 && !/^(I|A|STRONG|B|FONT|BR)$/.test(content.firstChild.tagName)) || |
159 | | - (content.firstChild.nodeType==3 && /^\s*$/.test(content.firstChild.data) && |
160 | | - (!content.childNodes[1] || !(content.childNodes[1].nodeType==3 || /^(I|A|STRONG|B|FONT|BR)$/.test(content.childNodes[1].tagName)))) |
161 | | - )) |
162 | | - continue; |
163 | 156 | if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0) |
164 | 157 | continue; |
| 158 | + [].forEach.call(content.childNodes,function(item){ |
| 159 | + if(item.nodeType==3) |
| 160 | + curNum+=item.data.length; |
| 161 | + }); |
165 | 162 | } |
166 | | - if(navigator.userAgent.toLowerCase().indexOf('firefox')!=-1){ |
167 | | - if(!largestContent || largestContent.textContent.length<content.textContent.length){ |
168 | | - largestContent=content; |
169 | | - } |
170 | | - }else{ |
171 | | - if(!largestContent || largestContent.innerText.length<content.innerText.length){ |
172 | | - largestContent=content; |
173 | | - } |
| 163 | + if(curNum>largestNum){ |
| 164 | + largestNum=curNum; |
| 165 | + largestContent=content; |
174 | 166 | } |
175 | 167 | } |
176 | 168 | if(!largestContent)return i18n.error; |
|
0 commit comments