Fix "Save to Zotero" in download overlay

This commit is contained in:
Dan Stillman 2015-11-10 15:52:09 -05:00
parent 8197fa4ce8
commit 790d6d3dd6
2 changed files with 44 additions and 39 deletions

View file

@ -49,7 +49,7 @@ var Zotero_DownloadOverlay = new function() {
*
* @return {Boolean} True if an item was saved, false if we were not supposed to save
*/
this.handleSave = function() {
this.handleSave = Zotero.Promise.coroutine(function* () {
if(!document.getElementById('zotero-radio').selected) return false;
var retrieveMetadata = document.getElementById('zotero-recognizePDF').selected;
@ -72,6 +72,7 @@ var Zotero_DownloadOverlay = new function() {
else {
Zotero.debug("Cannot save files to library " + itemGroup.ref.libraryID
+ " -- saving to personal library instead", 2);
libraryID = Zotero.Libraries.userLibraryID;
}
} catch(e) {
Zotero.debug(e, 1);
@ -80,34 +81,10 @@ var Zotero_DownloadOverlay = new function() {
var recognizePDF = document.getElementById('zotero-recognizePDF').checked
&& !document.getElementById('zotero-recognizePDF').hidden
&& !document.getElementById('zotero-recognizePDF').disabled;
var contentType = dialog.mLauncher.MIMEInfo.MIMEType;
// set up callback
var callback = function(item) {
if(!win) return;
if(item) {
progressWin.addLines([item.getDisplayTitle()], [item.getImageSrc()]);
progressWin.startCloseTimer();
if(collection) collection.addItem(item.id);
} else {
progressWin.addDescription(Zotero.getString("save.link.error"));
progressWin.startCloseTimer(8000);
}
if(recognizePDF) {
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
timer.init(function() {
try {
if(item.getFile()) {
timer.cancel();
var recognizer = new win.Zotero_RecognizePDF.ItemRecognizer();
recognizer.recognizeItems([item]);
}
} catch(e) { dump(e.toSource()) };
}, 1000, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
}
};
// mimic dialog cancellation
dialog.onCancel();
// show progress dialog
var progressWin = new Zotero.ProgressWindow();
@ -115,15 +92,44 @@ var Zotero_DownloadOverlay = new function() {
progressWin.show();
// perform import
Zotero.Attachments.importFromURL(url, false, false, false,
collection ? [collection.id] : [], dialog.mLauncher.MIMEInfo.MIMEType,
libraryID, callback);
try {
var item = yield Zotero.Attachments.importFromURL({
libraryID,
url,
collections: collection ? [collection.id] : [],
contentType
})
}
catch (e) {
if (!win) return;
progressWin.addDescription(Zotero.getString("save.link.error"));
progressWin.startCloseTimer(8000);
Zotero.logError(e);
return false;
}
// mimic dialog cancellation
dialog.onCancel();
if(!win) return;
progressWin.addLines([item.getDisplayTitle()], [item.getImageSrc()]);
progressWin.startCloseTimer();
if(collection) collection.addItem(item.id);
if(recognizePDF) {
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
timer.init(function() {
try {
if (item && item.getFile()) {
timer.cancel();
var recognizer = new win.Zotero_RecognizePDF.ItemRecognizer();
recognizer.recognizeItems([item]);
}
} catch(e) { dump(e.toSource()) };
}, 1000, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
}
return true;
};
});
/**
* Called when mode in dialog has been changed
@ -188,9 +194,9 @@ var Zotero_DownloadOverlay = new function() {
// Hook in event listener to ondialogaccept
document.documentElement.setAttribute('ondialogaccept',
'if(!Zotero_DownloadOverlay.handleSave()) { '
'Zotero_DownloadOverlay.handleSave().then(function (saved) { if (!saved) {'
+ document.documentElement.getAttribute('ondialogaccept')
+'}');
+'}})');
// Hook in event listener for mode change
var radios = document.getElementById('mode').

View file

@ -221,7 +221,7 @@ Zotero.Attachments = new function(){
/**
* @param {Object} options - 'url', 'parentItemID', 'collections', 'title',
* @param {Object} options - 'libraryID', 'url', 'parentItemID', 'collections', 'title',
* 'fileBaseName', 'contentType', 'cookieSandbox'
* @return {Promise<Zotero.Item>} - A promise for the created attachment item
*/
@ -247,8 +247,7 @@ Zotero.Attachments = new function(){
var urlRe = /^https?:\/\/[^\s]*$/;
var matches = urlRe.exec(url);
if (!matches) {
Components.utils.reportError("Invalid URL '" + url + "' in Zotero.Attachments.importFromURL()");
return false;
throw new Error("Invalid URL '" + url + "'");
}
// Save using a hidden browser