1
0

Readability-readerable.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 and node.className.indexOf to deal with SVG and MathML nodes.
  30. return (!node.style || node.style.display != "none")
  31. && !node.hasAttribute("hidden")
  32. //check for "fallback-image" so that wikimedia math images are displayed
  33. && (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true" || (node.className && node.className.indexOf && node.className.indexOf("fallback-image") !== -1));
  34. }
  35. /**
  36. * Decides whether or not the document is reader-able without parsing the whole thing.
  37. *
  38. * @return boolean Whether or not we suspect Readability.parse() will suceeed at returning an article object.
  39. */
  40. function isProbablyReaderable(doc, isVisible) {
  41. if (!isVisible) {
  42. isVisible = isNodeVisible;
  43. }
  44. var nodes = doc.querySelectorAll("p, pre");
  45. // Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
  46. // Some articles' DOM structures might look like
  47. // <div>
  48. // Sentences<br>
  49. // <br>
  50. // Sentences<br>
  51. // </div>
  52. var brNodes = doc.querySelectorAll("div > br");
  53. if (brNodes.length) {
  54. var set = new Set(nodes);
  55. [].forEach.call(brNodes, function(node) {
  56. set.add(node.parentNode);
  57. });
  58. nodes = Array.from(set);
  59. }
  60. var score = 0;
  61. // This is a little cheeky, we use the accumulator 'score' to decide what to return from
  62. // this callback:
  63. return [].some.call(nodes, function(node) {
  64. if (!isVisible(node))
  65. return false;
  66. var matchString = node.className + " " + node.id;
  67. if (REGEXPS.unlikelyCandidates.test(matchString) &&
  68. !REGEXPS.okMaybeItsACandidate.test(matchString)) {
  69. return false;
  70. }
  71. if (node.matches("li p")) {
  72. return false;
  73. }
  74. var textContentLength = node.textContent.trim().length;
  75. if (textContentLength < 140) {
  76. return false;
  77. }
  78. score += Math.sqrt(textContentLength - 140);
  79. if (score > 20) {
  80. return true;
  81. }
  82. return false;
  83. });
  84. }
  85. if (typeof exports === "object") {
  86. exports.isProbablyReaderable = isProbablyReaderable;
  87. }