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");
|
|
|
|
|
2015-04-24 21:29:05 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI", "resource:///modules/CustomizableUI.jsm");
|
2015-03-18 23:22:00 +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-03-13 09:04:56 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ReadingList", "resource:///modules/readinglist/ReadingList.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
|
|
|
|
2014-12-30 18:44:34 +03:00
|
|
|
let ReaderParent = {
|
|
|
|
|
|
|
|
MESSAGES: [
|
|
|
|
"Reader:AddToList",
|
2015-04-24 21:29:05 +03:00
|
|
|
"Reader:AddToPocket",
|
2014-12-30 18:44:34 +03:00
|
|
|
"Reader:ArticleGet",
|
|
|
|
"Reader:FaviconRequest",
|
|
|
|
"Reader:ListStatusRequest",
|
2015-04-28 01:29:18 +03:00
|
|
|
"Reader:PocketEnabledGet",
|
2014-12-30 18:44:34 +03:00
|
|
|
"Reader:RemoveFromList",
|
|
|
|
"Reader:Share",
|
|
|
|
"Reader:SystemUIVisibility",
|
2015-01-03 04:20:12 +03:00
|
|
|
"Reader:UpdateReaderButton",
|
2015-02-20 23:56:00 +03:00
|
|
|
"Reader:SetIntPref",
|
|
|
|
"Reader:SetCharPref",
|
2014-12-30 18:44:34 +03:00
|
|
|
],
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
|
|
|
|
for (let msg of this.MESSAGES) {
|
|
|
|
mm.addMessageListener(msg, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
|
|
|
switch (message.name) {
|
2015-04-24 21:29:05 +03:00
|
|
|
case "Reader:AddToList": {
|
2015-03-30 19:02:52 +03:00
|
|
|
let article = message.data.article;
|
2015-03-31 02:58:00 +03:00
|
|
|
ReadingList.getMetadataFromBrowser(message.target).then(function(metadata) {
|
|
|
|
if (metadata.previews.length > 0) {
|
|
|
|
article.preview = metadata.previews[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadingList.addItem({
|
|
|
|
url: article.url,
|
|
|
|
title: article.title,
|
|
|
|
excerpt: article.excerpt,
|
|
|
|
preview: article.preview
|
|
|
|
});
|
2015-03-30 19:02:52 +03:00
|
|
|
});
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
2015-04-24 21:29:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
case "Reader:AddToPocket": {
|
|
|
|
let doc = message.target.ownerDocument;
|
|
|
|
let pocketWidget = doc.getElementById("pocket-button");
|
|
|
|
let placement = CustomizableUI.getPlacementOfWidget("pocket-button");
|
|
|
|
if (placement) {
|
|
|
|
if (placement.area == CustomizableUI.AREA_PANEL) {
|
|
|
|
doc.defaultView.PanelUI.show().then(function() {
|
|
|
|
// The DOM node might not exist yet if the panel wasn't opened before.
|
|
|
|
pocketWidget = doc.getElementById("pocket-button");
|
|
|
|
pocketWidget.doCommand();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
pocketWidget.doCommand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
|
|
|
|
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) {
|
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { article: article });
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2015-04-28 01:29:18 +03:00
|
|
|
case "Reader:PocketEnabledGet": {
|
|
|
|
let pocketPlacement = CustomizableUI.getPlacementOfWidget("pocket-button");
|
|
|
|
let isPocketEnabled = pocketPlacement && pocketPlacement.area;
|
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:PocketEnabledData", { enabled: !!isPocketEnabled});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-30 18:44:34 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
case "Reader:ListStatusRequest":
|
2015-03-20 22:12:10 +03:00
|
|
|
ReadingList.hasItemForURL(message.data.url).then(inList => {
|
2015-03-13 09:04:56 +03:00
|
|
|
let mm = message.target.messageManager
|
|
|
|
// Make sure the target browser is still alive before trying to send data back.
|
|
|
|
if (mm) {
|
|
|
|
mm.sendAsyncMessage("Reader:ListStatusData",
|
2015-03-17 22:49:07 +03:00
|
|
|
{ inReadingList: inList, url: message.data.url });
|
2015-03-13 09:04:56 +03:00
|
|
|
}
|
|
|
|
});
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:RemoveFromList":
|
2015-03-13 09:04:56 +03:00
|
|
|
// We need to get the "real" item to delete it.
|
2015-03-20 22:12:10 +03:00
|
|
|
ReadingList.itemForURL(message.data.url).then(item => {
|
2015-03-13 09:04:56 +03:00
|
|
|
ReadingList.deleteItem(item)
|
|
|
|
});
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:Share":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:SystemUIVisibility":
|
|
|
|
// XXX: To implement.
|
|
|
|
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;
|
|
|
|
}
|
2015-02-20 23:56:00 +03:00
|
|
|
case "Reader:SetIntPref": {
|
|
|
|
if (message.data && message.data.name !== undefined) {
|
|
|
|
Services.prefs.setIntPref(message.data.name, message.data.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Reader:SetCharPref": {
|
|
|
|
if (message.data && message.data.name !== undefined) {
|
|
|
|
Services.prefs.setCharPref(message.data.name, message.data.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
updateReaderButton: function(browser) {
|
|
|
|
let win = browser.ownerDocument.defaultView;
|
|
|
|
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");
|
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"));
|
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"));
|
2015-01-03 04:20:12 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-04-13 22:12:57 +03:00
|
|
|
forceShowReaderIcon: function(browser) {
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2015-03-21 03:28:03 +03:00
|
|
|
toggleReaderMode: function(event) {
|
2015-01-03 04:20:12 +03:00
|
|
|
let win = event.target.ownerDocument.defaultView;
|
2015-03-13 06:06:37 +03:00
|
|
|
let browser = win.gBrowser.selectedBrowser;
|
|
|
|
let url = browser.currentURI.spec;
|
2015-02-05 05:27:39 +03:00
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
if (url.startsWith("about:reader")) {
|
2015-04-07 02:07:03 +03:00
|
|
|
let originalURL = ReaderMode.getOriginalUrl(url);
|
2015-02-05 05:27:39 +03:00
|
|
|
if (!originalURL) {
|
|
|
|
Cu.reportError("Error finding original URL for about:reader URL: " + url);
|
|
|
|
} else {
|
2015-02-24 07:44:27 +03:00
|
|
|
win.openUILinkIn(originalURL, "current", {"allowPinnedTabHostChange": true});
|
2015-02-05 05:27:39 +03:00
|
|
|
}
|
2015-01-03 04:20:12 +03:00
|
|
|
} else {
|
2015-03-13 06:06:37 +03:00
|
|
|
browser.messageManager.sendAsyncMessage("Reader:ParseDocument", { url: url });
|
2015-01-03 04:20:12 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-04-16 06:45:50 +03:00
|
|
|
/**
|
|
|
|
* Shows an info panel from the UITour for Reader Mode.
|
|
|
|
*
|
|
|
|
* @param browser The <browser> that the tour should be started for.
|
|
|
|
*/
|
|
|
|
showReaderModeInfoPanel(browser) {
|
|
|
|
let win = browser.ownerDocument.defaultView;
|
|
|
|
let targetPromise = UITour.getTarget(win, "readerMode-urlBar");
|
|
|
|
targetPromise.then(target => {
|
|
|
|
let browserBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties");
|
|
|
|
UITour.showInfo(win, browser.messageManager, target,
|
|
|
|
browserBundle.GetStringFromName("readerView.promo.firstDetectedArticle.title"),
|
|
|
|
browserBundle.GetStringFromName("readerView.promo.firstDetectedArticle.body"),
|
|
|
|
"chrome://browser/skin/reader-tour.png");
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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 => {
|
|
|
|
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
|
|
|
};
|