-
Notifications
You must be signed in to change notification settings - Fork 0
/
javadocInstantSearch.user.js
executable file
·257 lines (236 loc) · 8.57 KB
/
javadocInstantSearch.user.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// ==UserScript==
// @name Javadoc Instant Search
// @namespace http://jo.zerezo.com/projects/
// @description Instant search for Javadoc class names.
// @include */allclasses-frame.html
// @include */allclasses-noframe.html
// @include */package-frame.html
// @include */overview-frame.html
// ==/UserScript==
//
// version 0.5 (2012/11/17)
// author Joël THIEFFRY
// http://jo.zerezo.com/projects/javadocInstantSearch.html
//
// This script is distributed under the MIT licence.
// http://www.opensource.org/licenses/mit-license.php
//
(function() {
"use strict";
// ID of the container for Javadoc Instant Search
var ELEMENT_ID = "javadocInstantSearchElement";
// IDs for regex type selector
var ECLIPSE_REGEX_ID = "Eclipse";
var SIMPLIFIED_REGEX_ID = "Simplified";
var PLAIN_REGEX_ID = "Plain";
// Compatibility layer for adding and removing events
var addEvent;
var removeEvent;
if (document.addEventListener) {
addEvent = function(element, eventname, funcname) {
element.addEventListener(eventname, funcname, false);
};
removeEvent = function(element, eventname, funcname) {
element.removeEventListener(eventname, funcname, false);
};
} else if (document.attachEvent) {
addEvent = function(element, eventname, funcname) {
element.attachEvent("on" + eventname, funcname);
};
removeEvent = function(element, eventname, funcname) {
element.detachEvent("on" + eventname, funcname);
};
}
// Create and insert the Javadoc Instant Search container
var install = function() {
// Second safegard to prevent double instanciation (while debugging)
if (document.getElementById(ELEMENT_ID)) {
return true;
}
// Detection of javadoc format
var indexContainer = document.getElementsByClassName("indexContainer");
var table = document.getElementsByTagName("table");
if (!indexContainer && !table) {
return;
}
var isModernFormat = indexContainer.length === 1;
// Creation of search elements
var searchContainer = document.createElement("p");
searchContainer.id = ELEMENT_ID;
var textInput = document.createElement("input");
textInput.type = "text";
textInput.style.backgroundColor = "White";
var eraseIcon = document.createElement("input");
eraseIcon.type = "image";
// Origin: http://www.teria.com/~koseki/tools/gm/javadoc_isearch/index.html
eraseIcon.src = "data:image/gif;base64,R0lGODlhDQANAJEDAM%2FPz%2F%2F%2F%2F93d3UpihSH5BAEAAAMALAAAAAANAA0AAAIwnCegcpcg4nIw2sRGDZYnBAWiIHJQRZbec5XXEqnrmXIupMWdZGCXlAGhJg0h7lAAADs%3D";
eraseIcon.alt = "erase search pattern";
eraseIcon.style.marginLeft = "3px";
eraseIcon.style.marginRight = "3px";
var isOverview = document.URL.match("/overview-frame\\.html$", "") !== null;
var typeRegex = document.createElement("select");
typeRegex.size = 1;
typeRegex.multiple = false;
if (!isOverview) {
var typeRegexOptionEclipse = document.createElement("option");
typeRegexOptionEclipse.value = ECLIPSE_REGEX_ID;
typeRegexOptionEclipse.appendChild(document.createTextNode("Eclipse"));
typeRegexOptionEclipse.selected = true;
typeRegex.add(typeRegexOptionEclipse);
}
var typeRegexOptionSimplifiedRegex = document.createElement("option");
typeRegexOptionSimplifiedRegex.value = SIMPLIFIED_REGEX_ID;
typeRegexOptionSimplifiedRegex.appendChild(document.createTextNode("Simplified"));
typeRegexOptionSimplifiedRegex.selected = isOverview;
typeRegex.add(typeRegexOptionSimplifiedRegex);
var typeRegexOptionPlainRegex = document.createElement("option");
typeRegexOptionPlainRegex.value = PLAIN_REGEX_ID;
typeRegexOptionPlainRegex.appendChild(document.createTextNode("Regex"));
typeRegex.add(typeRegexOptionPlainRegex);
// Insertion of search elements
searchContainer.appendChild(textInput);
searchContainer.appendChild(eraseIcon);
searchContainer.appendChild(typeRegex);
if (isModernFormat) {
indexContainer = indexContainer[0];
indexContainer.insertBefore(searchContainer, indexContainer.firstChild);
} else {
table = table[0];
table.parentNode.insertBefore(searchContainer, table);
}
// Handle the timeout for text input
var searchTimeoutHandle = null;
var setSearchTimeout = function() {
if (searchTimeoutHandle) {
clearTimeout(searchTimeoutHandle);
}
searchTimeoutHandle = setTimeout(runSearch, 200); // in milliseconds
};
// Run the search
var runSearch = function() {
if (searchTimeoutHandle) {
clearTimeout(searchTimeoutHandle);
searchTimeoutHandle = null;
}
var acceptFunction = function() {
return true;
};
if (textInput.value) {
var regex = textInput.value;
var caseInvariantSwitch = "";
//console.log("input = " + regex);
var selectedTypeRegex = typeRegex.options[typeRegex.selectedIndex].value;
switch (selectedTypeRegex) {
case ECLIPSE_REGEX_ID:
regex = "^.*" + regex.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, "\\$1") + ".*$";
regex = regex.replace(/([A-Z])/g, ".*$1");
caseInvariantSwitch = "i";
break;
case SIMPLIFIED_REGEX_ID:
// Escape all except ? and *
regex = regex.replace(/([\\\^\$+\[\]{}.=!:(|)])/g, "\\$1");
regex = "^.*" + regex.replace(/(?:\*)+/g, ".*").replace(/(?:\?)+/g, ".") + ".*$";
caseInvariantSwitch = "i";
break;
}
//console.log("before = " + regex);
regex = regex.replace(/((.[\*\?])+)(?=\2)/g, "") // Replace successive x* and x? to only one
.replace(/((.)\*\2\?)+/g, ".*") // Replace x*x? with x*
.replace(/((.)\?\2\*)+/g, ".*") // Replace x?x* with x*
.replace(/((.)\+\2\*)+/g, ".+") // Replace x+x* with x+
.replace(/((.)\*\2\+)+/g, ".+") // Replace x*x+ with x+
.replace(/((.[\*\?])+)(?=\2)/g, ""); // Replace successive x* and x? to only one
//console.log("after = " + regex);
try {
var searchRegexp = new RegExp(regex, caseInvariantSwitch);
} catch (err) {
textInput.style.backgroundColor = "Tomato";
return false;
}
textInput.style.backgroundColor = "Lavender";
acceptFunction = function(name) {
return searchRegexp.test(name);
};
} else {
textInput.style.backgroundColor = "White";
}
if (isModernFormat) {
var origUl = indexContainer.getElementsByTagName("ul")[0];
var clonedUl = origUl.cloneNode(true);
(function(items) {
for (var iItem = 0; iItem < items.length; iItem++) {
var liItem = items[iItem];
var itemText = liItem.firstChild.firstChild;
if (itemText.tagName === "I") {
itemText = itemText.firstChild;
}
liItem.style.display = acceptFunction(itemText.nodeValue) ? "" : "none";
}
})(clonedUl.getElementsByTagName("li"));
indexContainer.replaceChild(clonedUl, origUl);
origUl.innerHTML = "";
} else {
var origTBody = table.getElementsByTagName("tbody")[0];
var clonedTBody = origTBody.cloneNode(true);
(function(items) {
for (var iItem = 0; iItem < items.length; iItem++) {
var aItem = items[iItem];
var itemText = aItem.firstChild;
if (itemText.tagName === "I") {
itemText = itemText.firstChild;
}
var displayStyle = acceptFunction(itemText.nodeValue) ? "" : "none";
aItem.style.display = displayStyle;
var nextBr = aItem.nextSibling;
while (nextBr.nodeType !== 1) {
nextBr = nextBr.nextSibling;
}
if (nextBr.tagName === "BR") {
nextBr.style.display = displayStyle;
}
}
})(clonedTBody.getElementsByTagName("a"));
table.replaceChild(clonedTBody, origTBody);
origTBody.innerHTML = "";
}
textInput.focus();
return false;
};
// Change the type of regex
var changeRegexType = function() {
if (textInput.value !== "") {
runSearch(); // will clear the timeout
} else {
textInput.focus();
return false;
}
};
// Erase the current input pattern
var eraseSearch = function() {
if (textInput.value !== "") {
textInput.value = "";
runSearch(); // will clear the timeout
} else {
textInput.focus();
return false;
}
};
// Install all event listeners
addEvent(textInput, "input", setSearchTimeout);
addEvent(typeRegex, "change", changeRegexType);
addEvent(eraseIcon, "click", eraseSearch);
// Uninstall all event listeners
var uninstall = function() {
removeEvent(textInput, "input", setSearchTimeout);
removeEvent(typeRegex, "change", changeRegexType);
removeEvent(eraseIcon, "click", eraseSearch);
};
addEvent(window, "unload", uninstall);
textInput.focus();
return true;
};
// Install the component when window has done loading, and the component is not already here
if (!document.getElementById(ELEMENT_ID)) {
install();
}
})();