From 0d145cd47b0badc4b20ced8c3a1b14ea9ab4d49d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 7 Dec 2006 13:39:30 +0000 Subject: [PATCH] Closes #416, Right-click to add attachment Added "Save Link As Zotero Snapshot" and "Save Image As Zotero Snapshot" options to the browser content context menu where appropriate Other fixes: - Implemented standalone image and plugin snapshots the right way (as opposed to the fairly broken way from yesterday) - Only natively handled files are loaded into a hidden browser when using importFromURL() -- plugin files are now saved directly with saveURI() - indexDocument() doesn't try to index non-text files Notes: - There's no feedback when saving large files, which will likely be a bit confusing for users -- one option would be to put the transfer into the downloads window, though that's a little weird. - I suspect this will fix the reported JSTOR PDF download issue (http://forums.zotero.org/discussion/217/), though I don't currently have a way of testing it. --- chrome/content/zotero/overlay.js | 25 ++++++++++++++ chrome/content/zotero/overlay.xul | 6 ++++ chrome/content/zotero/xpcom/attachments.js | 40 ++++++++++++++-------- chrome/content/zotero/xpcom/fulltext.js | 10 ++++++ chrome/content/zotero/xpcom/mime.js | 14 ++++++++ chrome/content/zotero/xpcom/zotero.js | 34 +++--------------- chrome/locale/en-US/zotero/zotero.dtd | 2 ++ 7 files changed, 87 insertions(+), 44 deletions(-) diff --git a/chrome/content/zotero/overlay.js b/chrome/content/zotero/overlay.js index 183ca8523..f6d7d678b 100644 --- a/chrome/content/zotero/overlay.js +++ b/chrome/content/zotero/overlay.js @@ -874,10 +874,35 @@ var ZoteroPane = new function() } } + var menuitem = document.getElementById("zotero-context-save-link-as-snapshot"); + if (menuitem) { + if (window.gContextMenu.onLink) { + menuitem.hidden = false; + showing = true; + } + else { + menuitem.hidden = true; + } + } + + var menuitem = document.getElementById("zotero-context-save-image-as-snapshot"); + if (menuitem) { + // Not using window.gContextMenu.hasBGImage -- if the user wants it, + // they can use the Firefox option to view and then import from there + if (window.gContextMenu.onImage) { + menuitem.hidden = false; + showing = true; + } + else { + menuitem.hidden = true; + } + } + var separator = document.getElementById("zotero-context-separator"); separator.hidden = !showing; } + function newNote(popup, parent, text) { if (!popup) diff --git a/chrome/content/zotero/overlay.xul b/chrome/content/zotero/overlay.xul index 38060be2c..1dd3c3fd8 100644 --- a/chrome/content/zotero/overlay.xul +++ b/chrome/content/zotero/overlay.xul @@ -52,6 +52,12 @@ + + diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js index f908c1cd5..e49b34323 100644 --- a/chrome/content/zotero/xpcom/attachments.js +++ b/chrome/content/zotero/xpcom/attachments.js @@ -178,9 +178,9 @@ Zotero.Attachments = new function(){ nsIURL.spec = url; var ext = nsIURL.fileExtension; - // If we can load this internally, use a hidden browser (so we can - // get the charset and title) - if (Zotero.MIME.hasInternalHandler(mimeType, ext)){ + // If we can load this natively, use a hidden browser (so we can + // get the charset and title and index the document) + if (Zotero.MIME.hasNativeHandler(mimeType, ext)){ var browser = Zotero.Browser.createHiddenBrowser(); browser.addEventListener("pageshow", function(){ Zotero.Attachments.importFromDocument(browser.contentDocument, sourceItemID); @@ -254,7 +254,7 @@ Zotero.Attachments = new function(){ // leaving the transaction open if the callback never triggers Zotero.DB.commitTransaction(); - wbp.saveURI(nsIURL, null, null, null, null, file); + wbp.saveURI(nsIURL, null, null, null, null, file); } catch (e){ Zotero.DB.rollbackTransaction(); @@ -340,15 +340,17 @@ Zotero.Attachments = new function(){ var title = forceTitle ? forceTitle : document.title; var mimeType = document.contentType; var charsetID = Zotero.CharacterSets.getID(document.characterSet); - var hasNativeHandler = Zotero.MIME.hasNativeHandler(mimeType, _getExtensionFromURL(url)) - // TODO: make this work -- with local plugin files, onStateChange in the - // nsIWebBrowserPersist's nsIWebProgressListener never completes and - // onProgressChange returns -1 for maxTotal, which prevents it from - // triggering the callback. - if (!hasNativeHandler && url.substr(0, 4) == 'file') { - Zotero.debug('Import of loaded files from plugins is not supported'); - return false; + if (!forceTitle) { + // Remove e.g. " - Scaled (-17%)" from end of images saved from links, + // though I'm not sure why it's getting added to begin with + if (mimeType.indexOf('image/') === 0) { + title = title.replace(/(.+ \([^,]+, [0-9]+x[0-9]+[^\)]+\)) - .+/, "$1" ); + } + // If not native type, strip mime type data in parens + else if (!Zotero.MIME.hasNativeHandler(mimeType, _getExtensionFromURL(url))) { + title = title.replace(/(.+) \([a-z]+\/[^\)]+\)/, "$1" ); + } } const nsIWBP = Components.interfaces.nsIWebBrowserPersist; @@ -434,13 +436,23 @@ Zotero.Attachments = new function(){ } Zotero.Fulltext.indexDocument(document, itemID); - }, !hasNativeHandler); + }); // The attachment is still incomplete here, but we can't risk // leaving the transaction open if the callback never triggers Zotero.DB.commitTransaction(); - wbp.saveDocument(document, file, destDir, mimeType, encodingFlags, false); + if (Zotero.MIME.isDocumentType(mimeType)) { + Zotero.debug('Saving with saveDocument()'); + wbp.saveDocument(document, file, destDir, mimeType, encodingFlags, false); + } + else { + Zotero.debug('Saving with saveURI()'); + var ioService = Components.classes["@mozilla.org/network/io-service;1"] + .getService(Components.interfaces.nsIIOService); + var nsIURL = ioService.newURI(url, null, null); + wbp.saveURI(nsIURL, null, null, null, null, file); + } } catch (e) { Zotero.DB.rollbackTransaction(); diff --git a/chrome/content/zotero/xpcom/fulltext.js b/chrome/content/zotero/xpcom/fulltext.js index c54fd8020..06ba574cc 100644 --- a/chrome/content/zotero/xpcom/fulltext.js +++ b/chrome/content/zotero/xpcom/fulltext.js @@ -159,6 +159,16 @@ Zotero.Fulltext = new function(){ Zotero.debug("Indexing document '" + document.title + "'"); + if (document.contentType.indexOf('text/') !== 0) { + Zotero.debug('File is not text in indexDocument()', 2); + return false; + } + + if (!document.characterSet){ + Zotero.debug("Text file didn't have charset in indexFile()", 1); + return false; + } + var text = document.body.innerHTML.replace(/(>)/g, '$1 '); text = HTMLToText(text); indexString(text, document.characterSet, itemID); diff --git a/chrome/content/zotero/xpcom/mime.js b/chrome/content/zotero/xpcom/mime.js index 4e31c68d0..a36c6b634 100644 --- a/chrome/content/zotero/xpcom/mime.js +++ b/chrome/content/zotero/xpcom/mime.js @@ -26,6 +26,7 @@ Zotero.MIME = new function(){ this.sniffForBinary = sniffForBinary; this.getMIMETypeFromData = getMIMETypeFromData; this.getMIMETypeFromFile = getMIMETypeFromFile; + this.isDocumentType = isDocumentType; this.hasNativeHandler = hasNativeHandler; this.hasInternalHandler = hasInternalHandler; this.fileHasInternalHandler = fileHasInternalHandler; @@ -41,6 +42,14 @@ Zotero.MIME = new function(){ [" + +