diff options
author | Jan-Michael Brummer <jan.brummer@tabos.org> | 2020-06-11 07:06:49 +0200 |
---|---|---|
committer | Jan-Michael Brummer <jan.brummer@tabos.org> | 2020-06-14 16:15:46 +0200 |
commit | cbf9765da58dd13b6ef219fcbcb7f02265c81572 (patch) | |
tree | a0c753905c26989a13648f28bb8dd95814f0e58b /third-party | |
parent | 3865933c86d9759581dabeb8e7bedd537c55226b (diff) | |
download | epiphany-cbf9765da58dd13b6ef219fcbcb7f02265c81572.tar.gz |
Add reader mode handler
Fixes: https://gitlab.gnome.org/GNOME/epiphany/-/issues/50
Diffstat (limited to 'third-party')
-rw-r--r-- | third-party/readability/README.epiphany | 10 | ||||
-rw-r--r-- | third-party/readability/Readability-readerable.js | 103 | ||||
-rw-r--r-- | third-party/readability/readability.gresource.xml | 1 |
3 files changed, 112 insertions, 2 deletions
diff --git a/third-party/readability/README.epiphany b/third-party/readability/README.epiphany index 731426a81..1273a0529 100644 --- a/third-party/readability/README.epiphany +++ b/third-party/readability/README.epiphany @@ -5,14 +5,20 @@ This directory contains an official readability.js release version, distributed ## Update process $ wget https://raw.githubusercontent.com/mozilla/readability/master/Readability.js +$ wget https://raw.githubusercontent.com/mozilla/readability/master/Readability-readerable.js -Copy Readability.js to <epiphany-source>/third-party/readability/ +Copy Readability.js and Readability-readerable.js to <epiphany-source>/third-party/readability/ -# Added the following to the bottom of the js file: +# Added the following to the bottom of the Readability.js file: // Added for Epiphany var documentClone = document.cloneNode(true); reader = new Readability(documentClone); reader.parse(); +# Added the following to the bottom of the Readability-readerable.js file: + +// Added for Epiphany +isProbablyReaderable(document, false); + # Documentation created by Jan-Michael Brummer <jan.brummer@tabos.org>
\ No newline at end of file diff --git a/third-party/readability/Readability-readerable.js b/third-party/readability/Readability-readerable.js new file mode 100644 index 000000000..4d4001c6e --- /dev/null +++ b/third-party/readability/Readability-readerable.js @@ -0,0 +1,103 @@ +/* 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 = { + // 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|footer|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|content|main|shadow/i, +}; + +function isNodeVisible(node) { + // Have to null-check node.style and node.className.indexOf to deal with SVG and MathML nodes. + return (!node.style || node.style.display != "none") + && !node.hasAttribute("hidden") + //check for "fallback-image" so that wikimedia math images are displayed + && (!node.hasAttribute("aria-hidden") || node.getAttribute("aria-hidden") != "true" || (node.className && node.className.indexOf && node.className.indexOf("fallback-image") !== -1)); +} + +/** + * 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) { + if (!isVisible) { + isVisible = isNodeVisible; + } + + var nodes = doc.querySelectorAll("p, pre"); + + // 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); + } + + 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; + + var matchString = node.className + " " + node.id; + if (REGEXPS.unlikelyCandidates.test(matchString) && + !REGEXPS.okMaybeItsACandidate.test(matchString)) { + return false; + } + + if (node.matches("li p")) { + return false; + } + + var textContentLength = node.textContent.trim().length; + if (textContentLength < 140) { + return false; + } + + score += Math.sqrt(textContentLength - 140); + + if (score > 20) { + return true; + } + return false; + }); +} + +if (typeof exports === "object") { + exports.isProbablyReaderable = isProbablyReaderable; +} + +// Added for Epiphany +isProbablyReaderable(document, false); diff --git a/third-party/readability/readability.gresource.xml b/third-party/readability/readability.gresource.xml index b3216fd5f..ff4dc2b2d 100644 --- a/third-party/readability/readability.gresource.xml +++ b/third-party/readability/readability.gresource.xml @@ -2,6 +2,7 @@ <gresources> <gresource prefix="/org/gnome/epiphany/readability"> <file compressed="true">Readability.js</file> + <file compressed="true">Readability-readerable.js</file> <file compressed="true">reader.css</file> </gresource> </gresources> |