-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-inspector.js
190 lines (167 loc) · 5.68 KB
/
web-inspector.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
/**
* A simple web inspector.
*
* Intended to be a singleton: this only exists once per page, so it
* attaches itself to the BODY element.
*/
var Inspector = function($) {
exports = {};
// The root element of the inspector.
var root = null;
var currentSelection = null;
var mouseIsOn = false;
var destroySelection = function() {
$('.selected').removeClass('selected');
}
var select = function(node) {
node.addClass("selected");
currentSelection = node;
}
var deselect = function(node) {
node.removeClass("selected");
currentSelection = null;
}
var template = ""
+ "<div class='tray'>"
+ " <textarea class='text-editor'></textarea>"
+ " <div class='property-editor'>"
+ " <div class='node-lookup'>"
+ " <input class='selector' /><input class='nth' />"
+ " <button class= 'mouse'>Mouse</button>"
+ " <button class= 'search'>Search</button>"
+ " </div>"
+ " <div class='property-list'>"
+ " </div>"
+ " </div>"
+ "</div>"
+ "<div class='handle'></div>";
var toggle = function(){
if (root.css('top') == '0px'){
root.animate({"top": "-300px"}, 500);
}
else {
root.animate({"top": "-0px"}, 500);
}
};
var searchBySelector = function() {
var nthBox = root.find(".nth");
var nthStr = nthBox.val();
var nth = parseInt(nthStr)
if(isNaN(nth)){
nth = 0;
}
var selectorBox = root.find(".selector");
var selectorStr = selectorBox.val();
var selection = $(selectorStr).eq(nth);
var html = selection.html();
var textEditor = root.find(".text-editor");
textEditor.empty();
textEditor.val(html);
var propertyView = root.find(".property-list");
var output = displayElements(selection);
propertyView.empty();
propertyView.append(output);
};
var displayElements = function(selection){
var textEditor = root.find(".text-editor");
var outputTable = $("<table></table>");
var elements = ["height","width","position","margin","padding","background-color","class"]
for(var i=0;i<elements.length;i++){
var newRow = $("<tr></tr>", {class:i});
var attrCell = $("<td></td>")
var valueCell = $("<td></td>")
attrCell.text(elements[i]);
valueCell.text(selection.css(elements[i]));
newRow.append(attrCell).append(valueCell);
outputTable.append(newRow);
}
return outputTable;
}
var mouseOn = function() {
mouseIsOn = true;
$('#wrapper *').mouseenter( function(event){
destroySelection();
select($(this));
event.stopPropagation();
})
$('#wrapper *').click( function(event){
event.preventDefault();
var textEditor = root.find(".text-editor");
textEditor.empty();
textEditor.val($(this).parent().html());
var propertyView = root.find(".property-list");
var output = displayElements($(this));
propertyView.html(output);
event.stopPropagation();
$(this).css('border', 'none');
mouseIsOn = false;
mouseOff();
})
$('#wrapper *').mouseleave( function(){
deselect($(this));
})
}
var mouseOff = function() {
mouseIsOn = false;
$('#wrapper *').unbind("mouseenter");
$('#wrapper *').unbind("click");
$('#wrapper *').unbind("mouseleave");
}
/*
* Construct the UI
*/
exports.initialize = function() {
root = $("<div class='inspector'></div>").appendTo($('body'));
root.append(template);
root.find(".handle").on("click", toggle);
root.find(".node-lookup .search").on("click", searchBySelector);
root.find(".node-lookup .mouse").on("click", function(){
if(!mouseIsOn){
mouseOn();
}
else{
mouseOff();
}
})
};
exports.toggle = toggle;
return exports;
};
/*****************************************************************************
* Boot up the web inspector!
*
* This will enable you to COPY AND PASTE this entire file into any web page
* to inspect it.
*
* XXX TODO!
* Change the CSS link below to point to the full URL of your CSS file!
*
* You shouldn't need to touch anything else below.
*
*****************************************************************************/
(function() {
var createInspector = function() {
window.inspector = Inspector(jQuery);
window.inspector.initialize();
}
// Add the CSS file to the HEAD
var css = document.createElement('link');
css.setAttribute('rel', 'stylesheet');
css.setAttribute('type', 'text/css');
css.setAttribute('href', 'web-inspector.css'); // XXX TODO CHANGEME!!
document.head.appendChild(css);
if ('jQuery' in window) {
createInspector(window.jQuery);
} else {
// Add jQuery to the HEAD and then start polling to see when it is there
var scr = document.createElement('script');
scr.setAttribute('src','http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
document.head.appendChild(scr);
var t = setInterval(function() {
if ('jQuery' in window) {
clearInterval(t); // Stop polling
createInspector();
}
}, 50);
}
})();