Readability-readerable.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* eslint-env es6:false */
  2. /* globals exports */
  3. /*
  4. * Copyright (c) 2010 Arc90 Inc
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /*
  19. * This code is heavily based on Arc90's readability.js (1.7.1) script
  20. * available at: http://code.google.com/p/arc90labs-readability
  21. */
  22. var REGEXPS = {
  23. // NOTE: These two regular expressions are duplicated in
  24. // Readability.js. Please keep both copies in sync.
  25. unlikelyCandidates: /-ad-|ai2html|banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|footer|gdpr|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i,
  26. okMaybeItsACandidate: /and|article|body|column|content|main|shadow/i,
  27. };
  28. function isNodeVisible(node) {
  29. // Have to null-check node.style to deal with SVG and MathML nodes.
  30. return (!node.style || node.style.display != "none") && !node.hasAttribute("hidden")
  31. && (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true");
  32. }
  33. /**
  34. * Decides whether or not the document is reader-able without parsing the whole thing.
  35. *
  36. * @return boolean Whether or not we suspect Readability.parse() will suceeed at returning an article object.
  37. */
  38. function isProbablyReaderable(doc, isVisible) {
  39. if (!isVisible) {
  40. isVisible = isNodeVisible;
  41. }
  42. var nodes = doc.querySelectorAll("p, pre");
  43. // Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
  44. // Some articles' DOM structures might look like
  45. // <div>
  46. // Sentences<br>
  47. // <br>
  48. // Sentences<br>
  49. // </div>
  50. var brNodes = doc.querySelectorAll("div > br");
  51. if (brNodes.length) {
  52. var set = new Set(nodes);
  53. [].forEach.call(brNodes, function(node) {
  54. set.add(node.parentNode);
  55. });
  56. nodes = Array.from(set);
  57. }
  58. var score = 0;
  59. // This is a little cheeky, we use the accumulator 'score' to decide what to return from
  60. // this callback:
  61. return [].some.call(nodes, function(node) {
  62. if (!isVisible(node))
  63. return false;
  64. var matchString = node.className + " " + node.id;
  65. if (REGEXPS.unlikelyCandidates.test(matchString) &&
  66. !REGEXPS.okMaybeItsACandidate.test(matchString)) {
  67. return false;
  68. }
  69. if (node.matches("li p")) {
  70. return false;
  71. }
  72. var textContentLength = node.textContent.trim().length;
  73. if (textContentLength < 140) {
  74. return false;
  75. }
  76. score += Math.sqrt(textContentLength - 140);
  77. if (score > 20) {
  78. return true;
  79. }
  80. return false;
  81. });
  82. }
  83. if (typeof exports === "object") {
  84. exports.isProbablyReaderable = isProbablyReaderable;
  85. }