spb/zyquo-atlas Public License
The AI-native macOS web browser — every surface, intelligent.
Swift 75.2%
JavaScript 22%
Shell 2%
Makefile 0.9%
1/*2 * Copyright (c) 2010 Arc90 Inc3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617/*18 * This code is heavily based on Arc90's readability.js (1.7.1) script19 * available at: http://code.google.com/p/arc90labs-readability20 */2122var REGEXPS = {23 // NOTE: These two regular expressions are duplicated in24 // Readability.js. Please keep both copies in sync.25 unlikelyCandidates:26 /-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,27 okMaybeItsACandidate: /and|article|body|column|content|main|mathjax|shadow/i,28};2930function isNodeVisible(node) {31 // Have to null-check node.style and node.className.includes to deal with SVG and MathML nodes.32 return (33 (!node.style || node.style.display != "none") &&34 !node.hasAttribute("hidden") &&35 //check for "fallback-image" so that wikimedia math images are displayed36 (!node.hasAttribute("aria-hidden") ||37 node.getAttribute("aria-hidden") != "true" ||38 (node.className &&39 node.className.includes &&40 node.className.includes("fallback-image")))41 );42}4344/**45 * Decides whether or not the document is reader-able without parsing the whole thing.46 * @param {Object} options Configuration object.47 * @param {number} [options.minContentLength=140] The minimum node content length used to decide if the document is readerable.48 * @param {number} [options.minScore=20] The minumum cumulated 'score' used to determine if the document is readerable.49 * @param {Function} [options.visibilityChecker=isNodeVisible] The function used to determine if a node is visible.50 * @return {boolean} Whether or not we suspect Readability.parse() will suceeed at returning an article object.51 */52function isProbablyReaderable(doc, options = {}) {53 // For backward compatibility reasons 'options' can either be a configuration object or the function used54 // to determine if a node is visible.55 if (typeof options == "function") {56 options = { visibilityChecker: options };57 }5859 var defaultOptions = {60 minScore: 20,61 minContentLength: 140,62 visibilityChecker: isNodeVisible,63 };64 options = Object.assign(defaultOptions, options);6566 var nodes = doc.querySelectorAll("p, pre, article");6768 // Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.69 // Some articles' DOM structures might look like70 // <div>71 // Sentences<br>72 // <br>73 // Sentences<br>74 // </div>75 var brNodes = doc.querySelectorAll("div > br");76 if (brNodes.length) {77 var set = new Set(nodes);78 [].forEach.call(brNodes, function (node) {79 set.add(node.parentNode);80 });81 nodes = Array.from(set);82 }8384 var score = 0;85 // This is a little cheeky, we use the accumulator 'score' to decide what to return from86 // this callback:87 return [].some.call(nodes, function (node) {88 if (!options.visibilityChecker(node)) {89 return false;90 }9192 var matchString = node.className + " " + node.id;93 if (94 REGEXPS.unlikelyCandidates.test(matchString) &&95 !REGEXPS.okMaybeItsACandidate.test(matchString)96 ) {97 return false;98 }99100 if (node.matches("li p")) {101 return false;102 }103104 var textContentLength = node.textContent.trim().length;105 if (textContentLength < options.minContentLength) {106 return false;107 }108109 score += Math.sqrt(textContentLength - options.minContentLength);110111 if (score > options.minScore) {112 return true;113 }114 return false;115 });116}117118if (typeof module === "object") {119 /* eslint-disable-next-line no-redeclare */120 /* global module */121 module.exports = isProbablyReaderable;122}123