| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * This file is part of Adblock Plus <http://adblockplus.org/>, |    2  * This file is part of Adblock Plus <http://adblockplus.org/>, | 
|    3  * Copyright (C) 2006-2013 Eyeo GmbH |    3  * Copyright (C) 2006-2013 Eyeo GmbH | 
|    4  * |    4  * | 
|    5  * Adblock Plus is free software: you can redistribute it and/or modify |    5  * Adblock Plus is free software: you can redistribute it and/or modify | 
|    6  * it under the terms of the GNU General Public License version 3 as |    6  * it under the terms of the GNU General Public License version 3 as | 
|    7  * published by the Free Software Foundation. |    7  * published by the Free Software Foundation. | 
|    8  * |    8  * | 
|    9  * Adblock Plus is distributed in the hope that it will be useful, |    9  * Adblock Plus is distributed in the hope that it will be useful, | 
|   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of |   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the |   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|   12  * GNU General Public License for more details. |   12  * GNU General Public License for more details. | 
|   13  * |   13  * | 
|   14  * You should have received a copy of the GNU General Public License |   14  * You should have received a copy of the GNU General Public License | 
|   15  * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. |   15  * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
|   16  */ |   16  */ | 
|   17  |   17  | 
|   18 /** |   18 /** | 
|   19  * @fileOverview Provides usage stats |   19  * @fileOverview Provides usage stats | 
|   20  */ |   20  */ | 
|   21  |   21  | 
|   22 let {Prefs} = require("prefs"); |   22 let {Prefs} = require("prefs"); | 
|   23 let {BlockingFilter} = require("filterClasses"); |   23 let {BlockingFilter} = require("filterClasses"); | 
|   24 let {FilterNotifier} = require("filterNotifier"); |   24 let {FilterNotifier} = require("filterNotifier"); | 
|   25  |   25  | 
 |   26 let badgeColor = [100, 100, 100, 255]; | 
 |   27  | 
|   26 /** |   28 /** | 
|   27  * Get statistics for specified tab |   29  * Get statistics for specified tab | 
|   28  * @param  {String} key   field key |   30  * @param  {String} key   field key | 
|   29  * @param  {Number} tabId tab ID (leave undefined for total stats) |   31  * @param  {Number} tabId tab ID (leave undefined for total stats) | 
|   30  * @return {Number}       field value |   32  * @return {Number}       field value | 
|   31  */ |   33  */ | 
|   32 let getStats = exports.getStats = function getStats(key, tabId) |   34 let getStats = exports.getStats = function getStats(key, tabId) | 
|   33 { |   35 { | 
|   34   if (tabId) |   36   if (tabId) | 
|   35   { |   37   { | 
|   36     let frameData = getFrameData(tabId, 0); |   38     let frameData = getFrameData(tabId, 0); | 
|   37     return (frameData && key in frameData ? frameData[key] : 0); |   39     return (frameData && key in frameData ? frameData[key] : 0); | 
|   38   } |   40   } | 
|   39   else |   41   else | 
|   40     return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); |   42     return (key in Prefs.stats_total ? Prefs.stats_total[key] : 0); | 
|   41 }; |   43 }; | 
|   42  |   44  | 
|   43 FilterNotifier.addListener(function(action, item, newValue, oldValue, tabId) |   45 FilterNotifier.addListener(function(action, item, newValue, oldValue, tabId) | 
|   44 { |   46 { | 
|   45   if (action != "filter.hitCount") |   47   if (action != "filter.hitCount") | 
|   46     return; |   48     return; | 
|   47    |   49    | 
|   48   var blocked = item instanceof BlockingFilter; |   50   let blocked = item instanceof BlockingFilter; | 
|   49    |   51    | 
|   50   // Increment counts |   52   // Increment counts | 
|   51   if (blocked) |   53   if (blocked) | 
|   52   { |   54   { | 
|   53     if ("blocked" in Prefs.stats_total) |   55     if ("blocked" in Prefs.stats_total) | 
|   54       Prefs.stats_total.blocked++; |   56       Prefs.stats_total.blocked++; | 
|   55     else |   57     else | 
|   56       Prefs.stats_total.blocked = 1; |   58       Prefs.stats_total.blocked = 1; | 
|   57     Prefs.stats_total = Prefs.stats_total; |   59     Prefs.stats_total = Prefs.stats_total; | 
|   58      |   60      | 
|   59     let frameData = getFrameData(tabId, 0); |   61     let frameData = getFrameData(tabId, 0); | 
|   60     if (frameData) |   62     if (frameData) | 
|   61     { |   63     { | 
|   62       if ("blocked" in frameData) |   64       if ("blocked" in frameData) | 
|   63         frameData.blocked++; |   65         frameData.blocked++; | 
|   64       else |   66       else | 
|   65         frameData.blocked = 1; |   67         frameData.blocked = 1; | 
|   66     } |   68     } | 
 |   69      | 
 |   70     // Update number in icon | 
 |   71     if (Prefs.show_iconnumber) | 
 |   72     { | 
 |   73       chrome.browserAction.setBadgeBackgroundColor({ | 
 |   74         color: badgeColor | 
 |   75       }); | 
 |   76        | 
 |   77       chrome.browserAction.setBadgeText({ | 
 |   78         tabId: tabId, | 
 |   79         text: frameData.blocked.toString() | 
 |   80       }); | 
 |   81     } | 
|   67   } |   82   } | 
|   68 }); |   83 }); | 
 |   84  | 
 |   85 /** | 
 |   86  * Execute function for each tab in any window | 
 |   87  * @param  {Function} func function to be executed | 
 |   88  */ | 
 |   89  | 
 |   90 function forEachTab(func) | 
 |   91 { | 
 |   92   chrome.windows.getAll(function(windows) | 
 |   93   { | 
 |   94     for (let i = 0; i < windows.length; i++) | 
 |   95     { | 
 |   96       chrome.tabs.query({ | 
 |   97         windowId: windows[i].id | 
 |   98       }, function(tabs) | 
 |   99       { | 
 |  100         for (let i = 0; i < tabs.length; i++) | 
 |  101           func(tabs[i]); | 
 |  102       }); | 
 |  103     } | 
 |  104   }); | 
 |  105 } | 
 |  106  | 
 |  107 Prefs.addListener(function(name) | 
 |  108 { | 
 |  109   if (name != "show_iconnumber") | 
 |  110     return; | 
 |  111    | 
 |  112   if (Prefs.show_iconnumber) | 
 |  113   { | 
 |  114     // Add number to icon | 
 |  115     chrome.browserAction.setBadgeBackgroundColor({ | 
 |  116       color: badgeColor | 
 |  117     }); | 
 |  118      | 
 |  119     forEachTab(function(tab) { | 
 |  120       let frameData = getFrameData(tab.id, 0); | 
 |  121       if (!frameData || !("blocked" in frameData)) | 
 |  122         return; | 
 |  123        | 
 |  124       chrome.browserAction.setBadgeText({ | 
 |  125         tabId: tab.id, | 
 |  126         text: frameData.blocked.toString() | 
 |  127       }); | 
 |  128     }); | 
 |  129   } | 
 |  130   else | 
 |  131   { | 
 |  132     // Remove number from icon | 
 |  133     forEachTab(function(tab) | 
 |  134     { | 
 |  135       chrome.browserAction.setBadgeText({ | 
 |  136         tabId: tab.id, | 
 |  137         text: "" | 
 |  138       }); | 
 |  139     }); | 
 |  140   } | 
 |  141 }); | 
| OLD | NEW |