2019-05-10 01:05:23 +02:00
|
|
|
// https://github.com/mozilla/readability/tree/814f0a3884350b6f1adfdebb79ca3599e9806605
|
|
|
|
|
2019-05-10 00:41:52 +02:00
|
|
|
/* eslint-env es6:false */
|
|
|
|
/* globals exports */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Arc90 Inc
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code is heavily based on Arc90's readability.js (1.7.1) script
|
|
|
|
* available at: http://code.google.com/p/arc90labs-readability
|
|
|
|
*/
|
|
|
|
|
|
|
|
var REGEXPS = {
|
2019-07-30 09:35:42 +02:00
|
|
|
// NOTE: These two regular expressions are duplicated in
|
|
|
|
// Readability.js. Please keep both copies in sync.
|
|
|
|
unlikelyCandidates: /-ad-|ai2html|banner|breadcrumbs|combx|comment|community|cover-wrap|disqus|extra|foot|gdpr|header|legends|menu|related|remark|replies|rss|shoutbox|sidebar|skyscraper|social|sponsor|supplemental|ad-break|agegate|pagination|pager|popup|yom-remote/i,
|
|
|
|
okMaybeItsACandidate: /and|article|body|column|main|shadow/i,
|
2019-05-10 00:41:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function isNodeVisible(node) {
|
2019-07-30 09:35:42 +02:00
|
|
|
// Have to null-check node.style to deal with SVG and MathML nodes.
|
|
|
|
return (!node.style || node.style.display != 'none') && !node.hasAttribute('hidden');
|
2019-05-10 00:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decides whether or not the document is reader-able without parsing the whole thing.
|
|
|
|
*
|
|
|
|
* @return boolean Whether or not we suspect Readability.parse() will suceeed at returning an article object.
|
|
|
|
*/
|
|
|
|
function isProbablyReaderable(doc, isVisible) {
|
2019-07-30 09:35:42 +02:00
|
|
|
if (!isVisible) {
|
|
|
|
isVisible = isNodeVisible;
|
|
|
|
}
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
var nodes = doc.querySelectorAll('p, pre');
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
// Get <div> nodes which have <br> node(s) and append them into the `nodes` variable.
|
|
|
|
// Some articles' DOM structures might look like
|
|
|
|
// <div>
|
|
|
|
// Sentences<br>
|
|
|
|
// <br>
|
|
|
|
// Sentences<br>
|
|
|
|
// </div>
|
|
|
|
var brNodes = doc.querySelectorAll('div > br');
|
|
|
|
if (brNodes.length) {
|
|
|
|
var set = new Set(nodes);
|
|
|
|
[].forEach.call(brNodes, function(node) {
|
|
|
|
set.add(node.parentNode);
|
|
|
|
});
|
|
|
|
nodes = Array.from(set);
|
|
|
|
}
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
var score = 0;
|
|
|
|
// This is a little cheeky, we use the accumulator 'score' to decide what to return from
|
|
|
|
// this callback:
|
|
|
|
return [].some.call(nodes, function(node) {
|
|
|
|
if (!isVisible(node))
|
|
|
|
return false;
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
var matchString = node.className + ' ' + node.id;
|
|
|
|
if (REGEXPS.unlikelyCandidates.test(matchString) &&
|
2019-05-10 00:41:52 +02:00
|
|
|
!REGEXPS.okMaybeItsACandidate.test(matchString)) {
|
2019-07-30 09:35:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
if (node.matches('li p')) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
var textContentLength = node.textContent.trim().length;
|
|
|
|
if (textContentLength < 140) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
score += Math.sqrt(textContentLength - 140);
|
2019-05-10 00:41:52 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
if (score > 20) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-05-10 00:41:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
if (typeof exports === 'object') {
|
|
|
|
exports.isProbablyReaderable = isProbablyReaderable;
|
2019-05-10 00:41:52 +02:00
|
|
|
}
|