Skip to content

Instantly share code, notes, and snippets.

@nlwillia
Last active June 6, 2017 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlwillia/206b8cb0da9baffd88bd5bdd5e1b31ce to your computer and use it in GitHub Desktop.
Save nlwillia/206b8cb0da9baffd88bd5bdd5e1b31ce to your computer and use it in GitHub Desktop.
Javadoc Pinning Enhancement Bookmarklet
$(function() {
function addPinningSupport() {
var frame = $(this);
// Clean up
frame.off('load', addPinningSupport);
var body = frame[0].contentDocument.body;
$('.pinning', body).remove();
frame.on('load', addPinningSupport);
$('<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">').appendTo(body);
var index = $('.indexContainer', body);
var container = $('<section role="region" class="pinning">').prependTo(index).append($('<h2>').html('Pinned'));
var list = $('<ul>').appendTo(container);
var pinned = JSON.parse(localStorage[getKey(frame)] || '[]');
$(container).toggle(pinned.length > 0);
$.each(pinned, function(i,href) { createPinnedElement(href); });
function createPinnedElement(href) {
var source = $('a[href="' + href + '"]', index);
if (source.length) {
var li = $('<li>').appendTo(list);
$('<a>').attr('href', href).attr('target', source.attr('target')).html(source.html()).appendTo(li);
return li;
}
}
function decorate(li) {
$('<span class="pinning">').html('&ensp;').appendTo(li);
$('<i class="fa fa-thumb-tack pinning">').click(pinHandler).appendTo(li);
}
function getKey(frame) {
return 'javadoc-pinning.' + frame[0].contentWindow.location.pathname;
}
function sortKey(li) {
return ($('a', li).text() || '').toLowerCase();
}
function pinHandler() {
var href = $('a', $(this).parent()).attr('href');
var index = pinned.indexOf(href);
if (index === -1) {
pinned.push(href);
decorate(createPinnedElement(href));
pinned.sort();
var sortable = list.children().get();
sortable.sort(function(a, b) { return sortKey(a).localeCompare(sortKey(b)); });
$.each(sortable, function(i, li) {
$(li).appendTo(list);
});
} else {
pinned.splice(index, 1);
$('a[href="' + href + '"]', list).parent().remove();
}
localStorage[getKey(frame)] = JSON.stringify(pinned);
$(container).toggle(pinned.length > 0);
}
$('li', index).each(function() {
decorate($(this));
});
}
addPinningSupport.apply($('frame,iframe').filter('[name=packageListFrame]'));
addPinningSupport.apply($('frame,iframe').filter('[name=packageFrame]'));
});
@nlwillia
Copy link
Author

nlwillia commented Jun 3, 2017

Javadoc Enhancement

Javadoc is great, but I find that I routinely work with only a subset of packages or classes, and it's sometimes difficult to find them in a big package or class list. This is a bookmarklet to add pinning support to the lists in the two navigation frames. It may be buggy across the different flavors of javadoc renderings that are out there, but it seems to work well enough with JDK8 and JDK9.

This is mainly a hack for my personal sanity, but anyone else is welcome to appropriate it for their own purposes if it's useful.

How to use it

Add a bookmark to your browser of choice pasting the following as the link address.

javascript:(function() {function getScript(url, success) {var script = document.createElement('script');script.src = url;script.onload = success;document.getElementsByTagName('head')[0].appendChild(script);}getScript('//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js', function() {if (window.frames[0]) {getScript('//rawgit.com/nlwillia/206b8cb0da9baffd88bd5bdd5e1b31ce/raw/858f80283de7c7e546f9d2254faedcfc1ca84555/javadoc-pinning.js');if (window.frames[0].frameElement.nodeName === 'FRAME') {getScript('//cdn.rawgit.com/haasted/Javadoc-Enhancer/e9a0e7f8f165b2f26c4dddb6efa3d1849c666ba8/jdocenhance.min.js');}}});})()

When viewing a javadoc page, click the bookmark to load the script and enable the extra functionality.

What it does

It injects three scripts into the page: jquery, the file above (via rawgit), and @haasted's Javadoc-Enhancer (which is awesome and what inspired me to address this longstanding personal gripe). FontAwesome will also be pulled in.

You should see thumb pin icons beside the links in the navigation frames on the left. Click them, and that item will be added to a Pinned list at the top. Click the pin beside an already pinned item to remove it. That's basically all there is to it.

The pinned list is persisted in your browser's localStorage area, and should restore itself when you return to the page and rerun the bookmarklet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment