Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. function trim(str, chars) { return ltrim(rtrim(str, chars), chars); }
  2. function ltrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
  3. function rtrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }
  4.  
  5. (function() {
  6.  
  7. // these methods perform operations on the readability structure, merging new values with
  8. // existing values.
  9. var textualCalc = [
  10.  
  11. // simple space count with cleaned text
  12. function spaceToWeight(cleanText, regularText, readability) {
  13. var trimmedSplit = cleanText.split(" ");
  14. if (trimmedSplit.length > 1) {
  15. readability.weight += trimmedSplit.length;
  16. } else if (cleanText.length > 0) {
  17. readability.weight++;
  18. }
  19. return readability;
  20. }
  21. ];
  22.  
  23.  
  24. // Methods here resolve to true or false, being keep or remove. the caller does the maintenance
  25. var readabilityCalc = [
  26.  
  27. // simple calculator based on the depth vs maxDepth vs weight
  28. function depthVsMaxDepthVsWeight(readability) {
  29.  
  30. }
  31. ];
  32.  
  33. // storage for all elements with a weight > 0
  34. var acceptableStack = [];
  35.  
  36. var weightNodes = function(elem, depth) {
  37.  
  38. var readability = {weight:0,children:0,maxDepth:depth,depth:depth, depths : [depth], element: elem };
  39.  
  40. // find text node, apply textual calcs
  41. if (elem.nodeType === 3) {
  42.  
  43. var regularText = elem.textContent || e.innerText;
  44. var cleanText = trim(regularText, " \r\n\t").replace(/ [ ]+/g," ");
  45.  
  46. // run all of the nodes through the textual calculators
  47. for (var i=0;i<textualCalc.length; i++){
  48. readability = textualCalc[i](cleanText,regularText, readability);
  49. }
  50.  
  51. } else {
  52.  
  53. var currentChild = null;
  54. var childrenReadability = null;
  55.  
  56. if (elem.childNodes && elem.childNodes.length) {
  57.  
  58. // Keep a rolling tab of the number of children
  59. readability.children += elem.childNodes.length;
  60.  
  61. for (var child in elem.childNodes) {
  62. currentChild = elem.childNodes[child];
  63.  
  64. childrenReadability = weightNodes(elem.childNodes[child], depth+1);
  65.  
  66. // merge what is possible into the readability count for this node.
  67.  
  68. // weight
  69. readability.weight += childrenReadability.weight;
  70.  
  71. // max depth
  72. if (childrenReadability.maxDepth > readability.maxDepth) {
  73. readability.maxDepth = childrenReadability.maxDepth;
  74. }
  75.  
  76. // track children, possibly a sideeffect of depths, but oh well.
  77. readability.children += childrenReadability.children;
  78.  
  79. // Track the actual depths in a single array, sort of looks like sax
  80. var sax = 0;
  81. for (;sax<childrenReadability.depths.length;sax++) {
  82. readability.depths.push(childrenReadability.depths[sax]);
  83. }
  84. }
  85. }
  86. }
  87.  
  88. if (readability.weight > 0) {
  89. acceptableStack.push(readability);
  90. console.log(readability);
  91. }
  92. return readability;
  93. }
  94.  
  95. console.clear();
  96. console.log("running..");
  97. weightNodes(document.body,0);
  98. console.log("weighted");
  99. // run all of the nodes through the final calculator
  100. for (var i=0;i<readabilityCalc.length; i++){
  101.  
  102. for (var j=acceptableStack.length; j>-1; j--) {
  103. if (!(readabilityCalc[i])(acceptableStack[j]))
  104. {
  105. // remove element
  106. console.log("Removing element...");
  107. delete acceptableStack[j];
  108. }
  109. }
  110. }
  111.  
  112. // the remaining elements can be pushed to the screen!
  113.  
  114.  
  115. })();
Add Comment
Please, Sign In to add comment