Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: stats.js

Issue 11627039: Added ad counting functionality (Closed)
Patch Set: Created Sept. 11, 2013, 3:10 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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: chrome.i18n.getMessage("stats_share_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: chrome.i18n.getMessage("stats_share_message"),
+ url: shareURL,
+ via: "AdblockPlus"
+ })
+ };
+
+ function onLoad()
+ {
+ statsPage = document.getElementById("statsPage");
+ statsTotal = document.getElementById("statsTotal");
+
+ 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
Thomas Greiner 2013/09/11 15:36:41 Let me know what you think of it (context: https:/
+ 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("_TOTAL_", blocked);
+
+ chrome.tabs.create({url: url});
+ }
+
+ var locale = chrome.i18n.getMessage("@@ui_locale");
+ function formatNumber(number)
+ {
+ return number.toLocaleString(locale);
+ }
+
+ document.addEventListener("DOMContentLoaded", onLoad, false);
+ window.addEventListener("unload", onUnload, false);
+})();

Powered by Google App Engine
This is Rietveld