|
4 | 4 | // @name:zh-TW 懶人小説下載器 |
5 | 5 | // @name:ja 怠惰者小説ダウンロードツール |
6 | 6 | // @namespace hoothin |
7 | | -// @version 0.8 |
| 7 | +// @version 1.0 |
8 | 8 | // @description Fetch and download main content on current page |
9 | 9 | // @description:zh-CN 通用网站内容抓取工具,可批量抓取小说、论坛内容等并保存为TXT文档 |
10 | 10 | // @description:zh-TW 通用網站內容抓取工具,可批量抓取小說、論壇內容等並保存為TXT文檔 |
|
30 | 30 | switch (lang){ |
31 | 31 | case "zh-CN": |
32 | 32 | i18n={ |
33 | | - fetch:"开始下载小说或其他【F9】", |
34 | | - info:"本文使用DownloadAllContent脚本下载" |
| 33 | + fetch:"开始下载小说或其他【Ctrl+F9】", |
| 34 | + info:"本文是使用懒人小说下载器(DownloadAllContent)脚本下载的" |
35 | 35 | }; |
36 | 36 | break; |
37 | 37 | default: |
38 | 38 | 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'" |
41 | 41 | }; |
42 | 42 | break; |
43 | 43 | } |
|
100 | 100 | } |
101 | 101 | } |
102 | 102 |
|
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"); |
106 | 108 | 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 | + } |
108 | 122 | 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)))) |
112 | 126 | )) |
113 | 127 | continue; |
114 | 128 | if(pageData==document && content.offsetWidth<=0 && content.offsetHeight<=0) |
|
130 | 144 | let childNode=childNodes[j]; |
131 | 145 | if(childNode.nodeType==3 && childNode.data && !/^\s*$/.test(childNode.data))hasText=true; |
132 | 146 | 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*/," "); |
134 | 148 | } |
135 | | - if(hasText || noTextEnable)rStr+=cStr; |
| 149 | + if(hasText || noTextEnable)rStr+=cStr+"\r\n"; |
136 | 150 | } |
137 | 151 | for(i=0;i<childlist.length;i++){ |
138 | 152 | var child=childlist[i]; |
|
160 | 174 | var aEles=document.querySelectorAll("a"),list=[]; |
161 | 175 | for(var i=0;i<aEles.length;i++){ |
162 | 176 | 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)){ |
164 | 178 | list.push(aEle); |
165 | 179 | } |
166 | 180 | } |
|
0 commit comments