diff options
author | Moonchild <git-repo@palemoon.org> | 2019-05-06 14:17:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-06 14:17:55 +0000 |
commit | d0b65e8b38ddd1c9dde816941eccac5094957f72 (patch) | |
tree | cd8526eba923a1ce954c92f54466bb86399ac4a3 /toolkit/components/reader/Readerable.js | |
parent | 12d23fb00559d91cc1dc1bf4e74e30caabd22ad5 (diff) | |
parent | 5c350eebcda338152572aea64400a07db5ce2ff4 (diff) | |
download | UXP-d0b65e8b38ddd1c9dde816941eccac5094957f72.tar UXP-d0b65e8b38ddd1c9dde816941eccac5094957f72.tar.gz UXP-d0b65e8b38ddd1c9dde816941eccac5094957f72.tar.lz UXP-d0b65e8b38ddd1c9dde816941eccac5094957f72.tar.xz UXP-d0b65e8b38ddd1c9dde816941eccac5094957f72.zip |
Merge pull request #1080 from Ascrod/readerview
Reader Updates
Diffstat (limited to 'toolkit/components/reader/Readerable.js')
-rw-r--r-- | toolkit/components/reader/Readerable.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/toolkit/components/reader/Readerable.js b/toolkit/components/reader/Readerable.js new file mode 100644 index 000000000..cee8adc08 --- /dev/null +++ b/toolkit/components/reader/Readerable.js @@ -0,0 +1,96 @@ +// -*- 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.jsm. + +/* exported Readerable */ +/* import-globals-from Readability-readerable.js */ + +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; + +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +function isNodeVisible(node) { + return node.clientHeight > 0 && node.clientWidth > 0; +} + +var Readerable = { + DEBUG: 0, + + get isEnabledForParseOnLoad() { + return this.isEnabled || this.isForceEnabled; + }, + + log(msg) { + if (this.DEBUG) + dump("Reader: " + msg); + }, + + /** + * 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 instanceof doc.defaultView.HTMLDocument)) { + return false; + } + + let uri = Services.io.newURI(doc.location.href); + if (!this.shouldCheckUri(uri)) { + return false; + } + + return isProbablyReaderable(doc, isNodeVisible); + }, + + _blockedHosts: [ + "amazon.com", + "basilisk-browser.org", + "github.com", + "mail.google.com", + "palemoon.org", + "pinterest.com", + "reddit.com", + "twitter.com", + "youtube.com", + ], + + shouldCheckUri(uri, isBaseUri = false) { + if (!(uri.schemeIs("http") || uri.schemeIs("https"))) { + this.log("Not parsing URI scheme: " + uri.scheme); + return false; + } + + try { + uri.QueryInterface(Ci.nsIURL); + } catch (ex) { + // If this doesn't work, presumably the URL is not well-formed or something + return false; + } + // Sadly, some high-profile pages have false positives, so bail early for those: + let asciiHost = uri.asciiHost; + if (!isBaseUri && this._blockedHosts.some(blockedHost => asciiHost.endsWith(blockedHost))) { + return false; + } + + if (!isBaseUri && (!uri.filePath || uri.filePath == "/")) { + this.log("Not parsing home page: " + uri.spec); + return false; + } + + return true; + }, +}; + +XPCOMUtils.defineLazyPreferenceGetter( + Readerable, "isEnabled", "reader.parse-on-load.enabled", true); +XPCOMUtils.defineLazyPreferenceGetter( + Readerable, "isForceEnabled", "reader.parse-on-load.force-enabled", false); |