Advertisement
Guest User

Untitled

a guest
Jan 20th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@author slimx
  2. (function() {
  3.     //add label to findbar
  4.     var status = document.getAnonymousElementByAttribute(gFindBar, 'anonid', 'match-case-status');
  5.     var sep = document.createElement("toolbarspacer");
  6.     var count = document.createElement("label");
  7.     count.hidden = true;
  8.     status.parentNode.insertBefore(sep, status);
  9.     status.parentNode.insertBefore(count, status);//findbar-container
  10.  
  11.  
  12.     gFindBar.__proto__._foundMatches = count;
  13.    
  14.     gFindBar.__proto__._updateMatchesCount = function(aRes) {
  15.         if (!this._updateMatchCountTimeout)
  16.             window.clearTimeout(this._updateMatchCountTimeout);
  17.         this._updateMatchCountTimeout =
  18.                 window.setTimeout(function(aRes, aSelf) {
  19.                     aSelf._updateMatchesCountWorker(aRes);
  20.                 }, 0, aRes, this);
  21.     }
  22.  
  23.     gFindBar.__proto__._updateMatchesCountWorker = function(aRes) {
  24.         var word = this._findField.value;
  25.         if (aRes == this.nsITypeAheadFind.FIND_NOTFOUND || !word) {
  26.             this._foundMatches.hidden = true;
  27.             this._foundMatches.value = "";
  28.         }
  29.         else {
  30.             var matchesCount = this._countMatches(word).toString();
  31.             if (matchesCount != "0") {
  32.                 if (matchesCount == "1")
  33.                     this._foundMatches.value = matchesCount + " Treffer";
  34.                 else if (matchesCount == "-1") {
  35.                     var matchLimit = 100;
  36.                     this._foundMatches.value = "Ueber 100 Treffer";
  37.                     //                    var key = (matchLimit > 1000) ? "Decrease" : "Increase";
  38.                 } else
  39.                     this._foundMatches.value = matchesCount + " Treffer";
  40.                 this._foundMatches.hidden = false;
  41.             }
  42.             else {
  43.                 this._foundMatches.hidden = true;
  44.                 this._foundMatches.value = "";
  45.             }
  46.  
  47.             window.clearTimeout(this._updateMatchCountTimeout);
  48.         }
  49.     }
  50.  
  51.     gFindBar.__proto__._countMatches = function(aWord, aWindow) {
  52.         var win = aWindow || this.browser.contentWindow;
  53.  
  54.         var countFound = 0;
  55.         for (var i = 0, count; win.frames && i < win.frames.length; i++) {
  56.             if ((count = this._countMatches(aWord, win.frames[i])) != -1)
  57.                 countFound += count;
  58.             else
  59.                 return count;
  60.         }
  61.  
  62.         var doc = win.document;
  63.         if (!doc || !(doc instanceof HTMLDocument))
  64.             return countFound;
  65.  
  66.         var body = doc.body;
  67.  
  68.         var count = body.childNodes.length;
  69.         var searchRange = doc.createRange();
  70.         var startPt = doc.createRange();
  71.         var endPt = doc.createRange();
  72.  
  73.         searchRange.setStart(body, 0);
  74.         searchRange.setEnd(body, count);
  75.  
  76.         startPt.setStart(body, 0);
  77.         startPt.setEnd(body, 0);
  78.         endPt.setStart(body, count);
  79.         endPt.setEnd(body, count);
  80.  
  81.         var retRange = null;
  82.         var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"]
  83.                 .createInstance()
  84.                 .QueryInterface(Components.interfaces.nsIFind);
  85.  
  86.         finder.caseSensitive = this._shouldBeCaseSensitive(aWord);
  87.  
  88.         var matchLimit = 100;
  89.         while ((retRange = finder.Find(aWord, searchRange, startPt, endPt))) {
  90.             if (this._rangeIsVisible(retRange, win)) {
  91.                 if (this._findMode == this.FIND_LINKS) {
  92.                     if (this._rangeStartsInLink(retRange))
  93.                         ++ countFound;
  94.                 }
  95.                 else
  96.                     ++ countFound;
  97.             }
  98.             if (countFound == matchLimit) {
  99.                 countFound = -1;
  100.                 break;
  101.             }
  102.             startPt = doc.createRange();
  103.             startPt.setStart(retRange.startContainer, retRange.startOffset + 1);
  104.         }
  105.  
  106.         return countFound;
  107.     }
  108.  
  109.     gFindBar.__proto__._rangeIsVisible = function(aRange, aWindow) {
  110.         var node = aRange.startContainer;
  111.  
  112.         if (node.nodeType == node.ELEMENT_NODE) {
  113.             if (node.hasChildNodes) {
  114.                 var childNode = node.childNodes[aRange.startOffset];
  115.                 if (childNode)
  116.                     node = childNode;
  117.             }
  118.         }
  119.  
  120.         while (node && node.nodeType != node.ELEMENT_NODE)
  121.             node = node.parentNode;
  122.  
  123.         // There is no perfect way to check if a node is visible in JavaScript,
  124.         // so use the best measures we can have
  125.         if (node) {
  126.             var style = aWindow.getComputedStyle(node, "");
  127.             if (style) {
  128.                 if (style.visibility == "hidden" ||
  129.                         style.visibility == "collapse" ||
  130.                         style.display == "none")
  131.                     return false;
  132.                 if (style.left != "auto" && style.width != "auto")
  133.                     if (style.left < 0 && style.left + style.width < 0)
  134.                         return false;
  135.                 if (style.top != "auto" && style.height != "auto")
  136.                     if (style.top < 0 && style.top + style.height < 0)
  137.                         return false;
  138.             }
  139.         }
  140.  
  141.         return true;
  142.     }
  143.  
  144.     gFindBar.__proto__._rangeStartsInLink = function(aRange) {
  145.         var isInsideLink = false;
  146.  
  147.         var node = aRange.startContainer;
  148.  
  149.         if (node.nodeType == node.ELEMENT_NODE) {
  150.             if (node.hasChildNodes) {
  151.                 var childNode = node.childNodes[aRange.startOffset];
  152.                 if (childNode)
  153.                     node = childNode;
  154.             }
  155.         }
  156.  
  157.         const XLink_NS = "http://www.w3.org/1999/xlink";
  158.         do {
  159.             if (node instanceof HTMLAnchorElement) {
  160.                 isInsideLink = node.hasAttribute("href");
  161.                 break;
  162.             }
  163.             else if (typeof node.hasAttributeNS == "function" &&
  164.                     node.hasAttributeNS(XLink_NS, "href")) {
  165.                 isInsideLink = (node.getAttributeNS(XLink_NS, "type") == "simple");
  166.                 break;
  167.             }
  168.  
  169.             node = node.parentNode;
  170.         } while (node);
  171.  
  172.         return isInsideLink;
  173.     }
  174.  
  175.     //insert count function to original
  176.     eval("gFindBar._updateCaseSensitivity=" + gFindBar._updateCaseSensitivity.toString().slice(0, -1) + "this._updateMatchesCount();}");
  177.     eval("gFindBar._updateStatusUI=" + gFindBar._updateStatusUI.toString().slice(0, -1) + "this._updateMatchesCount();}");
  178. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement