MCPcopy Index your code
hub / github.com/hoothin/UserScripts / getContentByLargest

Function getContentByLargest

DownloadAllContent/DownloadAllContent.user.js:1628–1676  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1626 if(!largestContent)return i18n.error+" : NO TEXT CONTENT";
1627 var retainImage=!!GM_getValue("retainImage");
1628 function getContentByLargest() {
1629 var childlist=pageData.querySelectorAll(largestContent.nodeName);//+(largestContent.className?"."+largestContent.className.replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, '.'):""));
1630 function getRightStr(ele, noTextEnable){
1631 [].forEach.call(ele.querySelectorAll("a[href]"), a => {
1632 a.parentNode && a.parentNode.removeChild(a);
1633 });
1634 if(retainImage){
1635 [].forEach.call(ele.querySelectorAll("img[src]"), img => {
1636 let imgTxtNode=document.createTextNode(`![img](${canonicalUri(img.getAttribute("src"), url || location.href)})`);
1637 img.parentNode.replaceChild(imgTxtNode, img);
1638 });
1639 }
1640 let childNodes=ele.childNodes,cStr="\r\n",hasText=false;
1641 for(let j=0;j<childNodes.length;j++){
1642 let childNode=childNodes[j];
1643 if(childNode.nodeType==3 && childNode.data && !/^[\s\-\_\?\>\|]*$/.test(childNode.data))hasText=true;
1644 if(childNode.innerHTML){
1645 childNode.innerHTML=childNode.innerHTML.replace(/\<\s*br\s*\>/gi,"\r\n").replace(/\n+/gi,"\n").replace(/\r+/gi,"\r");
1646 }
1647 let content=childNode.textContent;
1648 if(content){
1649 if(!content.trim())continue;
1650 cStr+=content.replace(/[\uFEFF\xA0 ]+/g," ").replace(/([^\r]|^)\n([^\r]|$)/gi,"$1\r\n$2");
1651 }
1652 if(childNode.nodeType!=3 && !/^(I|A|STRONG|B|FONT|IMG)$/.test(childNode.nodeName))cStr+="\r\n";
1653 else if(childNode.nextSibling && /^P$/.test(childNode.nextSibling.nodeName))cStr+="\r\n";
1654 }
1655 if(hasText || noTextEnable || ele==largestContent)rStr+=cStr+"\r\n";
1656 }
1657 var sameDepthChildren=[];
1658 for(i=0;i<childlist.length;i++){
1659 var child=childlist[i];
1660 if(getDepth(child)==getDepth(largestContent)){
1661 if(largestContent.className != child.className)continue;
1662 sameDepthChildren.push(child);
1663 }
1664 }
1665 var minLength = largestNum>>2;
1666 var tooShort = sameDepthChildren.length <= 3;
1667 sameDepthChildren.forEach(child => {
1668 if(tooShort && child.innerText.length < minLength) return;
1669 if((largestContent.className && largestContent.className == child.className) || largestContent.parentNode == child.parentNode){
1670 getRightStr(child, true);
1671 }else {
1672 getRightStr(child, false);
1673 }
1674 });
1675 rStr = rStr.replace(/[\n\r]+/g,"\n\r");
1676 }
1677 getContentByLargest();
1678 if (rStr.length < 100) {
1679 let articles = pageData.querySelectorAll("article,.content,#content");

Callers 1

getPageContentFunction · 0.85

Calls 2

getDepthFunction · 0.85
getRightStrFunction · 0.85

Tested by

no test coverage detected