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

Delta Between Two Patch Sets: stats.js

Issue 11627039: Added ad counting functionality (Closed)
Left Patch Set: Changed strings & using appLocale Created Sept. 18, 2013, 9:45 a.m.
Right Patch Set: Created Sept. 20, 2013, 3 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « popup.html ('k') | webrequest.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 (function() 18 (function()
19 { 19 {
20 var backgroundPage = chrome.extension.getBackgroundPage(); 20 var backgroundPage = chrome.extension.getBackgroundPage();
21 var require = backgroundPage.require; 21 var require = backgroundPage.require;
22 var ChromeCompat = backgroundPage.ChromeCompat; 22 var getStats = require("stats").getStats;
23 var stats = require("stats");
24 var StatsObject = stats.StatsObject;
25 var Stats = stats.Stats;
26 var FilterNotifier = require("filterNotifier").FilterNotifier; 23 var FilterNotifier = require("filterNotifier").FilterNotifier;
27 24
28 var currentTabId; 25 var currentTabId;
29 var statsPage; 26 var shareURL = "https://adblockplus.org/";
30 var statsTotal;
31 var shareBox;
32 var shareURL = "https://adblockplus.org";
Wladimir Palant 2013/09/18 13:22:17 Add a slash at the end of the URL?
33 var redirectURL = "https://www.facebook.com";
Wladimir Palant 2013/09/18 13:22:17 Given that this is Facebook-specific, I don't thin
34 27
35 function createShareLink(url, params) 28 var messageMark = {};
29 var shareLinks = {
30 facebook: ["https://www.facebook.com/dialog/feed", {
31 app_id: 475542399197328,
Wladimir Palant 2013/09/24 12:43:36 Just realized that this by far exceeds the 32 bit
32 link: shareURL,
33 redirect_uri: "https://www.facebook.com/",
34 ref: "adcounter",
35 name: messageMark,
36 actions: JSON.stringify([
37 {
38 name: i18n.getMessage("stats_share_download"),
39 link: shareURL
40 }
41 ])
42 }],
43 gplus: ["https://plus.google.com/share", {
44 url: shareURL
45 }],
46 twitter: ["https://twitter.com/intent/tweet", {
47 text: messageMark,
48 url: shareURL,
49 via: "AdblockPlus"
50 }]
51 };
52
53 function createShareLink(network, blockedCount)
36 { 54 {
55 var url = shareLinks[network][0];
56 var params = shareLinks[network][1];
57
37 var querystring = []; 58 var querystring = [];
38 for (var key in params) 59 for (var key in params)
39 querystring.push(key + "=" + encodeURIComponent(params[key])); 60 {
Wladimir Palant 2013/09/18 13:22:17 encodeURIComponent(key) please, don't assume that
61 var value = params[key];
62 if (value == messageMark)
63 value = i18n.getMessage("stats_share_message", blockedCount);
64 querystring.push(encodeURIComponent(key) + "=" + encodeURIComponent(value) );
65 }
40 return url + "?" + querystring.join("&"); 66 return url + "?" + querystring.join("&");
41 } 67 }
42 68
43 var shareLinks = {
44 facebook: createShareLink("https://www.facebook.com/dialog/feed", {
45 app_id: 475542399197328,
46 link: shareURL,
47 redirect_uri: redirectURL,
48 ref: "adcounter",
49 name: "_MESSAGE_",
50 actions: JSON.stringify([
51 {
52 name: chrome.i18n.getMessage("stats_share_download"),
53 link: shareURL
54 }
55 ])
56 }),
57 gplus: createShareLink("https://plus.google.com/share", {
58 url: shareURL
59 }),
60 twitter: createShareLink("https://twitter.com/intent/tweet", {
61 text: "_MESSAGE_",
62 url: shareURL,
63 via: "AdblockPlus"
64 })
65 };
66
67 function onLoad() 69 function onLoad()
68 { 70 {
69 statsPage = document.querySelector("#statsPage > strong"); 71 document.getElementById("shareBox").addEventListener("click", share, false);
70 statsTotal = document.querySelector("#statsTotal > strong"); 72 document.getElementById("share").addEventListener("click", toggleShareBox, f alse);
71 73
72 shareBox = document.getElementById("shareBox"); 74 // Update stats
73 var shareBoxLink = document.getElementById("share"); 75 chrome.tabs.query({
74 shareBoxLink.addEventListener("click", showShareBox, false); 76 active: true,
75 77 windowId: chrome.windows.WINDOW_ID_CURRENT
76 var shareActions = document.querySelectorAll("#shareBox > a"); 78 }, function(tabs)
77 for (var i = 0; i < shareActions.length; i++)
78 shareActions[i].addEventListener("click", share, false);
Wladimir Palant 2013/09/18 13:22:17 shareBox.addEventListener("click", share, false) w
79
80 // update stats
81 statsTotal.innerText = formatNumber(Stats.total.blocked);
82 chrome.tabs.query({active: true}, function(tabs)
Wladimir Palant 2013/09/18 13:22:17 You need windowId: chrome.windows.WINDOW_ID_CURREN
83 { 79 {
84 if (tabs.length > 0) 80 if (tabs.length > 0)
85 { 81 {
86 currentTabId = tabs[0].id; 82 currentTabId = tabs[0].id;
87 updateStats(); 83 updateStats();
88 84
89 FilterNotifier.addListener(onNotify); 85 FilterNotifier.addListener(onNotify);
90 86
91 document.getElementById("statsStuff").removeAttribute("hidden"); 87 document.getElementById("statsContainer").removeAttribute("hidden");
92 } 88 }
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
93 }); 89 });
94 } 90 }
95 91
96 function onUnload() 92 function onUnload()
97 { 93 {
98 FilterNotifier.removeListener(onNotify); 94 FilterNotifier.removeListener(onNotify);
99 } 95 }
100 96
101 function onNotify(action, item) 97 function onNotify(action, item)
102 { 98 {
103 if (action == "document.stats") 99 if (action == "filter.hitCount")
104 updateStats(); 100 updateStats();
105 } 101 }
106 102
107 function updateStats() 103 function updateStats()
108 { 104 {
109 ChromeCompat.tabs.sendMessage(currentTabId, {reqtype: "get-stats"}, function (response) 105 var statsPage = document.getElementById("statsPage");
110 { 106 var blockedPage = getStats("blocked", currentTabId).toLocaleString();
111 var page = new StatsObject(response); 107 i18n.setElementText(statsPage, "stats_label_page", [blockedPage]);
112 statsPage.innerText = formatNumber(page.blocked); 108
113 statsTotal.innerText = formatNumber(Stats.total.blocked); 109 var statsTotal = document.getElementById("statsTotal");
Wladimir Palant 2013/09/18 13:22:17 So that's why we have placeholders, so that we can
114 }); 110 var blockedTotal = getStats("blocked").toLocaleString();
111 i18n.setElementText(statsTotal, "stats_label_total", [blockedTotal]);
115 } 112 }
116 113
117 function showShareBox(ev) 114 function toggleShareBox(ev)
118 { 115 {
119 if (shareBox.hasAttribute("hidden")) 116 var shareBox = document.getElementById("shareBox");
120 shareBox.removeAttribute("hidden"); 117 shareBox.hidden = !shareBox.hidden;
121 else
122 shareBox.setAttribute("hidden", true);
Wladimir Palant 2013/09/18 13:22:17 How about: var shareBox = document.getElementById
123 } 118 }
124 119
125 function share(ev) 120 function share(ev)
126 { 121 {
127 // Easter Egg 122 // Easter Egg
128 var blocked = Stats.total.blocked; 123 var blocked = getStats("blocked");
129 if (blocked <= 9000 || blocked >= 10000) 124 if (blocked <= 9000 || blocked >= 10000)
130 blocked = formatNumber(blocked); 125 blocked = blocked.toLocaleString();
131 else 126 else
132 blocked = chrome.i18n.getMessage("stats_over", formatNumber(9000)); 127 blocked = i18n.getMessage("stats_over", (9000).toLocaleString());
133 128
134 var url = shareLinks[ev.target.dataset.social] 129 var url = createShareLink(ev.target.dataset.social, blocked);
135 .replace("_MESSAGE_", chrome.i18n.getMessage("stats_share_message", blocke d));
Wladimir Palant 2013/09/18 13:22:17 This is a rather ugly approach, and you forgot to
136
137 chrome.tabs.create({url: url}); 130 chrome.tabs.create({url: url});
138 }
139
140 var locale = require("utils").Utils.appLocale;
141 function formatNumber(number)
142 {
143 return number.toLocaleString(locale);
144 } 131 }
Wladimir Palant 2013/09/18 13:22:17 Utils.appLocale returns the browser's locale. So t
145 132
146 document.addEventListener("DOMContentLoaded", onLoad, false); 133 document.addEventListener("DOMContentLoaded", onLoad, false);
147 window.addEventListener("unload", onUnload, false); 134 window.addEventListener("unload", onUnload, false);
148 })(); 135 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld