OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 * @param {...} var_args | 47 * @param {...} var_args |
48 * @template T | 48 * @template T |
49 */ | 49 */ |
50 function push(array, var_args) | 50 function push(array, var_args) |
51 { | 51 { |
52 for (var i = 1; i < arguments.length; ++i) | 52 for (var i = 1; i < arguments.length; ++i) |
53 array[array.length] = arguments[i]; | 53 array[array.length] = arguments[i]; |
54 } | 54 } |
55 | 55 |
56 /** | 56 /** |
57 * @param {(!Arguments.<T>|!NodeList)} array | |
58 * @param {number=} index | |
59 * @return {!Array.<T>} | |
60 * @template T | |
61 */ | |
62 function slice(array, index) | |
63 { | |
64 var result = []; | |
65 for (var i = index || 0, j = 0; i < array.length; ++i, ++j) | |
66 result[j] = array[i]; | |
67 return result; | |
68 } | |
69 | |
70 /** | |
71 * @param {*} obj | 57 * @param {*} obj |
72 * @return {string} | 58 * @return {string} |
73 * @suppress {uselessCode} | 59 * @suppress {uselessCode} |
74 */ | 60 */ |
75 function toString(obj) | 61 function toString(obj) |
76 { | 62 { |
77 // We don't use String(obj) because String16 could be overridden. | 63 // We don't use String(obj) because String16 could be overridden. |
78 // Also the ("" + obj) expression may throw. | 64 // Also the ("" + obj) expression may throw. |
79 try { | 65 try { |
80 return "" + obj; | 66 return "" + obj; |
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 }, | 521 }, |
536 | 522 |
537 /** | 523 /** |
538 * @param {!Object} nativeCommandLineAPI | 524 * @param {!Object} nativeCommandLineAPI |
539 * @return {!Object} | 525 * @return {!Object} |
540 */ | 526 */ |
541 installCommandLineAPI: function(nativeCommandLineAPI) | 527 installCommandLineAPI: function(nativeCommandLineAPI) |
542 { | 528 { |
543 // NOTE: This list contains only not native Command Line API methods. Fo
r full list: V8Console. | 529 // NOTE: This list contains only not native Command Line API methods. Fo
r full list: V8Console. |
544 // NOTE: Argument names of these methods will be printed in the console,
so use pretty names! | 530 // NOTE: Argument names of these methods will be printed in the console,
so use pretty names! |
545 var members = [ "$", "$$", "$x", "monitorEvents", "unmonitorEvents", "ge
tEventListeners" ]; | 531 var members = [ "monitorEvents", "unmonitorEvents", "getEventListeners"
]; |
546 for (var member of members) | 532 for (var member of members) |
547 nativeCommandLineAPI[member] = CommandLineAPIImpl[member]; | 533 nativeCommandLineAPI[member] = CommandLineAPIImpl[member]; |
548 var functionToStringMap = new Map([ | 534 var functionToStringMap = new Map([ |
549 ["$", "function $(selector, [startNode]) { [Command Line AP
I] }"], | |
550 ["$$", "function $$(selector, [startNode]) { [Command Line A
PI] }"], | |
551 ["$x", "function $x(xpath, [startNode]) { [Command Line API]
}"], | |
552 ["monitorEvents", "function monitorEvents(object, [types]) { [Comm
and Line API] }"], | 535 ["monitorEvents", "function monitorEvents(object, [types]) { [Comm
and Line API] }"], |
553 ["unmonitorEvents", "function unmonitorEvents(object, [types]) { [Co
mmand Line API] }"], | 536 ["unmonitorEvents", "function unmonitorEvents(object, [types]) { [Co
mmand Line API] }"], |
554 ["getEventListeners", "function getEventListeners(node) { [Command L
ine API] }"] | 537 ["getEventListeners", "function getEventListeners(node) { [Command L
ine API] }"] |
555 ]); | 538 ]); |
556 for (let entry of functionToStringMap) | 539 for (let entry of functionToStringMap) |
557 nativeCommandLineAPI[entry[0]].toString = (() => entry[1]); | 540 nativeCommandLineAPI[entry[0]].toString = (() => entry[1]); |
558 return nativeCommandLineAPI; | 541 return nativeCommandLineAPI; |
559 }, | 542 }, |
560 | 543 |
561 /** | 544 /** |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 } | 1023 } |
1041 return string.substr(0, maxLength) + "\u2026"; | 1024 return string.substr(0, maxLength) + "\u2026"; |
1042 }, | 1025 }, |
1043 | 1026 |
1044 __proto__: null | 1027 __proto__: null |
1045 } | 1028 } |
1046 | 1029 |
1047 var CommandLineAPIImpl = { __proto__: null } | 1030 var CommandLineAPIImpl = { __proto__: null } |
1048 | 1031 |
1049 /** | 1032 /** |
1050 * @param {string} selector | |
1051 * @param {!Node=} opt_startNode | |
1052 * @return {*} | |
1053 */ | |
1054 CommandLineAPIImpl.$ = function (selector, opt_startNode) | |
1055 { | |
1056 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode)) | |
1057 return opt_startNode.querySelector(selector); | |
1058 | |
1059 return inspectedGlobalObject.document.querySelector(selector); | |
1060 } | |
1061 | |
1062 /** | |
1063 * @param {string} selector | |
1064 * @param {!Node=} opt_startNode | |
1065 * @return {*} | |
1066 */ | |
1067 CommandLineAPIImpl.$$ = function (selector, opt_startNode) | |
1068 { | |
1069 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode)) | |
1070 return slice(opt_startNode.querySelectorAll(selector)); | |
1071 return slice(inspectedGlobalObject.document.querySelectorAll(selector)); | |
1072 } | |
1073 | |
1074 /** | |
1075 * @param {!Node=} node | |
1076 * @return {boolean} | |
1077 */ | |
1078 CommandLineAPIImpl._canQuerySelectorOnNode = function(node) | |
1079 { | |
1080 return !!node && InjectedScriptHost.subtype(node) === "node" && (node.nodeTy
pe === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_NODE || node.nodeTyp
e === Node.DOCUMENT_FRAGMENT_NODE); | |
1081 } | |
1082 | |
1083 /** | |
1084 * @param {string} xpath | |
1085 * @param {!Node=} opt_startNode | |
1086 * @return {*} | |
1087 */ | |
1088 CommandLineAPIImpl.$x = function(xpath, opt_startNode) | |
1089 { | |
1090 var doc = (opt_startNode && opt_startNode.ownerDocument) || inspectedGlobalO
bject.document; | |
1091 var result = doc.evaluate(xpath, opt_startNode || doc, null, XPathResult.ANY
_TYPE, null); | |
1092 switch (result.resultType) { | |
1093 case XPathResult.NUMBER_TYPE: | |
1094 return result.numberValue; | |
1095 case XPathResult.STRING_TYPE: | |
1096 return result.stringValue; | |
1097 case XPathResult.BOOLEAN_TYPE: | |
1098 return result.booleanValue; | |
1099 default: | |
1100 var nodes = []; | |
1101 var node; | |
1102 while (node = result.iterateNext()) | |
1103 push(nodes, node); | |
1104 return nodes; | |
1105 } | |
1106 } | |
1107 | |
1108 /** | |
1109 * @param {!Object} object | 1033 * @param {!Object} object |
1110 * @param {!Array.<string>|string=} opt_types | 1034 * @param {!Array.<string>|string=} opt_types |
1111 */ | 1035 */ |
1112 CommandLineAPIImpl.monitorEvents = function(object, opt_types) | 1036 CommandLineAPIImpl.monitorEvents = function(object, opt_types) |
1113 { | 1037 { |
1114 if (!object || !object.addEventListener || !object.removeEventListener) | 1038 if (!object || !object.addEventListener || !object.removeEventListener) |
1115 return; | 1039 return; |
1116 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types); | 1040 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types); |
1117 for (var i = 0; i < types.length; ++i) { | 1041 for (var i = 0; i < types.length; ++i) { |
1118 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false
); | 1042 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false
); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1213 /** | 1137 /** |
1214 * @param {!Event} event | 1138 * @param {!Event} event |
1215 */ | 1139 */ |
1216 CommandLineAPIImpl._logEvent = function(event) | 1140 CommandLineAPIImpl._logEvent = function(event) |
1217 { | 1141 { |
1218 inspectedGlobalObject.console.log(event.type, event); | 1142 inspectedGlobalObject.console.log(event.type, event); |
1219 } | 1143 } |
1220 | 1144 |
1221 return injectedScript; | 1145 return injectedScript; |
1222 }) | 1146 }) |
OLD | NEW |