2014-12-30 18:44:34 +03:00
|
|
|
// -*- 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";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [ "ReaderParent" ];
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
|
2016-08-16 22:44:15 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", "resource://gre/modules/PlacesUtils.jsm");
|
2014-12-30 18:44:34 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ReaderMode", "resource://gre/modules/ReaderMode.jsm");
|
2015-04-16 06:45:50 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "UITour", "resource:///modules/UITour.jsm");
|
2014-12-30 18:44:34 +03:00
|
|
|
|
2015-02-27 22:29:18 +03:00
|
|
|
const gStringBundle = Services.strings.createBundle("chrome://global/locale/aboutReader.properties");
|
2015-02-11 18:29:34 +03:00
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var ReaderParent = {
|
2017-04-23 23:27:35 +03:00
|
|
|
// Listeners are added in nsBrowserGlue.js
|
2016-12-30 02:34:54 +03:00
|
|
|
receiveMessage(message) {
|
2014-12-30 18:44:34 +03:00
|
|
|
switch (message.name) {
|
|
|
|
case "Reader:ArticleGet":
|
|
|
|
this._getArticle(message.data.url, message.target).then((article) => {
|
2015-01-24 02:23:21 +03:00
|
|
|
// Make sure the target browser is still alive before trying to send data back.
|
|
|
|
if (message.target.messageManager) {
|
2016-12-30 02:34:54 +03:00
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { article });
|
2015-01-24 02:23:21 +03:00
|
|
|
}
|
2015-07-17 15:30:42 +03:00
|
|
|
}, e => {
|
|
|
|
if (e && e.newURL) {
|
2016-07-13 11:14:34 +03:00
|
|
|
// Make sure the target browser is still alive before trying to send data back.
|
|
|
|
if (message.target.messageManager) {
|
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { newURL: e.newURL });
|
|
|
|
}
|
2015-07-17 15:30:42 +03:00
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:FaviconRequest": {
|
2015-03-18 23:22:00 +03:00
|
|
|
if (message.target.messageManager) {
|
|
|
|
let faviconUrl = PlacesUtils.promiseFaviconLinkUrl(message.data.url);
|
|
|
|
faviconUrl.then(function onResolution(favicon) {
|
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:FaviconReturn", {
|
|
|
|
url: message.data.url,
|
|
|
|
faviconUrl: favicon.path.replace(/^favicon:/, "")
|
|
|
|
})
|
|
|
|
},
|
|
|
|
function onRejection(reason) {
|
|
|
|
Cu.reportError("Error requesting favicon URL for about:reader content: " + reason);
|
|
|
|
}).catch(Cu.reportError);
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
case "Reader:UpdateReaderButton": {
|
|
|
|
let browser = message.target;
|
|
|
|
if (message.data && message.data.isArticle !== undefined) {
|
|
|
|
browser.isArticle = message.data.isArticle;
|
|
|
|
}
|
|
|
|
this.updateReaderButton(browser);
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
updateReaderButton(browser) {
|
2016-07-16 11:20:04 +03:00
|
|
|
let win = browser.ownerGlobal;
|
2015-01-03 04:20:12 +03:00
|
|
|
if (browser != win.gBrowser.selectedBrowser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let button = win.document.getElementById("reader-mode-button");
|
2015-03-21 03:35:55 +03:00
|
|
|
let command = win.document.getElementById("View:ReaderView");
|
2017-03-04 21:01:19 +03:00
|
|
|
let key = win.document.getElementById("key_toggleReaderMode");
|
2015-01-03 04:20:12 +03:00
|
|
|
if (browser.currentURI.spec.startsWith("about:reader")) {
|
|
|
|
button.setAttribute("readeractive", true);
|
|
|
|
button.hidden = false;
|
2015-03-21 03:35:55 +03:00
|
|
|
let closeText = gStringBundle.GetStringFromName("readerView.close");
|
|
|
|
button.setAttribute("tooltiptext", closeText);
|
|
|
|
command.setAttribute("label", closeText);
|
|
|
|
command.setAttribute("hidden", false);
|
2015-03-30 18:32:00 +03:00
|
|
|
command.setAttribute("accesskey", gStringBundle.GetStringFromName("readerView.close.accesskey"));
|
2016-07-30 05:13:56 +03:00
|
|
|
key.setAttribute("disabled", false);
|
2015-01-03 04:20:12 +03:00
|
|
|
} else {
|
|
|
|
button.removeAttribute("readeractive");
|
|
|
|
button.hidden = !browser.isArticle;
|
2015-03-21 03:35:55 +03:00
|
|
|
let enterText = gStringBundle.GetStringFromName("readerView.enter");
|
|
|
|
button.setAttribute("tooltiptext", enterText);
|
|
|
|
command.setAttribute("label", enterText);
|
|
|
|
command.setAttribute("hidden", !browser.isArticle);
|
2015-03-30 18:32:00 +03:00
|
|
|
command.setAttribute("accesskey", gStringBundle.GetStringFromName("readerView.enter.accesskey"));
|
2016-07-30 05:13:56 +03:00
|
|
|
key.setAttribute("disabled", !browser.isArticle);
|
2015-01-03 04:20:12 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
forceShowReaderIcon(browser) {
|
2015-04-13 22:12:57 +03:00
|
|
|
browser.isArticle = true;
|
|
|
|
this.updateReaderButton(browser);
|
|
|
|
},
|
|
|
|
|
2015-04-16 21:40:18 +03:00
|
|
|
buttonClick(event) {
|
2015-04-06 04:50:08 +03:00
|
|
|
if (event.button != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.toggleReaderMode(event);
|
|
|
|
},
|
|
|
|
|
2016-12-30 02:34:54 +03:00
|
|
|
toggleReaderMode(event) {
|
2016-07-16 11:20:04 +03:00
|
|
|
let win = event.target.ownerGlobal;
|
2015-03-13 06:06:37 +03:00
|
|
|
let browser = win.gBrowser.selectedBrowser;
|
2016-04-19 00:46:58 +03:00
|
|
|
browser.messageManager.sendAsyncMessage("Reader:ToggleReaderMode");
|
2015-01-03 04:20:12 +03:00
|
|
|
},
|
|
|
|
|
2014-12-30 18:44:34 +03:00
|
|
|
/**
|
2015-03-13 06:06:37 +03:00
|
|
|
* Gets an article for a given URL. This method will download and parse a document.
|
2014-12-30 18:44:34 +03:00
|
|
|
*
|
|
|
|
* @param url The article URL.
|
|
|
|
* @param browser The browser where the article is currently loaded.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
|
|
*/
|
|
|
|
_getArticle: Task.async(function* (url, browser) {
|
2015-04-11 00:41:14 +03:00
|
|
|
return yield ReaderMode.downloadAndParseDocument(url).catch(e => {
|
2015-07-17 15:30:42 +03:00
|
|
|
if (e && e.newURL) {
|
|
|
|
// Pass up the error so we can navigate the browser in question to the new URL:
|
|
|
|
throw e;
|
|
|
|
}
|
2015-04-11 00:41:14 +03:00
|
|
|
Cu.reportError("Error downloading and parsing document: " + e);
|
|
|
|
return null;
|
|
|
|
});
|
2015-03-13 06:06:37 +03:00
|
|
|
})
|
2014-12-30 18:44:34 +03:00
|
|
|
};
|