michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: Locate anchors for annotations and prepare to display the annotations. michael@0: michael@0: For each annotation, if its URL matches this page, michael@0: - get the ancestor whose ID matches the ID in the anchor michael@0: - look for a
element whose content contains the anchor text michael@0: michael@0: That's considered a match. Then we: michael@0: - highlight the anchor element michael@0: - add an 'annotated' class to tell the selector to skip this element michael@0: - embed the annottion text as a new attribute michael@0: michael@0: For all annotated elements: michael@0: - bind 'mouseenter' and 'mouseleave' events to the element, to send 'show' michael@0: and 'hide' messages back to the add-on. michael@0: */ michael@0: michael@0: self.on('message', function onMessage(annotations) { michael@0: annotations.forEach( michael@0: function(annotation) { michael@0: if(annotation.url == document.location.toString()) { michael@0: createAnchor(annotation); michael@0: } michael@0: }); michael@0: michael@0: $('.annotated').css('border', 'solid 3px yellow'); michael@0: michael@0: $('.annotated').bind('mouseenter', function(event) { michael@0: self.port.emit('show', $(this).attr('annotation')); michael@0: event.stopPropagation(); michael@0: event.preventDefault(); michael@0: }); michael@0: michael@0: $('.annotated').bind('mouseleave', function() { michael@0: self.port.emit('hide'); michael@0: }); michael@0: }); michael@0: michael@0: michael@0: function createAnchor(annotation) { michael@0: annotationAnchorAncestor = $('#' + annotation.ancestorId)[0] || document.body; michael@0: annotationAnchor = $(annotationAnchorAncestor).parent().find( michael@0: ':contains("' + annotation.anchorText + '")').last(); michael@0: annotationAnchor.addClass('annotated'); michael@0: annotationAnchor.attr('annotation', annotation.annotationText); michael@0: }