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

Side by Side Diff: stats.js

Issue 11627039: Added ad counting functionality (Closed)
Patch Set: Created Sept. 18, 2013, 10:07 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « popup.html ('k') | webrequest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 (function()
19 {
20 var backgroundPage = chrome.extension.getBackgroundPage();
21 var require = backgroundPage.require;
22 var ChromeCompat = backgroundPage.ChromeCompat;
23 var stats = require("stats");
24 var StatsObject = stats.StatsObject;
25 var Stats = stats.Stats;
26 var FilterNotifier = require("filterNotifier").FilterNotifier;
27
28 var currentTabId;
29 var statsPage;
30 var statsTotal;
31 var shareBox;
32 var shareURL = "https://adblockplus.org";
33 var redirectURL = "https://www.facebook.com";
34
35 function createShareLink(url, params)
36 {
37 var querystring = [];
38 for (var key in params)
39 querystring.push(key + "=" + encodeURIComponent(params[key]));
40 return url + "?" + querystring.join("&");
41 }
42
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()
68 {
69 statsPage = document.querySelector("#statsPage > strong");
70 statsTotal = document.querySelector("#statsTotal > strong");
71
72 shareBox = document.getElementById("shareBox");
73 var shareBoxLink = document.getElementById("share");
74 shareBoxLink.addEventListener("click", showShareBox, false);
75
76 var shareActions = document.querySelectorAll("#shareBox > a");
77 for (var i = 0; i < shareActions.length; i++)
78 shareActions[i].addEventListener("click", share, false);
79
80 // update stats
81 statsTotal.innerText = formatNumber(Stats.total.blocked);
82 chrome.tabs.query({active: true}, function(tabs)
83 {
84 if (tabs.length > 0)
85 {
86 currentTabId = tabs[0].id;
87 updateStats();
88
89 FilterNotifier.addListener(onNotify);
90
91 document.getElementById("statsStuff").removeAttribute("hidden");
92 }
93 });
94 }
95
96 function onUnload()
97 {
98 FilterNotifier.removeListener(onNotify);
99 }
100
101 function onNotify(action, item)
102 {
103 if (action == "document.stats")
104 updateStats();
105 }
106
107 function updateStats()
108 {
109 ChromeCompat.tabs.sendMessage(currentTabId, {reqtype: "get-stats"}, function (response)
110 {
111 var page = new StatsObject(response);
112 statsPage.innerText = formatNumber(page.blocked);
113 statsTotal.innerText = formatNumber(Stats.total.blocked);
114 });
115 }
116
117 function showShareBox(ev)
118 {
119 if (shareBox.hasAttribute("hidden"))
120 shareBox.removeAttribute("hidden");
121 else
122 shareBox.setAttribute("hidden", true);
123 }
124
125 function share(ev)
126 {
127 // Easter Egg
128 var blocked = Stats.total.blocked;
129 if (blocked <= 9000 || blocked >= 10000)
130 blocked = formatNumber(blocked);
131 else
132 blocked = chrome.i18n.getMessage("stats_over", formatNumber(9000));
133
134 var url = shareLinks[ev.target.dataset.social]
135 .replace("_MESSAGE_", chrome.i18n.getMessage("stats_share_message", blocke d));
136
137 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 }
145
146 document.addEventListener("DOMContentLoaded", onLoad, false);
147 window.addEventListener("unload", onUnload, false);
148 })();
OLDNEW
« no previous file with comments | « popup.html ('k') | webrequest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld