Index: stats.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/stats.js |
@@ -0,0 +1,148 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2013 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+(function() |
+{ |
+ var backgroundPage = chrome.extension.getBackgroundPage(); |
+ var require = backgroundPage.require; |
+ var ChromeCompat = backgroundPage.ChromeCompat; |
+ var stats = require("stats"); |
+ var StatsObject = stats.StatsObject; |
+ var Stats = stats.Stats; |
+ var FilterNotifier = require("filterNotifier").FilterNotifier; |
+ |
+ var currentTabId; |
+ var statsPage; |
+ var statsTotal; |
+ var shareBox; |
+ var shareURL = "https://adblockplus.org"; |
+ var redirectURL = "https://www.facebook.com"; |
+ |
+ function createShareLink(url, params) |
+ { |
+ var querystring = []; |
+ for (var key in params) |
+ querystring.push(key + "=" + encodeURIComponent(params[key])); |
+ return url + "?" + querystring.join("&"); |
+ } |
+ |
+ var shareLinks = { |
+ facebook: createShareLink("https://www.facebook.com/dialog/feed", { |
+ app_id: 475542399197328, |
+ link: shareURL, |
+ redirect_uri: redirectURL, |
+ ref: "adcounter", |
+ name: "_MESSAGE_", |
+ actions: JSON.stringify([ |
+ { |
+ name: chrome.i18n.getMessage("stats_share_download"), |
+ link: shareURL |
+ } |
+ ]) |
+ }), |
+ gplus: createShareLink("https://plus.google.com/share", { |
+ url: shareURL |
+ }), |
+ twitter: createShareLink("https://twitter.com/intent/tweet", { |
+ text: "_MESSAGE_", |
+ url: shareURL, |
+ via: "AdblockPlus" |
+ }) |
+ }; |
+ |
+ function onLoad() |
+ { |
+ statsPage = document.querySelector("#statsPage > strong"); |
+ statsTotal = document.querySelector("#statsTotal > strong"); |
+ |
+ shareBox = document.getElementById("shareBox"); |
+ var shareBoxLink = document.getElementById("share"); |
+ shareBoxLink.addEventListener("click", showShareBox, false); |
+ |
+ var shareActions = document.querySelectorAll("#shareBox > a"); |
+ for (var i = 0; i < shareActions.length; i++) |
+ shareActions[i].addEventListener("click", share, false); |
+ |
+ // update stats |
+ statsTotal.innerText = formatNumber(Stats.total.blocked); |
+ chrome.tabs.query({active: true}, function(tabs) |
+ { |
+ if (tabs.length > 0) |
+ { |
+ currentTabId = tabs[0].id; |
+ updateStats(); |
+ |
+ FilterNotifier.addListener(onNotify); |
+ |
+ document.getElementById("statsStuff").removeAttribute("hidden"); |
+ } |
+ }); |
+ } |
+ |
+ function onUnload() |
+ { |
+ FilterNotifier.removeListener(onNotify); |
+ } |
+ |
+ function onNotify(action, item) |
+ { |
+ if (action == "document.stats") |
+ updateStats(); |
+ } |
+ |
+ function updateStats() |
+ { |
+ ChromeCompat.tabs.sendMessage(currentTabId, {reqtype: "get-stats"}, function(response) |
+ { |
+ var page = new StatsObject(response); |
+ statsPage.innerText = formatNumber(page.blocked); |
+ statsTotal.innerText = formatNumber(Stats.total.blocked); |
+ }); |
+ } |
+ |
+ function showShareBox(ev) |
+ { |
+ if (shareBox.hasAttribute("hidden")) |
+ shareBox.removeAttribute("hidden"); |
+ else |
+ shareBox.setAttribute("hidden", true); |
+ } |
+ |
+ function share(ev) |
+ { |
+ // Easter Egg |
+ var blocked = Stats.total.blocked; |
+ if (blocked <= 9000 || blocked >= 10000) |
+ blocked = formatNumber(blocked); |
+ else |
+ blocked = chrome.i18n.getMessage("stats_over", formatNumber(9000)); |
+ |
+ var url = shareLinks[ev.target.dataset.social] |
+ .replace("_MESSAGE_", chrome.i18n.getMessage("stats_share_message", blocked)); |
+ |
+ chrome.tabs.create({url: url}); |
+ } |
+ |
+ var locale = require("utils").Utils.appLocale; |
+ function formatNumber(number) |
+ { |
+ return number.toLocaleString(locale); |
+ } |
+ |
+ document.addEventListener("DOMContentLoaded", onLoad, false); |
+ window.addEventListener("unload", onUnload, false); |
+})(); |