CommandLineAPIModuleSource.js [plain text]
(function (InjectedScriptHost, inspectedWindow, injectedScriptId, injectedScript, CommandLineAPIHost) {
function bind(func, thisObject, ...outerArgs)
{
return function(...innerArgs) {
return func.apply(thisObject, outerArgs.concat(innerArgs));
};
}
function CommandLineAPI(commandLineAPIImpl, callFrame)
{
this.$_ = injectedScript._lastResult;
this.$exception = injectedScript._exceptionValue;
this.__defineGetter__("$0", bind(commandLineAPIImpl._inspectedObject, commandLineAPIImpl));
for (let i = 1; i <= injectedScript._savedResults.length; ++i)
this.__defineGetter__("$" + i, bind(injectedScript._savedResult, injectedScript, i));
for (let member of CommandLineAPI.members_) {
this[member] = bind(commandLineAPIImpl[member], commandLineAPIImpl);
this[member].toString = function() { return "function " + member + "() { [Command Line API] }" };
}
}
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 Array.from(start.querySelectorAll(selector));
return Array.from(inspectedWindow.document.querySelectorAll(selector));
},
_canQuerySelectorOnNode: function(node)
{
return !!node && InjectedScriptHost.subtype(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)
{
var string;
var subtype = injectedScript._subtype(object);
if (subtype === "node")
string = object.outerHTML;
else if (subtype === "regexp")
string = "" + object;
else if (injectedScript.isPrimitiveValue(object))
string = "" + object;
else if (typeof object === "symbol")
string = String(object);
else if (typeof object === "function")
string = "" + object;
else {
try {
string = JSON.stringify(object, null, " ");
} catch (e) {
string = "" + object;
}
}
CommandLineAPIHost.copyText(string);
},
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 {};
})