Add attr()/text() to translator sandbox

The current document is automatically used (but can still be provided as
the first argument to avoid accidental bugs during the transition).

Closes 
Addresses 
This commit is contained in:
Dan Stillman 2017-12-27 18:18:20 -05:00
parent 5350931617
commit 2bc44dddd1

View File

@ -1866,6 +1866,44 @@ Zotero.Translate.Base.prototype = {
this._sandboxManager.sandbox.Z = this._sandboxZotero; this._sandboxManager.sandbox.Z = this._sandboxZotero;
this._sandboxManager.sandbox.ZU = this._sandboxZotero.Utilities; this._sandboxManager.sandbox.ZU = this._sandboxZotero.Utilities;
this._transferItem = this._sandboxZotero._transferItem; this._transferItem = this._sandboxZotero._transferItem;
// Add web helper functions
if (this.type == 'web') {
this._sandboxManager.sandbox.attr = this._attr.bind(this);
this._sandboxManager.sandbox.text = this._text.bind(this);
}
},
/**
* Helper function to extract HTML attribute text
*/
_attr: function (selector, attr, index) {
if (typeof arguments[0] == 'string') {
var doc = this.document;
}
// Support legacy polyfill signature
else {
this._debug("WARNING: attr() no longer requires a document as the first argument");
[doc, selector, attr, index] = arguments;
}
var elem = index ? doc.querySelectorAll(selector).item(index) : doc.querySelector(selector);
return elem ? elem.getAttribute(attr) : null;
},
/**
* Helper function to extract HTML element text
*/
_text: function (selector, index) {
if (typeof arguments[0] == 'string') {
var doc = this.document;
}
// Support legacy polyfill signature
else {
this._debug("WARNING: text() no longer requires a document as the first argument");
[doc, selector, attr, index] = arguments;
}
var elem = index ? doc.querySelectorAll(selector).item(index) : doc.querySelector(selector);
return elem ? elem.textContent : null;
}, },
/** /**