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"; |
Wladimir Palant
2013/09/18 13:22:17
Add a slash at the end of the URL?
|
+ var redirectURL = "https://www.facebook.com"; |
Wladimir Palant
2013/09/18 13:22:17
Given that this is Facebook-specific, I don't thin
|
+ |
+ function createShareLink(url, params) |
+ { |
+ var querystring = []; |
+ for (var key in params) |
+ querystring.push(key + "=" + encodeURIComponent(params[key])); |
Wladimir Palant
2013/09/18 13:22:17
encodeURIComponent(key) please, don't assume that
|
+ 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); |
Wladimir Palant
2013/09/18 13:22:17
shareBox.addEventListener("click", share, false) w
|
+ |
+ // update stats |
+ statsTotal.innerText = formatNumber(Stats.total.blocked); |
+ chrome.tabs.query({active: true}, function(tabs) |
Wladimir Palant
2013/09/18 13:22:17
You need windowId: chrome.windows.WINDOW_ID_CURREN
|
+ { |
+ 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); |
Wladimir Palant
2013/09/18 13:22:17
So that's why we have placeholders, so that we can
|
+ }); |
+ } |
+ |
+ function showShareBox(ev) |
+ { |
+ if (shareBox.hasAttribute("hidden")) |
+ shareBox.removeAttribute("hidden"); |
+ else |
+ shareBox.setAttribute("hidden", true); |
Wladimir Palant
2013/09/18 13:22:17
How about:
var shareBox = document.getElementById
|
+ } |
+ |
+ 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)); |
Wladimir Palant
2013/09/18 13:22:17
This is a rather ugly approach, and you forgot to
|
+ |
+ chrome.tabs.create({url: url}); |
+ } |
+ |
+ var locale = require("utils").Utils.appLocale; |
+ function formatNumber(number) |
+ { |
+ return number.toLocaleString(locale); |
+ } |
Wladimir Palant
2013/09/18 13:22:17
Utils.appLocale returns the browser's locale. So t
|
+ |
+ document.addEventListener("DOMContentLoaded", onLoad, false); |
+ window.addEventListener("unload", onUnload, false); |
+})(); |