|
4 | 4 | // @name:zh-TW 怠惰小説下載器 |
5 | 5 | // @name:ja 怠惰者小説ダウンロードツール |
6 | 6 | // @namespace hoothin |
7 | | -// @version 2.8.1 |
| 7 | +// @version 2.8.2 |
8 | 8 | // @description Fetch and download main textual content from the current page, provide special support for novels |
9 | 9 | // @description:zh-CN 通用网站内容抓取工具,可批量抓取任意站点的小说、论坛内容等并保存为TXT文档 |
10 | 10 | // @description:zh-TW 通用網站內容抓取工具,可批量抓取任意站點的小說、論壇內容等並保存為TXT文檔 |
@@ -1311,52 +1311,67 @@ if (window.top != window.self) { |
1311 | 1311 | } |
1312 | 1312 | if(!largestContent)return i18n.error+" : NO TEXT CONTENT"; |
1313 | 1313 | var retainImage=!!GM_getValue("retainImage"); |
1314 | | - var childlist=pageData.querySelectorAll(largestContent.nodeName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):"")); |
1315 | | - function getRightStr(ele, noTextEnable){ |
1316 | | - if(retainImage){ |
1317 | | - [].forEach.call(ele.querySelectorAll("img[src]"), img => { |
1318 | | - let imgTxtNode=document.createTextNode(`, url || location.href)})`); |
1319 | | - img.parentNode.replaceChild(imgTxtNode, img); |
| 1314 | + function getContentByLargest() { |
| 1315 | + var childlist=pageData.querySelectorAll(largestContent.nodeName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):"")); |
| 1316 | + function getRightStr(ele, noTextEnable){ |
| 1317 | + [].forEach.call(ele.querySelectorAll("a[href]"), a => { |
| 1318 | + a.parentNode && a.parentNode.removeChild(a); |
1320 | 1319 | }); |
1321 | | - } |
1322 | | - let childNodes=ele.childNodes,cStr="\r\n",hasText=false; |
1323 | | - [].forEach.call(ele.querySelectorAll("a[href]"), a => { |
1324 | | - a.parentNode && a.parentNode.removeChild(a); |
1325 | | - }); |
1326 | | - for(let j=0;j<childNodes.length;j++){ |
1327 | | - let childNode=childNodes[j]; |
1328 | | - if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true; |
1329 | | - if(childNode.innerHTML){ |
1330 | | - childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r"); |
| 1320 | + if(retainImage){ |
| 1321 | + [].forEach.call(ele.querySelectorAll("img[src]"), img => { |
| 1322 | + let imgTxtNode=document.createTextNode(`, url || location.href)})`); |
| 1323 | + img.parentNode.replaceChild(imgTxtNode, img); |
| 1324 | + }); |
1331 | 1325 | } |
1332 | | - let content=childNode.textContent; |
1333 | | - if(content){ |
1334 | | - if(!content.trim())continue; |
1335 | | - cStr+=content.replace(/[\uFEFF\xA0 ]+/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2"); |
| 1326 | + let childNodes=ele.childNodes,cStr="\r\n",hasText=false; |
| 1327 | + for(let j=0;j<childNodes.length;j++){ |
| 1328 | + let childNode=childNodes[j]; |
| 1329 | + if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true; |
| 1330 | + if(childNode.innerHTML){ |
| 1331 | + childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r"); |
| 1332 | + } |
| 1333 | + let content=childNode.textContent; |
| 1334 | + if(content){ |
| 1335 | + if(!content.trim())continue; |
| 1336 | + cStr+=content.replace(/[\uFEFF\xA0 ]+/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2"); |
| 1337 | + } |
| 1338 | + if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|IMG)$/.test(childNode.nodeName))cStr+="\r\n"; |
1336 | 1339 | } |
1337 | | - if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|IMG)$/.test(childNode.nodeName))cStr+="\r\n"; |
| 1340 | + if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n"; |
1338 | 1341 | } |
1339 | | - if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n"; |
1340 | | - } |
1341 | | - var sameDepthChildren=[]; |
1342 | | - for(i=0;i<childlist.length;i++){ |
1343 | | - var child=childlist[i]; |
1344 | | - if(getDepth(child)==getDepth(largestContent)){ |
1345 | | - if(largestContent.className != child.className)continue; |
1346 | | - sameDepthChildren.push(child); |
| 1342 | + var sameDepthChildren=[]; |
| 1343 | + for(i=0;i<childlist.length;i++){ |
| 1344 | + var child=childlist[i]; |
| 1345 | + if(getDepth(child)==getDepth(largestContent)){ |
| 1346 | + if(largestContent.className != child.className)continue; |
| 1347 | + sameDepthChildren.push(child); |
| 1348 | + } |
1347 | 1349 | } |
| 1350 | + var minLength = largestNum>>2; |
| 1351 | + var tooShort = sameDepthChildren.length <= 3; |
| 1352 | + sameDepthChildren.forEach(child => { |
| 1353 | + if(tooShort && child.innerText.length < minLength) return; |
| 1354 | + if((largestContent.className && largestContent.className == child.className) || largestContent.parentNode == child.parentNode){ |
| 1355 | + getRightStr(child, true); |
| 1356 | + }else { |
| 1357 | + getRightStr(child, false); |
| 1358 | + } |
| 1359 | + }); |
| 1360 | + rStr = rStr.replace(/[\n\r]+/g,"\n\r"); |
1348 | 1361 | } |
1349 | | - var minLength = largestNum>>2; |
1350 | | - var tooShort = sameDepthChildren.length <= 3; |
1351 | | - sameDepthChildren.forEach(child => { |
1352 | | - if(tooShort && child.innerText.length < minLength) return; |
1353 | | - if((largestContent.className && largestContent.className == child.className) || largestContent.parentNode == child.parentNode){ |
1354 | | - getRightStr(child, true); |
1355 | | - }else { |
1356 | | - getRightStr(child, false); |
| 1362 | + getContentByLargest(); |
| 1363 | + if (rStr.length < 100) { |
| 1364 | + let articles = pageData.querySelectorAll("article"); |
| 1365 | + if (articles && articles.length == 1) { |
| 1366 | + largestContent = articles[0]; |
| 1367 | + largestNum = largestContent.innerText.length; |
| 1368 | + if (largestNum > 100) { |
| 1369 | + rStr = ""; |
| 1370 | + getContentByLargest(); |
| 1371 | + } |
1357 | 1372 | } |
1358 | | - }); |
1359 | | - return rStr.replace(/[\n\r]+/g,"\n\r"); |
| 1373 | + } |
| 1374 | + return rStr; |
1360 | 1375 | } |
1361 | 1376 |
|
1362 | 1377 | function getI18n(key, args){ |
|
0 commit comments