-
Notifications
You must be signed in to change notification settings - Fork 26
/
devtoolsBackground.js
220 lines (190 loc) · 8.27 KB
/
devtoolsBackground.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* jslint browser: true */
/* global chrome, $0 */
function getPanelContents(settings) {
if (!$0) return;
settings || (settings = {});
// In case we're in an <iframe>
var document = $0.ownerDocument;
var window = document.defaultView;
// We will only work if there is a "jQuery" function.
// TODO: Alternatively check for an AMD module.
if (typeof window.jQuery !== 'function')
return (function(msg) {
return (msg.Error = '@(window.jQuery is missing)'), msg;
})(Object.create(null));
if (typeof window.jQuery._data !== 'function')
return (function(msg) {
return (msg.Error = '@(jQuery version is too old)'), msg;
})(Object.create(null));
return (function($, el) {
function internalClass(obj) {
return Object.prototype.toString.call(obj).match(/\[object (\w+)\]/)[1];
}
// Return objects with "__proto__ = null" so we don't clutter up the panel.
function extendNull() {
return $.extend.apply(null, [].concat.apply([Object.create(null)], arguments));
}
// Turn certain objects into descriptive strings so they're easier to read.
function exportify(obj, none) {
if (obj === null) return none ? '@(none)' : '@(null)';
if (obj === undefined) return none ? '@(none)' : '@(undefined)';
if (obj === false && none) return '@(none)';
if (typeof obj !== 'object') return obj;
var hasKeys = !!Object.keys(obj).length;
if (none && !hasKeys) return '@(none)';
if (hasKeys) return extendNull(obj);
return '@(empty ' + internalClass(obj) + ')';
}
// Get/create the comment nodes "<!-- @(window) -->" and "<!-- @(document) -->"
// that go below "<html>". These are used as representations for
// the "window" and "document" objects. Since they don't have a representation
// in the Elements Panel, but can have event listeners, we add the comment
// nodes for convenience.
//
// These use to be above the "<!DOCTYPE html>". There is bug in Chrome
// the caused the Elements Panel to scroll to the selected element if the DOM
// changed when these helper elements were above "<html>". So, they got moved.
//
// To turn them off go to Extensions -> jQuery Audit -> options
//
var jQueryAudit = !settings.displayHelpers ? {} : window.jQueryAudit ||
(window.jQueryAudit = (function() {
var target = document.documentElement.firstChild;
var parent = target.parentNode;
var jQA = {};
try {
jQA.window = parent.insertBefore(
document.createComment('@(window)'),
target );
jQA.document = parent.insertBefore(
document.createComment('@(document)'),
target );
jQA.cleanup = function() {
parent.removeChild(jQA.window);
parent.removeChild(jQA.document);
jQA.cleanup = jQA.window = jQA.document = null;
};
} catch(err) {}
return jQA;
})());
// Check if we're on "<!-- @(window) -->" or "<!-- @(document) -->"
var target = (el === jQueryAudit.window) ? window :
(el === jQueryAudit.document) ? document :
el;
// Get all the events this element will react to, either because they're
// directly bound to it w/o delegation, or because some ancestor is delegating.
var events = (function() {
var el = target;
var ancestors = (el === window) ? [ window ] :
(el === document) ? [ document ] :
[ el ].concat( $(el).parents().toArray(), document, window );
return $.map(ancestors, function(node) {
return $.map(($._data(node)||0).events || {}, function(val) {
return val;
}).filter(function(ev) {
var sel = ev.selector;
if (!sel) return node === el;
// Pretty much the same methodology jQuery uses
// https://github.com/jquery/jquery/blob/1.10.2/src/event.js#L435
return (ev.needsContext) ?
$(sel, node).length :
$.find(sel, node, null, (el.nodeType === 1) ? [ el ] : []).length;
}).map(function(ev) {
return extendNull(ev, {
delegator: (node === el) ? '@(this element)' : node
});
});
}).reduce(function(acc, ev) {
++acc.sum[ev.type] || (acc.sum[ev.type] = 0);
acc.all[ev.type + '.' + acc.sum[ev.type]] = ev;
acc.i++;
return acc;
}, {
i: 0,
all: {},
sum: {}
});
})();
var internalData = (function() {
return $._data(target);
})();
var data = (function() {
var _data = $._data(target);
// Avoid $(..).data() side-effects
return _data && _data.parsedAttrs && _data.data;
})();
var dataset = (function() {
return target.dataset;
})();
// If you have <div><p class="myClass"><a href="#"></p></div> and you select the <p>,
// it returns <p class="myClass"></p>
var ownHTML = (function() {
var outerHTML = target.outerHTML;
if (outerHTML && ('innerHTML' in target))
return outerHTML.replace(target.innerHTML, '');
})();
// Panel output
return extendNull( (function() {
var panel = {};
panel['@(this element)'] = target;
panel['Data'] = exportify( data );
panel['Events(' + events.i + ')'] = exportify( events.all, true);
panel['Internal Data'] = exportify( internalData, true );
panel['dataset'] = exportify( dataset );
panel['own HTML'] = exportify( ownHTML, true );
return panel;
})() );
})(window.jQuery, $0);
}
var backgroundPageConnection = chrome.runtime.connect({
name: 'devtools-jqueryaudit'
});
// Pass "tabId" to the background page so we can cleanup after the panel closes.
backgroundPageConnection.postMessage({
tabId: chrome.devtools.inspectedWindow.tabId
});
var elements = chrome.devtools.panels.elements;
elements.createSidebarPane('jQuery Audit', function(sidebar) {
// Assume we're visible from the start
var isVisible = true;
updatePanelContents();
elements.onSelectionChanged.addListener(function() {
if (isVisible) updatePanelContents();
});
// "onShown" is flaky on Chrome 31
if (getChromeVersion() > 31) {
// Don't update the sidebar if it's not visible
sidebar.onShown.addListener(function() {
isVisible = true;
updatePanelContents();
});
sidebar.onHidden.addListener(function() {
isVisible = false;
});
}
function updatePanelContents() {
sidebar.setExpression( serializeAsIIFE(getPanelContents, getSettings()) );
}
});
function serializeAsIIFE(fn) {
var params = Array.prototype.slice
.call(arguments, 1)
.map(function(arg) {
return JSON.stringify(arg);
}).join(',');
return [ '(', fn.toString(), ')(', params, ')' ].join('');
}
function getChromeVersion() {
return parseInt(navigator.userAgent.match(/chrome\/(\d+)/i)[1]);
}
function getSettings() {
var store = localStorage;
// Default settings
var settings = {
displayHelpers: true
};
if ('displayHelpers' in store) {
(settings.displayHelpers = !!parseInt(store.displayHelpers));
}
return settings;
}