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. 20, 2013, 3 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
« no previous file with comments | « popup.html ('k') | webrequest.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: stats.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/stats.js
@@ -0,0 +1,135 @@
+/*
+ * 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 getStats = require("stats").getStats;
+ var FilterNotifier = require("filterNotifier").FilterNotifier;
+
+ var currentTabId;
+ var shareURL = "https://adblockplus.org/";
+
+ var messageMark = {};
+ var shareLinks = {
+ facebook: ["https://www.facebook.com/dialog/feed", {
+ app_id: 475542399197328,
Wladimir Palant 2013/09/24 12:43:36 Just realized that this by far exceeds the 32 bit
+ link: shareURL,
+ redirect_uri: "https://www.facebook.com/",
+ ref: "adcounter",
+ name: messageMark,
+ actions: JSON.stringify([
+ {
+ name: i18n.getMessage("stats_share_download"),
+ link: shareURL
+ }
+ ])
+ }],
+ gplus: ["https://plus.google.com/share", {
+ url: shareURL
+ }],
+ twitter: ["https://twitter.com/intent/tweet", {
+ text: messageMark,
+ url: shareURL,
+ via: "AdblockPlus"
+ }]
+ };
+
+ function createShareLink(network, blockedCount)
+ {
+ var url = shareLinks[network][0];
+ var params = shareLinks[network][1];
+
+ var querystring = [];
+ for (var key in params)
+ {
+ var value = params[key];
+ if (value == messageMark)
+ value = i18n.getMessage("stats_share_message", blockedCount);
+ querystring.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
+ }
+ return url + "?" + querystring.join("&");
+ }
+
+ function onLoad()
+ {
+ document.getElementById("shareBox").addEventListener("click", share, false);
+ document.getElementById("share").addEventListener("click", toggleShareBox, false);
+
+ // Update stats
+ chrome.tabs.query({
+ active: true,
+ windowId: chrome.windows.WINDOW_ID_CURRENT
+ }, function(tabs)
+ {
+ if (tabs.length > 0)
+ {
+ currentTabId = tabs[0].id;
+ updateStats();
+
+ FilterNotifier.addListener(onNotify);
+
+ document.getElementById("statsContainer").removeAttribute("hidden");
+ }
Wladimir Palant 2013/09/24 12:43:36 How about |else window.close()| here just in case?
Thomas Greiner 2013/09/24 14:40:58 Closing the popup right after a user clicked on th
Wladimir Palant 2013/09/25 08:44:51 You are right of course. I forgot that the stats a
+ });
+ }
+
+ function onUnload()
+ {
+ FilterNotifier.removeListener(onNotify);
+ }
+
+ function onNotify(action, item)
+ {
+ if (action == "filter.hitCount")
+ updateStats();
+ }
+
+ function updateStats()
+ {
+ var statsPage = document.getElementById("statsPage");
+ var blockedPage = getStats("blocked", currentTabId).toLocaleString();
+ i18n.setElementText(statsPage, "stats_label_page", [blockedPage]);
+
+ var statsTotal = document.getElementById("statsTotal");
+ var blockedTotal = getStats("blocked").toLocaleString();
+ i18n.setElementText(statsTotal, "stats_label_total", [blockedTotal]);
+ }
+
+ function toggleShareBox(ev)
+ {
+ var shareBox = document.getElementById("shareBox");
+ shareBox.hidden = !shareBox.hidden;
+ }
+
+ function share(ev)
+ {
+ // Easter Egg
+ var blocked = getStats("blocked");
+ if (blocked <= 9000 || blocked >= 10000)
+ blocked = blocked.toLocaleString();
+ else
+ blocked = i18n.getMessage("stats_over", (9000).toLocaleString());
+
+ var url = createShareLink(ev.target.dataset.social, blocked);
+ chrome.tabs.create({url: url});
+ }
+
+ document.addEventListener("DOMContentLoaded", onLoad, false);
+ window.addEventListener("unload", onUnload, false);
+})();
« no previous file with comments | « popup.html ('k') | webrequest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld