diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/reader/Readerable.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/reader/Readerable.js')
-rw-r--r-- | toolkit/components/reader/Readerable.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/toolkit/components/reader/Readerable.js b/toolkit/components/reader/Readerable.js new file mode 100644 index 0000000000..e558642fe3 --- /dev/null +++ b/toolkit/components/reader/Readerable.js @@ -0,0 +1,93 @@ +// -*- indent-tabs-mode: nil; js-indent-level: 2 -*- +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +// This file and Readability-readerable.js are merged together into +// Readerable.sys.mjs. + +/* exported Readerable */ +/* import-globals-from Readability-readerable.js */ + +const { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); + +var Readerable = { + get isEnabledForParseOnLoad() { + return this.isEnabled; + }, + + /** + * Decides whether or not a document is reader-able without parsing the whole thing. + * + * @param doc A document to parse. + * @return boolean Whether or not we should show the reader mode button. + */ + isProbablyReaderable(doc) { + // Only care about 'real' HTML documents: + if ( + doc.mozSyntheticDocument || + !doc.defaultView.HTMLDocument.isInstance(doc) + ) { + return false; + } + + let uri = Services.io.newURI(doc.location.href); + if (!this.shouldCheckUri(uri)) { + return false; + } + + return isProbablyReaderable(doc, this._isNodeVisible); + }, + + _isNodeVisible(node) { + return node.clientHeight > 0 && node.clientWidth > 0; + }, + + _blockedHosts: [ + "amazon.com", + "github.com", + "mail.google.com", + "pinterest.com", + "reddit.com", + "twitter.com", + "youtube.com", + ], + + shouldCheckUri(uri, isBaseUri = false) { + if (!["http", "https", "file", "moz-nullprincipal"].includes(uri.scheme)) { + return false; + } + + if (!isBaseUri && uri.scheme.startsWith("http")) { + // Sadly, some high-profile pages have false positives, so bail early for those: + let { host } = uri; + if (this._blockedHosts.some(blockedHost => host.endsWith(blockedHost))) { + // Allow github on non-project pages + if ( + host == "github.com" && + !uri.filePath.includes("/projects") && + !uri.filePath.includes("/issues") + ) { + return true; + } + return false; + } + + if (uri.filePath == "/") { + return false; + } + } + + return true; + }, +}; + +XPCOMUtils.defineLazyPreferenceGetter( + Readerable, + "isEnabled", + "reader.parse-on-load.enabled", + true +); |