addon-sdk/source/examples/annotator/data/selector.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 /*
     6 The selector locates elements that are suitable for annotation and enables
     7 the user to select them.
     9 On 'mouseenter' events associated with <p> elements:
    10 - if the selector is active and the element is not already annotated
    11 - find the nearest ancestor which has an id attribute: this is supposed to
    12 make identification of this element more accurate
    13 - highlight the element
    14 - bind 'click' for the element to send a message back to the add-on, including
    15 all the information associated with the anchor.
    16 */
    18 var matchedElement = null;
    19 var originalBgColor = null;
    20 var active = false;
    22 function resetMatchedElement() {
    23   if (matchedElement) {
    24     matchedElement.css('background-color', originalBgColor);
    25     matchedElement.unbind('click.annotator');
    26   }
    27 }
    29 self.on('message', function onMessage(activation) {
    30   active = activation;
    31   if (!active) {
    32     resetMatchedElement();
    33   }
    34 });
    36 function getInnerText(element) {
    37   // jQuery.text() returns content of <script> tags, we need to ignore those
    38   var list = [];
    39   element.find("*").andSelf().contents()
    40     .filter(function () {
    41       return this.nodeType == 3 && this.parentNode.tagName != "SCRIPT";
    42     })
    43     .each(function () {
    44       list.push(this.nodeValue);
    45     });
    46   return list.join("");
    47 }
    49 $('*').mouseenter(function() {
    50   if (!active || $(this).hasClass('annotated')) {
    51     return;
    52   }
    53   resetMatchedElement();
    54   ancestor = $(this).closest("[id]");
    55   matchedElement = $(this).first();
    56   originalBgColor = matchedElement.css('background-color');
    57   matchedElement.css('background-color', 'yellow');
    58   matchedElement.bind('click.annotator', function(event) {
    59     event.stopPropagation();
    60     event.preventDefault();
    61     self.port.emit('show',
    62       [
    63         document.location.toString(),
    64         ancestor.attr("id"),
    65         getInnerText(matchedElement)
    66       ]
    67    );
    68   });
    69 });
    71 $('*').mouseout(function() {
    72   resetMatchedElement();
    73 });

mercurial