CommandLineAPIModuleSource.js [plain text]
(function (InjectedScriptHost, inspectedWindow, injectedScriptId, injectedScript, CommandLineAPIHost) {
function slice(array, index)
{
var result = [];
for (var i = index || 0; i < array.length; ++i)
result.push(array[i]);
return result;
}
function bind(func, thisObject, var_args)
{
var args = slice(arguments, 2);
function bound(var_args)
{
return func.apply(thisObject, args.concat(slice(arguments)));
}
bound.toString = function() {
return "bound: " + func;
};
return bound;
}
function CommandLineAPI(commandLineAPIImpl, callFrame)
{
function inScopeVariables(member)
{
if (!callFrame)
return false;
var scopeChain = callFrame.scopeChain;
for (var i = 0; i < scopeChain.length; ++i) {
if (member in scopeChain[i])
return true;
}
return false;
}
function customToStringMethod(name)
{
return function () { return "function " + name + "() { [Command Line API] }"; };
}
for (var i = 0; i < CommandLineAPI.members_.length; ++i) {
var member = CommandLineAPI.members_[i];
if (member in inspectedWindow || inScopeVariables(member))
continue;
this[member] = bind(commandLineAPIImpl[member], commandLineAPIImpl);
this[member].toString = customToStringMethod(member);
}
this.__defineGetter__("$0", bind(commandLineAPIImpl._inspectedObject, commandLineAPIImpl));
for (var i = 1; i <= injectedScript._savedResults.length; ++i) {
var member = "$" + i;
if (member in inspectedWindow || inScopeVariables(member))
continue;
this.__defineGetter__("$" + i, bind(injectedScript._savedResult, injectedScript, i));
}
this.$_ = injectedScript._lastResult;
this.$exception = injectedScript._exceptionValue;
}
CommandLineAPI.members_ = [
"$", "$$", "$x", "dir", "dirxml", "keys", "values", "profile", "profileEnd", "table",
"monitorEvents", "unmonitorEvents", "inspect", "copy", "clear", "getEventListeners"
];
function CommandLineAPIImpl()
{
}
CommandLineAPIImpl.prototype = {
$: function (selector, start)
{
if (this._canQuerySelectorOnNode(start))
return start.querySelector(selector);
var result = inspectedWindow.document.querySelector(selector);
if (result)
return result;
if (selector && selector[0] !== "#") {
result = inspectedWindow.document.getElementById(selector);
if (result) {
inspectedWindow.console.warn("The console function $() has changed from $=getElementById(id) to $=querySelector(selector). You might try $(\"#%s\")", selector );
return null;
}
}
return result;
},
$$: function (selector, start)
{
if (this._canQuerySelectorOnNode(start))
return start.querySelectorAll(selector);
return inspectedWindow.document.querySelectorAll(selector);
},
_canQuerySelectorOnNode: function(node)
{
return !!node && InjectedScriptHost.type(node) === "node" && (node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_NODE || node.nodeType === Node.DOCUMENT_FRAGMENT_NODE);
},
$x: function(xpath, context)
{
var doc = (context && context.ownerDocument) || inspectedWindow.document;
var result = doc.evaluate(xpath, context || doc, null, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.NUMBER_TYPE:
return result.numberValue;
case XPathResult.STRING_TYPE:
return result.stringValue;
case XPathResult.BOOLEAN_TYPE:
return result.booleanValue;
default:
var nodes = [];
var node;
while (node = result.iterateNext())
nodes.push(node);
return nodes;
}
},
dir: function()
{
return inspectedWindow.console.dir.apply(inspectedWindow.console, arguments)
},
dirxml: function()
{
return inspectedWindow.console.dirxml.apply(inspectedWindow.console, arguments)
},
keys: function(object)
{
return Object.keys(object);
},
values: function(object)
{
var result = [];
for (var key in object)
result.push(object[key]);
return result;
},
profile: function()
{
return inspectedWindow.console.profile.apply(inspectedWindow.console, arguments)
},
profileEnd: function()
{
return inspectedWindow.console.profileEnd.apply(inspectedWindow.console, arguments)
},
table: function()
{
return inspectedWindow.console.table.apply(inspectedWindow.console, arguments)
},
monitorEvents: function(object, types)
{
if (!object || !object.addEventListener || !object.removeEventListener)
return;
types = this._normalizeEventTypes(types);
for (var i = 0; i < types.length; ++i) {
object.removeEventListener(types[i], this._logEvent, false);
object.addEventListener(types[i], this._logEvent, false);
}
},
unmonitorEvents: function(object, types)
{
if (!object || !object.addEventListener || !object.removeEventListener)
return;
types = this._normalizeEventTypes(types);
for (var i = 0; i < types.length; ++i)
object.removeEventListener(types[i], this._logEvent, false);
},
inspect: function(object)
{
return this._inspect(object);
},
copy: function(object)
{
if (injectedScript._subtype(object) === "node")
object = object.outerHTML;
CommandLineAPIHost.copyText(object);
},
clear: function()
{
CommandLineAPIHost.clearConsoleMessages();
},
getEventListeners: function(node)
{
return CommandLineAPIHost.getEventListeners(node);
},
_inspectedObject: function()
{
return CommandLineAPIHost.inspectedObject();
},
_normalizeEventTypes: function(types)
{
if (typeof types === "undefined")
types = [ "mouse", "key", "touch", "control", "load", "unload", "abort", "error", "select", "change", "submit", "reset", "focus", "blur", "resize", "scroll", "search", "devicemotion", "deviceorientation" ];
else if (typeof types === "string")
types = [ types ];
var result = [];
for (var i = 0; i < types.length; i++) {
if (types[i] === "mouse")
result.splice(0, 0, "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel");
else if (types[i] === "key")
result.splice(0, 0, "keydown", "keyup", "keypress", "textInput");
else if (types[i] === "touch")
result.splice(0, 0, "touchstart", "touchmove", "touchend", "touchcancel");
else if (types[i] === "control")
result.splice(0, 0, "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset");
else
result.push(types[i]);
}
return result;
},
_logEvent: function(event)
{
inspectedWindow.console.log(event.type, event);
},
_inspect: function(object)
{
if (arguments.length === 0)
return;
var objectId = injectedScript._wrapObject(object, "");
var hints = {};
switch (injectedScript._describe(object)) {
case "Database":
var databaseId = CommandLineAPIHost.databaseId(object)
if (databaseId)
hints.databaseId = databaseId;
break;
case "Storage":
var storageId = CommandLineAPIHost.storageId(object)
if (storageId)
hints.domStorageId = InjectedScriptHost.evaluate("(" + storageId + ")");
break;
}
CommandLineAPIHost.inspect(objectId, hints);
return object;
}
}
injectedScript.CommandLineAPI = CommandLineAPI;
injectedScript._commandLineAPIImpl = new CommandLineAPIImpl();
return {};
})