- closes #539, Support URL bar saving when opening supported import format directly

- Zotero.Translate error messages are now prefixed with "Translate: "
This commit is contained in:
Simon Kornblith 2007-09-26 21:51:31 +00:00
parent 0c3ea37bee
commit 6cffa75cd0
2 changed files with 60 additions and 59 deletions

View File

@ -288,11 +288,10 @@ var Zotero_Browser = new function() {
*/ */
function contentLoad(event) { function contentLoad(event) {
var isHTML = event.originalTarget instanceof HTMLDocument; var isHTML = event.originalTarget instanceof HTMLDocument;
var doc = event.originalTarget;
var rootDoc = doc;
if(isHTML) { if(isHTML) {
var doc = event.originalTarget;
var rootDoc = doc;
// get the appropriate root document to check which browser we're on // get the appropriate root document to check which browser we're on
while(rootDoc.defaultView.frameElement) { while(rootDoc.defaultView.frameElement) {
rootDoc = rootDoc.defaultView.frameElement.ownerDocument; rootDoc = rootDoc.defaultView.frameElement.ownerDocument;
@ -345,11 +344,11 @@ var Zotero_Browser = new function() {
}, false); }, false);
} }
} }
// detect translators
tab.detectTranslators(rootDoc, doc);
} }
// detect translators
tab.detectTranslators(rootDoc, doc);
// clear annotateNextLoad // clear annotateNextLoad
if(tab.annotateNextLoad) { if(tab.annotateNextLoad) {
tab.annotateNextLoad = tab.annotateID = undefined; tab.annotateNextLoad = tab.annotateID = undefined;
@ -590,13 +589,17 @@ Zotero_Browser.Tab.prototype.detectTranslators = function(rootDoc, doc) {
} }
} }
// get translators if(doc instanceof HTMLDocument) {
var me = this; // get translators
var me = this;
var translate = new Zotero.Translate("web");
translate.setDocument(doc); var translate = new Zotero.Translate("web");
translate.setHandler("translators", function(obj, item) { me._translatorsAvailable(obj, item) }); translate.setDocument(doc);
var translators = translate.getTranslators(); translate.setHandler("translators", function(obj, item) { me._translatorsAvailable(obj, item) });
translate.getTranslators();
} else if(doc.documentURI.length > 7 && doc.documentURI.substr(0, 7) == "file://") {
this._attemptLocalFileImport(doc);
}
} }
@ -616,6 +619,21 @@ Zotero_Browser.Tab.prototype._searchFrames = function(rootDoc, searchDoc) {
return false; return false;
} }
/*
* Attempts import of a file; to be run on local files only
*/
Zotero_Browser.Tab.prototype._attemptLocalFileImport = function(doc) {
var file = Components.classes["@mozilla.org/network/protocol;1?name=file"]
.getService(Components.interfaces.nsIFileProtocolHandler)
.getFileFromURLSpec(doc.documentURI);
var me = this;
var translate = new Zotero.Translate("import");
translate.setLocation(file);
translate.setHandler("translators", function(obj, item) { me._translatorsAvailable(obj, item) });
translate.getTranslators();
}
/* /*
* translate a page, saving in saveLocation * translate a page, saving in saveLocation
*/ */
@ -704,8 +722,10 @@ Zotero_Browser.Tab.prototype._translatorsAvailable = function(translate, transla
this.page.translate = translate; this.page.translate = translate;
this.page.translators = translators; this.page.translators = translators;
this.page.document = translate.document; this.page.document = translate.document;
} else if(translate.type != "import" && translate.document.documentURI.length > 7
&& translate.document.documentURI.substr(0, 7) == "file://") {
this._attemptLocalFileImport(translate.document);
} }
Zotero_Browser.updateStatus(); Zotero_Browser.updateStatus();
} }

View File

@ -430,7 +430,7 @@ Zotero.Translate.prototype.runHandler = function(type, argument) {
var returnValue = undefined; var returnValue = undefined;
if(this._handlers[type]) { if(this._handlers[type]) {
for(var i in this._handlers[type]) { for(var i in this._handlers[type]) {
Zotero.debug("running handler "+i+" for "+type); Zotero.debug("Translate: running handler "+i+" for "+type);
try { try {
if(this._parentTranslator) { if(this._parentTranslator) {
returnValue = this._handlers[type][i](null, argument); returnValue = this._handlers[type][i](null, argument);
@ -446,7 +446,7 @@ Zotero.Translate.prototype.runHandler = function(type, argument) {
} else { } else {
// otherwise, fail silently, so as not to interfere with // otherwise, fail silently, so as not to interfere with
// interface cleanup // interface cleanup
Zotero.debug(e+' in handler '+i+' for '+type); Zotero.debug("Translate: "+e+' in handler '+i+' for '+type);
} }
} }
} }
@ -457,20 +457,7 @@ Zotero.Translate.prototype.runHandler = function(type, argument) {
/* /*
* gets all applicable translators * gets all applicable translators
* *
* for import, you should call this after setFile; otherwise, you'll just get * for import, you should call this after setLocation; otherwise, you'll just get
* a list of all import filters, not filters equipped to handle a specific file
*
* this returns a list of translator objects, of which the following fields
* are useful:
*
* translatorID - the GUID of the translator
* label - the name of the translator
* itemType - the type of item this scraper says it will scrape
*/
/*
* gets all applicable translators
*
* for import, you should call this after setFile; otherwise, you'll just get
* a list of all import filters, not filters equipped to handle a specific file * a list of all import filters, not filters equipped to handle a specific file
* *
* this returns a list of translator objects, of which the following fields * this returns a list of translator objects, of which the following fields
@ -501,7 +488,7 @@ Zotero.Translate.prototype.getTranslators = function() {
this._setSandboxMode("detect"); this._setSandboxMode("detect");
var possibleTranslators = new Array(); var possibleTranslators = new Array();
Zotero.debug("searching for translators for "+(this.path ? this.path : "an undisclosed location")); Zotero.debug("Translate: searching for translators for "+(this.path ? this.path : "an undisclosed location"));
// see which translators can translate // see which translators can translate
this._translatorSearch = new Zotero.Translate.TranslatorSearch(this, translators); this._translatorSearch = new Zotero.Translate.TranslatorSearch(this, translators);
@ -527,7 +514,7 @@ Zotero.Translate.prototype._loadTranslator = function() {
// parse detect code for the translator // parse detect code for the translator
this._parseDetectCode(this.translator[0]); this._parseDetectCode(this.translator[0]);
Zotero.debug("parsing code for "+this.translator[0].label); Zotero.debug("Translate: parsing code for "+this.translator[0].label);
try { try {
Components.utils.evalInSandbox(this.translator[0].code, this._sandbox); Components.utils.evalInSandbox(this.translator[0].code, this._sandbox);
@ -641,7 +628,7 @@ Zotero.Translate.prototype._generateSandbox = function() {
if(this.type == "web") { if(this.type == "web") {
// use real URL, not proxied version, to create sandbox // use real URL, not proxied version, to create sandbox
sandboxLocation = this.document.defaultView; sandboxLocation = this.document.defaultView;
Zotero.debug("binding sandbox to "+this.document.location.href); Zotero.debug("Translate: binding sandbox to "+this.document.location.href);
} else { } else {
// generate sandbox for search by extracting domain from translator // generate sandbox for search by extracting domain from translator
// target, if one exists // target, if one exists
@ -653,7 +640,7 @@ Zotero.Translate.prototype._generateSandbox = function() {
sandboxLocation = m[0]; sandboxLocation = m[0];
} }
} }
Zotero.debug("binding sandbox to "+sandboxLocation); Zotero.debug("Translate: binding sandbox to "+sandboxLocation);
} }
this._sandbox = new Components.utils.Sandbox(sandboxLocation); this._sandbox = new Components.utils.Sandbox(sandboxLocation);
@ -810,7 +797,7 @@ Zotero.Translate.prototype._setSandboxMode = function(mode) {
*/ */
Zotero.Translate.prototype._configure = function(option, value) { Zotero.Translate.prototype._configure = function(option, value) {
this.configOptions[option] = value; this.configOptions[option] = value;
Zotero.debug("setting configure option "+option+" to "+value); Zotero.debug("Translate: setting configure option "+option+" to "+value);
} }
/* /*
@ -822,7 +809,7 @@ Zotero.Translate.prototype._configure = function(option, value) {
*/ */
Zotero.Translate.prototype._addOption = function(option, value) { Zotero.Translate.prototype._addOption = function(option, value) {
this.displayOptions[option] = value; this.displayOptions[option] = value;
Zotero.debug("setting display option "+option+" to "+value); Zotero.debug("Translate: setting display option "+option+" to "+value);
} }
/* /*
@ -895,7 +882,7 @@ Zotero.Translate.prototype._translationComplete = function(returnValue, error) {
if(this.type == "search" && !this._itemsDone) { if(this.type == "search" && !this._itemsDone) {
// if we're performing a search and didn't get any results, go on // if we're performing a search and didn't get any results, go on
// to the next translator // to the next translator
Zotero.debug("could not find a result using "+this.translator[0].label+": \n" Zotero.debug("Translate: could not find a result using "+this.translator[0].label+": \n"
+this._generateErrorString(error)); +this._generateErrorString(error));
if(this.translator.length > 1) { if(this.translator.length > 1) {
this.translator.shift(); this.translator.shift();
@ -1023,7 +1010,6 @@ Zotero.Translate.prototype._closeStreams = function() {
* handles tags and see also data for notes and attachments * handles tags and see also data for notes and attachments
*/ */
Zotero.Translate.prototype._itemTagsAndSeeAlso = function(item, newItem) { Zotero.Translate.prototype._itemTagsAndSeeAlso = function(item, newItem) {
Zotero.debug("handling notes and see also");
// add to ID map // add to ID map
if(item.itemID) { if(item.itemID) {
this._IDMap[item.itemID] = newItem.getID(); this._IDMap[item.itemID] = newItem.getID();
@ -1073,7 +1059,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
if(this._parentTranslator) { if(this._parentTranslator) {
var pt = this._parentTranslator; var pt = this._parentTranslator;
item.complete = function() { pt._itemDone(this) }; item.complete = function() { pt._itemDone(this) };
Zotero.debug("done from parent sandbox"); Zotero.debug("Translate: calling done from parent sandbox");
} }
this.runHandler("itemDone", item); this.runHandler("itemDone", item);
return; return;
@ -1100,11 +1086,11 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
// create new item // create new item
if(type == "attachment") { if(type == "attachment") {
if(this.type != "import") { if(this.type != "import") {
Zotero.debug("discarding standalone attachment"); Zotero.debug("Translate: discarding standalone attachment");
return; return;
} }
Zotero.debug("adding attachment"); Zotero.debug("Translate: adding attachment");
if(!item.path) { if(!item.path) {
// create from URL // create from URL
@ -1112,7 +1098,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
var myID = Zotero.Attachments.linkFromURL(item.url, attachedTo, var myID = Zotero.Attachments.linkFromURL(item.url, attachedTo,
(item.mimeType ? item.mimeType : undefined), (item.mimeType ? item.mimeType : undefined),
(item.title ? item.title : undefined)); (item.title ? item.title : undefined));
Zotero.debug("created attachment; id is "+myID); Zotero.debug("Translate: created attachment; id is "+myID);
if(!myID) { if(!myID) {
// if we didn't get an ID, don't continue adding // if we didn't get an ID, don't continue adding
// notes, because we can't without knowing the ID // notes, because we can't without knowing the ID
@ -1120,7 +1106,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
} }
var newItem = Zotero.Items.get(myID); var newItem = Zotero.Items.get(myID);
} else { } else {
Zotero.debug("not adding attachment: no path or url specified"); Zotero.debug("Translate: not adding attachment: no path or url specified");
return; return;
} }
} else { } else {
@ -1181,7 +1167,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
try { try {
var creatorType = Zotero.CreatorTypes.getID(data[j].creatorType); var creatorType = Zotero.CreatorTypes.getID(data[j].creatorType);
} catch(e) { } catch(e) {
Zotero.debug("invalid creator type "+data[j].creatorType+" for creator index "+j); Zotero.debug("Translate: invalid creator type "+data[j].creatorType+" for creator index "+j);
} }
} }
@ -1197,14 +1183,14 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
// try to map from base field // try to map from base field
if(Zotero.ItemFields.isBaseField(fieldID)) { if(Zotero.ItemFields.isBaseField(fieldID)) {
var fieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(typeID, fieldID); var fieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(typeID, fieldID);
if(fieldID) Zotero.debug("mapping "+field+" to "+Zotero.ItemFields.getName(fieldID)); if(fieldID) Zotero.debug("Translate: mapping "+field+" to "+Zotero.ItemFields.getName(fieldID));
} }
// if field is valid for this type, set field // if field is valid for this type, set field
if(fieldID && Zotero.ItemFields.isValidForType(fieldID, typeID)) { if(fieldID && Zotero.ItemFields.isValidForType(fieldID, typeID)) {
newItem.setField(fieldID, data); newItem.setField(fieldID, data);
} else { } else {
Zotero.debug("discarded field "+field+" for item: field not valid for type "+type); Zotero.debug("Translate: discarded field "+field+" for item: field not valid for type "+type);
} }
} }
} }
@ -1266,11 +1252,10 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
// handle attachments // handle attachments
if(item.attachments && Zotero.Prefs.get("automaticSnapshots")) { if(item.attachments && Zotero.Prefs.get("automaticSnapshots")) {
Zotero.debug("HANDLING ATTACHMENTS");
for each(var attachment in item.attachments) { for each(var attachment in item.attachments) {
if(this.type == "web") { if(this.type == "web") {
if(!attachment.url && !attachment.document) { if(!attachment.url && !attachment.document) {
Zotero.debug("not adding attachment: no URL specified"); Zotero.debug("Translate: not adding attachment: no URL specified");
} else { } else {
if(attachment.snapshot === false) { if(attachment.snapshot === false) {
// if snapshot is explicitly set to false, attach as link // if snapshot is explicitly set to false, attach as link
@ -1280,7 +1265,7 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
(attachment.title ? attachment.title : attachment.document.title)); (attachment.title ? attachment.title : attachment.document.title));
} else { } else {
if(!attachment.mimeType || !attachment.title) { if(!attachment.mimeType || !attachment.title) {
Zotero.debug("NOTICE: either mimeType or title is missing; attaching file will be slower"); Zotero.debug("Translate: NOTICE: either mimeType or title is missing; attaching file will be slower");
} }
attachmentID = Zotero.Attachments.linkFromURL(attachment.url, myID, attachmentID = Zotero.Attachments.linkFromURL(attachment.url, myID,
@ -1294,8 +1279,6 @@ Zotero.Translate.prototype._itemDone = function(item, attachedTo) {
if(attachment.document) { if(attachment.document) {
Zotero.Attachments.importFromDocument(attachment.document, myID, attachment.title); Zotero.Attachments.importFromDocument(attachment.document, myID, attachment.title);
} else { } else {
Zotero.debug("GOT ATTACHMENT");
var mimeType = null; var mimeType = null;
var title = null; var title = null;
@ -1400,10 +1383,10 @@ Zotero.Translate.prototype._processCollection = function(collection, parentID) {
} else { } else {
// add mapped items to collection // add mapped items to collection
if(this._IDMap[child.id]) { if(this._IDMap[child.id]) {
Zotero.debug("adding "+this._IDMap[child.id]); Zotero.debug("Translate: adding "+this._IDMap[child.id]);
newCollection.addItem(this._IDMap[child.id]); newCollection.addItem(this._IDMap[child.id]);
} else { } else {
Zotero.debug("could not map "+child.id+" to an imported item"); Zotero.debug("Translate: could not map "+child.id+" to an imported item");
} }
} }
} }
@ -1722,8 +1705,6 @@ Zotero.Translate.prototype._export = function() {
} }
} }
Zotero.debug(this.displayOptions);
// export file data, if requested // export file data, if requested
if(this.displayOptions["exportFileData"]) { if(this.displayOptions["exportFileData"]) {
// generate directory // generate directory
@ -2082,7 +2063,7 @@ Zotero.Translate.TranslatorSearch.prototype.execute = function() {
// get next translator // get next translator
var translator = this.translators.shift(); var translator = this.translators.shift();
if((this.translate.type == "import" || this.translate.type == "web") && !this.translate.location) { if((this.translate.type == "import" || this.translate.type == "web") && !this.translate.location && !this.translate._storage) {
// if no location yet (e.g., getting list of possible web translators), // if no location yet (e.g., getting list of possible web translators),
// just return true // just return true
this.addTranslator(translator); this.addTranslator(translator);
@ -2136,7 +2117,7 @@ Zotero.Translate.TranslatorSearch.prototype.execute = function() {
try { try {
this.translate._importConfigureIO(); // so it can read this.translate._importConfigureIO(); // so it can read
} catch(e) { } catch(e) {
Zotero.debug(e+' in opening IO for '+translator.label); Zotero.debug("Translate: "+e+' in opening IO for '+translator.label);
this.execute(); this.execute();
return; return;
} }
@ -2164,7 +2145,7 @@ Zotero.Translate.TranslatorSearch.prototype.execute = function() {
return; return;
} }
Zotero.debug("executed detectCode for "+translator.label); Zotero.debug("Translate: executed detectCode for "+translator.label);
if(this.translate.type == "web" && this.translate.waitForCompletion) { if(this.translate.type == "web" && this.translate.waitForCompletion) {
this.asyncMode = true; this.asyncMode = true;
@ -2211,7 +2192,7 @@ Zotero.Translate.TranslatorSearch.prototype.checkDone = function() {
* Processes the return value from a translator * Processes the return value from a translator
*/ */
Zotero.Translate.TranslatorSearch.prototype.processReturnValue = function(translator, returnValue) { Zotero.Translate.TranslatorSearch.prototype.processReturnValue = function(translator, returnValue) {
Zotero.debug("found translator "+translator.label); Zotero.debug("Translate: found translator "+translator.label);
if(typeof(returnValue) == "string") { if(typeof(returnValue) == "string") {
translator.itemType = returnValue; translator.itemType = returnValue;