Bug 1162713 - Implement "Save Link to Pocket" context menu item. r=dolske

This commit is contained in:
Jared Wein 2015-05-14 14:04:05 -04:00
Родитель a270716d85
Коммит 5f69727d1f
8 изменённых файлов: 96 добавлений и 44 удалений

Просмотреть файл

@ -79,6 +79,10 @@
label="&saveLinkCmd.label;"
accesskey="&saveLinkCmd.accesskey;"
oncommand="gContextMenu.saveLink();"/>
<menuitem id="context-savelinktopocket"
label="&saveLinkToPocketCmd.label;"
accesskey="&saveLinkToPocketCmd.accesskey;"
oncommand="gContextMenu.saveLinkToPocket();"/>
<menu id="context-marklinkMenu" label="&social.marklinkMenu.label;"
accesskey="&social.marklinkMenu.accesskey;">
<menupopup/>
@ -269,7 +273,7 @@
<menuitem id="context-pocket"
label="&saveToPocketCmd.label;"
accesskey="&saveToPocketCmd.accesskey;"
oncommand="gContextMenu.saveToPocket();"/>
oncommand="gContextMenu.savePageToPocket();"/>
<menu id="context-markpageMenu" label="&social.markpageMenu.label;"
accesskey="&social.markpageMenu.accesskey;">
<menupopup/>

Просмотреть файл

@ -11,4 +11,6 @@ pocket-button.tooltiptext = Bei Pocket speichern
# From browser-pocket.dtd
saveToPocketCmd.label = Seite bei Pocket speichern
saveToPocketCmd.accesskey = k
saveLinkToPocketCmd.label = Link in Pocket speichern
saveLinkToPocketCmd.accesskey = o
pocketMenuitem.label = Pocket-Liste anzeigen

Просмотреть файл

@ -11,4 +11,6 @@ pocket-button.tooltiptext = Guardar en Pocket
# From browser-pocket.dtd
saveToPocketCmd.label = Guardar página en Pocket
saveToPocketCmd.accesskey = k
saveLinkToPocketCmd.label = Guardar enlace en Pocket
saveLinkToPocketCmd.accesskey = k
pocketMenuitem.label = Ver lista de Pocket

Просмотреть файл

@ -11,4 +11,6 @@ pocket-button.tooltiptext = Pocket に保存
# From browser-pocket.dtd
saveToPocketCmd.label = Pocket にページを保存
saveToPocketCmd.accesskey = k
saveLinkToPocketCmd.label = Pocket にリンクを保存
saveLinkToPocketCmd.accesskey = o
pocketMenuitem.label = Pocket のマイリストを表示

Просмотреть файл

@ -11,4 +11,6 @@ pocket-button.tooltiptext = Сохранить в Pocket
# From browser-pocket.dtd
saveToPocketCmd.label = Сохранить страницу в Pocket
saveToPocketCmd.accesskey = х
saveLinkToPocketCmd.label = Сохранить ссылку в Pocket
saveLinkToPocketCmd.accesskey = P
pocketMenuitem.label = Показать список Pocket

Просмотреть файл

@ -7,4 +7,6 @@
<!ENTITY saveToPocketCmd.label "Save Page to Pocket">
<!ENTITY saveToPocketCmd.accesskey "k">
<!ENTITY saveLinkToPocketCmd.label "Save Link to Pocket">
<!ENTITY saveLinkToPocketCmd.accesskey "o">
<!ENTITY pocketMenuitem.label "View Pocket List">

Просмотреть файл

@ -6,6 +6,12 @@
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
Components.utils.import("resource://gre/modules/InlineSpellChecker.jsm");
Components.utils.import("resource://gre/modules/BrowserUtils.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
"resource:///modules/CustomizableUI.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Pocket",
"resource:///modules/Pocket.jsm");
var gContextMenuContentData = null;
@ -178,36 +184,47 @@ nsContextMenu.prototype = {
CastingApps.getServicesForVideo(this.target).length > 0;
this.setItemAttr("context-castvideo", "disabled", !shouldShowCast);
let canPocket = false;
if (shouldShow && window.gBrowser &&
this.browser.getTabBrowser() == window.gBrowser) {
let uri = this.browser.currentURI;
canPocket =
CustomizableUI.getPlacementOfWidget("pocket-button") &&
(uri.schemeIs("http") || uri.schemeIs("https") ||
(uri.schemeIs("about") && ReaderMode.getOriginalUrl(uri.spec)));
if (canPocket) {
let locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).
getSelectedLocale("browser");
if (locale != "en-US") {
if (locale == "ja-JP-mac")
locale = "ja";
let url = "chrome://browser/content/browser-pocket-" + locale + ".properties";
let bundle = Services.strings.createBundle(url);
let item = document.getElementById("context-pocket");
try {
item.setAttribute("label", bundle.GetStringFromName("saveToPocketCmd.label"));
item.setAttribute("accesskey", bundle.GetStringFromName("saveToPocketCmd.accesskey"));
} catch (err) {
// GetStringFromName throws when the bundle doesn't exist. In that
// case, the item will retain the browser-pocket.dtd en-US string that
// it has in the markup.
}
this.initPocketItems();
},
initPocketItems: function CM_initPocketItems() {
var showSaveCurrentPageToPocket = !(this.onTextInput || this.onLink ||
this.isContentSelected || this.onImage ||
this.onCanvas || this.onVideo || this.onAudio);
let targetURI = (this.onSaveableLink || this.onPlainTextLink) ? this.linkURI : this.browser.currentURI;
let canPocket = CustomizableUI.getPlacementOfWidget("pocket-button") &&
window.pktApi && window.pktApi.isUserLoggedIn();
canPocket = canPocket && (targetURI.schemeIs("http") || targetURI.schemeIs("https") ||
(targetURI.schemeIs("about") && ReaderMode.getOriginalUrl(targetURI.spec)));
canPocket = canPocket && window.gBrowser && this.browser.getTabBrowser() == window.gBrowser;
if (canPocket) {
let locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).
getSelectedLocale("browser");
if (locale != "en-US") {
if (locale == "ja-JP-mac")
locale = "ja";
let url = "chrome://browser/content/browser-pocket-" + locale + ".properties";
let bundle = Services.strings.createBundle(url);
let saveToPocketItem = document.getElementById("context-pocket");
let saveLinkToPocketItem = document.getElementById("context-savelinktopocket");
try {
saveToPocketItem.setAttribute("label", bundle.GetStringFromName("saveToPocketCmd.label"));
saveToPocketItem.setAttribute("accesskey", bundle.GetStringFromName("saveToPocketCmd.accesskey"));
saveLinkToPocketItem.setAttribute("label", bundle.GetStringFromName("saveLinkToPocketCmd.label"));
saveLinkToPocketItem.setAttribute("accesskey", bundle.GetStringFromName("saveLinkToPocketCmd.accesskey"));
} catch (err) {
// GetStringFromName throws when the bundle doesn't exist. In that
// case, the item will retain the browser-pocket.dtd en-US string that
// it has in the markup.
}
}
}
this.showItem("context-pocket", canPocket && window.pktApi && window.pktApi.isUserLoggedIn());
this.showItem("context-pocket", canPocket && showSaveCurrentPageToPocket);
let showSaveLinkToPocket = canPocket && !showSaveCurrentPageToPocket &&
(this.onSaveableLink || this.onPlainTextLink);
this.showItem("context-savelinktopocket", showSaveLinkToPocket);
},
initViewItems: function CM_initViewItems() {
@ -1661,20 +1678,12 @@ nsContextMenu.prototype = {
saveDocument(this.browser.contentDocumentAsCPOW);
},
saveToPocket: function CM_saveToPocket() {
let pocketWidget = document.getElementById("pocket-button");
let placement = CustomizableUI.getPlacementOfWidget("pocket-button");
if (!placement)
return;
saveLinkToPocket: function CM_saveLinkToPocket() {
Pocket.savePage(this.browser, this.linkURL);
},
if (placement.area == CustomizableUI.AREA_PANEL) {
PanelUI.show().then(function() {
pocketWidget = document.getElementById("pocket-button");
pocketWidget.doCommand();
});
} else {
pocketWidget.doCommand();
}
savePageToPocket: function CM_saveToPocket() {
Pocket.savePage(this.browser, this.browser.currentURI.spec, this.browser.contentTitle);
},
printFrame: function CM_printFrame() {

Просмотреть файл

@ -27,14 +27,21 @@ let Pocket = {
let window = document.defaultView;
let iframe = document.getElementById('pocket-panel-iframe');
let urlToSave = Pocket._urlToSave;
let titleToSave = Pocket._titleToSave;
Pocket._urlToSave = null;
Pocket._titleToSave = null;
// ViewShowing fires immediately before it creates the contents,
// in lieu of an AfterViewShowing event, just spin the event loop.
window.setTimeout(function() {
window.pktUI.pocketButtonOnCommand();
if (urlToSave) {
window.pktUI.tryToSaveUrl(urlToSave, titleToSave);
} else {
window.pktUI.pocketButtonOnCommand();
}
if (iframe.contentDocument &&
iframe.contentDocument.readyState == "complete")
{
iframe.contentDocument.readyState == "complete") {
window.pktUI.pocketPanelDidShow();
} else {
// iframe didn't load yet. This seems to always be the case when in
@ -82,4 +89,26 @@ let Pocket = {
}
}
},
_urlToSave: null,
_titleToSave: null,
savePage(browser, url, title) {
let document = browser.ownerDocument;
let pocketWidget = document.getElementById("pocket-button");
let placement = CustomizableUI.getPlacementOfWidget("pocket-button");
if (!placement)
return;
this._urlToSave = url;
this._titleToSave = title;
if (placement.area == CustomizableUI.AREA_PANEL) {
let win = document.defaultView;
win.PanelUI.show().then(function() {
pocketWidget = document.getElementById("pocket-button");
pocketWidget.doCommand();
});
} else {
pocketWidget.doCommand();
}
},
};