Greasemonkey User Script: Javadoc Instant Search
This Greasemonkey script adds Instant Search function to Javadoc class frame.
- Three search pattern modes (choose in the selector):
- Eclipse: search around capitals, for exemple
NullPointerException(as in the Eclipse IDE Java's type search).
?to replace one character,
*to replace any number of characters, for example
- Regex: for real men, use a plain regex here, for example
- Pattern background becomes:
- white if no pattern;
- blue if active pattern;
- red if error in pattern.
- Button to erase pattern.
In Google Chrome, userscripts support is native since Chrome v4.
- Fixed: Issue #1 Incorrect search on doubles. The real reason was a side effect of regex reuse, see Regex/lastIndex - Unexpected behaviour on StackOverflow.
- Fixed: heavy DOM manipulation caused massive slowdown and even stalled and crashed Google Chrome. As a side effect, also improved browser response.
- Fixed: regexes (improper escaping and bad patterns).
- Modified: Eclipse and Simplified search pattern modes are case-insensitive (as I found in Eclipse IDE).
- Modified: code checked through the excellent JSHint.
- Added: support for old javadoc versions (see Implementation details).
- Fixed: minor fixes (set focus on startup only if successfully installed; new filename pattern).
- Fixed: simplified pattern mode was wrong interpreting
?as "0 or 1 any character" instead of "1 any character".
- Added: 200-millisecond delay between input in pattern box and search run; this improves responsiveness while typing a pattern.
- Added: clicking on erase, or selecting of regex type, will then set focus on the pattern box.
- Initial version, written minimal documentation, uploaded to [userscripts.org][Userscripts] page.
There were three goals:
- A userscript for Greasemonkey plugin : done.
- A doclet extension: can't be done because a new doclet can't reuse standard doclet mecanism.
- A tool to insert the userscript into generated Javadoc : to be done.
Later, I found KOSEKI Kengo's script named Javadoc Incremental Search. This code and webpage inspired me (for example the embedded erase icon comes directly from it), but my implementation is really different: my code is simpler because it does less things.
I've found two formats of generated javadoc:
The modern hierarchy:
html head body h1 (title) div class="indexContainer" ul (one for interfaces, one for classes) li a (for every item) ...
The old hierarchy:
HTML HEAD BODY FONT B (title) BR TABLE TR TD FONT A BR (for every item)
Given a corresponding page filename, if we find a div whose class is
indexContainerthen the modern hierarchy is assumed, otherwise we choose the old hierarchy (which is supported from v0.3).
The search component is a paragraph whose id is
javadocInstantSearchElement. This id is looked for to avoid reinstalling the search component.
The search component is added as a first child of the element whose class is
The regex transformation from Eclipse model to full regex follows these steps:
- Neutralize all special caracters:
.*$to allow search in any position.
.*before all capital letters:
All regexes are simplified using the following replaces:
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