1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Javascript that is being injected into the inspectable page
* while debugging.
*/
goog.provide('devtools.Injected');
/**
* Main injected object.
* @constructor.
*/
devtools.Injected = function() {
/**
* This cache contains mapping from object it to an object instance for
* all results of the evaluation / console logs.
*/
this.cachedConsoleObjects_ = {};
/**
* Last id for the cache above.
*/
this.lastCachedConsoleObjectId_ = 1;
};
/**
* Caches console object for subsequent calls to getConsoleObjectProperties.
* @param {Object} obj Object to cache.
* @return {Object} console object wrapper.
*/
devtools.Injected.prototype.wrapConsoleObject = function(obj) {
var type = typeof obj;
if ((type == 'object' && obj != null) || type == 'function') {
var objId = '#consoleobj#' + this.lastCachedConsoleObjectId_++;
this.cachedConsoleObjects_[objId] = obj;
var result = { ___devtools_id : objId };
result.___devtools_class_name = Object.describe(obj, true);
// Loop below fills dummy object with properties for completion.
for (var name in obj) {
result[name] = '';
}
return result;
}
return obj;
};
/**
* Caches console object for subsequent calls to getConsoleObjectProperties.
* @param {Object} obj Object to cache.
* @return {string} Console object wrapper serialized into a JSON string.
*/
devtools.Injected.prototype.serializeConsoleObject = function(obj) {
var result = this.wrapConsoleObject(obj);
return JSON.stringify(result,
function (key, value) {
if (value === undefined) {
return 'undefined';
}
return value;
});
};
/**
* Dispatches given method with given args on the host object.
* @param {string} method Method name.
*/
devtools.Injected.prototype.InspectorController = function(method, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
return InspectorController[method].apply(InspectorController, args);
};
/**
* Dispatches given method with given args on the InjectedScript.
* @param {string} method Method name.
*/
devtools.Injected.prototype.InjectedScript = function(method, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
var result = InjectedScript[method].apply(InjectedScript, args);
return result;
};
// Plugging into upstreamed support.
InjectedScript._window = function() {
return contentWindow;
};
InjectedScript._nodeForId = function(nodeId) {
return DevToolsAgentHost.getNodeForId(nodeId);
};
InjectedScript._objectForId = function(id) {
if (typeof id == 'number') {
return DevToolsAgentHost.getNodeForId(id);
}
return devtools$$obj.cachedConsoleObjects_[id];
};
Object.type = function(obj, win)
{
if (obj === null)
return "null";
var type = typeof obj;
if (type !== "object" && type !== "function")
return type;
win = win || window;
if (obj instanceof win.Node)
return (obj.nodeType === undefined ? type : "node");
if (obj instanceof win.String)
return "string";
if (obj instanceof win.Array)
return "array";
if (obj instanceof win.Boolean)
return "boolean";
if (obj instanceof win.Number)
return "number";
if (obj instanceof win.Date)
return "date";
if (obj instanceof win.RegExp)
return "regexp";
if (obj instanceof win.Error)
return "error";
return type;
}
// Temporarily moved into the injected context.
Object.hasProperties = function(obj)
{
if (typeof obj === "undefined" || typeof obj === "null")
return false;
for (var name in obj)
return true;
return false;
}
Object.describe = function(obj, abbreviated)
{
var type1 = Object.type(obj);
var type2 = (obj == null) ? "null" : obj.constructor.name;
switch (type1) {
case "object":
case "node":
return type2;
case "array":
return "[" + obj.toString() + "]";
case "string":
if (obj.length > 100)
return "\"" + obj.substring(0, 100) + "\u2026\"";
return "\"" + obj + "\"";
case "function":
var objectText = String(obj);
if (!/^function /.test(objectText))
objectText = (type2 == "object") ? type1 : type2;
else if (abbreviated)
objectText = /.*/.exec(obj)[0].replace(/ +$/g, "");
return objectText;
case "regexp":
return String(obj).replace(/([\\\/])/g, "\\$1").replace(/\\(\/[gim]*)$/, "$1").substring(1);
default:
return String(obj);
}
}
|