Last active
June 6, 2017 19:21
-
-
Save nlwillia/206b8cb0da9baffd88bd5bdd5e1b31ce to your computer and use it in GitHub Desktop.
Javadoc Pinning Enhancement Bookmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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(' ').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]')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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.