Question
How can plugin calls be handled in a reliable way to ensure that the caller always receives a JavaScript array, whether the plugin is a Firefox add-on or an ActiveX control? The current approach wraps plugin calls in functions like:
function getDevice(deviceKey) {
return plugin.getDevice(deviceKey);
}
An alternative method could be:
function getDevice(deviceKey) {
return normalizeArray(plugin.getDevice(deviceKey));
}
function normalizeArray(array) {
return typeof(array) == 'unknown' ? array.toArray() : array;
}
However, this solution requires remembering to call normalizeArray from the wrapper functions and may have limitations in terms of robustness and assumptions. Is there a more effective way to handle this scenario?
Background
I am developing JavaScript code to interact with a plugin using JavaScript. This plugin is accessible as an add-on in Firefox and as an ActiveX in Internet Explorer. Several methods within the plugin return arrays. In Firefox, calling:
typeof(retVal);
on the object returns 'object', where operations like:
retVal.length;
retval[0];
work as expected. However, when the same methods are called in Internet Explorer, using:
typeof(retVal);
returns 'unknown', resulting in undefined values for calls such as:
retVal.length;
retval[0];
Upon investigation, I found that the ActiveX object actually returns an array of variants. While this object is recognizable by JScript, a JavaScript version of the array can be obtained by calling retVal.toArray().