Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 "use strict"; | |
19 | |
18 (function() | 20 (function() |
19 { | 21 { |
20 var shade; | 22 var shade; |
21 var scrollTimer; | 23 var scrollTimer; |
22 | 24 |
23 function onDomReady() | 25 // Load subscriptions for features |
24 { | 26 var featureSubscriptions = [ |
25 shade = document.getElementById("shade"); | 27 { |
26 | 28 feature: "malware", |
29 homepage: "http://malwaredomains.com/", | |
30 title: "Malware Domains", | |
31 url: "https://easylist-downloads.adblockplus.org/malwaredomains_full.txt" | |
32 }, | |
33 { | |
34 feature: "social", | |
35 homepage: "https://www.fanboy.co.nz/", | |
36 title: "Fanboy's Social Blocking List", | |
37 url: "https://easylist-downloads.adblockplus.org/fanboy-social.txt" | |
38 }, | |
39 { | |
40 feature: "tracking", | |
41 homepage: "https://easylist.adblockplus.org/", | |
42 title: "EasyPrivacy", | |
43 url: "https://easylist-downloads.adblockplus.org/easyprivacy.txt" | |
44 } | |
45 ]; | |
46 | |
47 function onDOMLoaded() | |
48 { | |
49 // Show warning if data corruption was detected | |
50 if (typeof backgroundPage != "undefined" && backgroundPage.seenDataCorruptio n) | |
51 { | |
52 E("dataCorruptionWarning").removeAttribute("hidden"); | |
53 setLinks("dataCorruptionWarning", Utils.getDocLink("knownIssuesChrome_filt erstorage")); | |
54 } | |
55 | |
56 // Set up URL | |
57 setLinks("acceptableAdsExplanation", Utils.getDocLink("acceptable_ads_criter ia"), openFilters); | |
58 | |
59 shade = E("shade"); | |
27 shade.addEventListener("mouseover", scrollPage, false); | 60 shade.addEventListener("mouseover", scrollPage, false); |
28 shade.addEventListener("mouseout", stopScroll, false); | 61 shade.addEventListener("mouseout", stopScroll, false); |
29 | 62 |
30 window.addEventListener('resize', onWindowResize, false); | 63 // Set up typo feature |
31 document.addEventListener('scroll', onScroll, false); | 64 if (require("typoBootstrap")) |
Wladimir Palant
2013/05/15 12:12:02
Nit: double quotation marks here and above please.
Thomas Greiner
2013/05/23 09:17:21
Done.
| |
65 { | |
66 var featureTypo = E("feature-typo"); | |
67 featureTypo.removeAttribute("hidden"); | |
68 | |
69 updateToggleButton("typo", Prefs.correctTypos); | |
70 | |
71 var listener = function(name) | |
72 { | |
73 if (name == "correctTypos") | |
74 updateToggleButton("typo", Prefs.correctTypos); | |
75 } | |
76 Prefs.addListener(listener); | |
77 window.addEventListener("unload", function(event) | |
78 { | |
79 Prefs.removeListener(listener); | |
80 }, false); | |
81 | |
82 E("toggle-typo").addEventListener("click", toggleTypoCorrectionEnabled, fa lse); | |
83 } | |
84 | |
85 // Set up feature buttons linked to subscriptions | |
86 featureSubscriptions.forEach(setToggleSubscriptionButton); | |
87 var filterListener = function(action) | |
88 { | |
89 if (/^subscription\.(added|removed|disabled)$/.test(action)) | |
90 { | |
91 for (var i = 0; i < featureSubscriptions.length; i++) | |
92 { | |
93 var featureSubscription = featureSubscriptions[i]; | |
94 updateToggleButton(featureSubscription.feature, isSubscriptionEnabled( featureSubscription)); | |
95 } | |
96 } | |
97 } | |
98 FilterNotifier.addListener(filterListener); | |
99 window.addEventListener("unload", function(event) | |
100 { | |
101 FilterNotifier.removeListener(filterListener); | |
102 }, false); | |
103 | |
104 window.addEventListener("resize", onWindowResize, false); | |
105 document.addEventListener("scroll", onScroll, false); | |
32 | 106 |
33 onWindowResize(); | 107 onWindowResize(); |
108 | |
109 initSocialLinks(null); | |
34 } | 110 } |
35 | 111 |
36 function onScroll() | 112 function onScroll() |
37 { | 113 { |
38 var currentHeight = document.documentElement.scrollTop + document.body.scrol lTop + document.documentElement.clientHeight; | 114 var currentHeight = document.documentElement.scrollTop + document.body.scrol lTop + document.documentElement.clientHeight; |
39 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh t) ? "0.0" : "0.5"; | 115 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh t) ? "0.0" : "0.5"; |
40 } | 116 } |
41 | 117 |
42 function onWindowResize() | 118 function onWindowResize() |
43 { | 119 { |
44 onScroll(); | 120 onScroll(); |
45 } | 121 } |
46 | 122 |
123 function toggleTypoCorrectionEnabled() | |
124 { | |
125 Prefs.correctTypos = !Prefs.correctTypos; | |
126 } | |
127 | |
128 function isSubscriptionEnabled(featureSubscription) | |
129 { | |
130 return featureSubscription.url in FilterStorage.knownSubscriptions | |
131 && !Subscription.fromURL(featureSubscription.url).disabled; | |
132 } | |
133 | |
134 function setToggleSubscriptionButton(featureSubscription) | |
135 { | |
136 var feature = featureSubscription.feature; | |
137 | |
138 var element = E("toggle-" + feature); | |
139 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription)); | |
140 element.addEventListener("click", function(event) | |
141 { | |
142 var subscription = Subscription.fromURL(featureSubscription.url); | |
143 if (isSubscriptionEnabled(featureSubscription)) | |
144 FilterStorage.removeSubscription(subscription); | |
145 else | |
146 { | |
147 subscription.disabled = false; | |
148 subscription.title = featureSubscription.title; | |
149 subscription.homepage = featureSubscription.homepage; | |
150 FilterStorage.addSubscription(subscription); | |
151 if (!subscription.lastDownload) | |
152 Synchronizer.execute(subscription); | |
153 } | |
154 }, false); | |
155 } | |
156 | |
47 function scrollPage() | 157 function scrollPage() |
48 { | 158 { |
49 if (scrollTimer) | 159 if (scrollTimer) |
50 stopScroll(); | 160 stopScroll(); |
51 | 161 |
52 scrollTimer = setInterval(function() | 162 scrollTimer = setInterval(function() |
53 { | 163 { |
54 window.scrollBy(0, 5); | 164 window.scrollBy(0, 5); |
55 }, 20); | 165 }, 20); |
56 } | 166 } |
57 | 167 |
58 function stopScroll() | 168 function stopScroll() |
59 { | 169 { |
60 clearTimeout(scrollTimer); | 170 clearTimeout(scrollTimer); |
61 scrollTimer = null; | 171 scrollTimer = null; |
62 } | 172 } |
63 | 173 |
64 document.addEventListener("DOMContentLoaded", onDomReady, false); | 174 function openSharePopup(url) |
175 { | |
176 var iframe = E("share-popup"); | |
177 var glassPane = E("glass-pane"); | |
178 var popupMessageReceived = false; | |
179 | |
180 var popupMessageListener = function(event) | |
181 { | |
182 var originFilter = Filter.fromText("||adblockplus.org^"); | |
183 if (!originFilter.matches(event.origin, "OTHER", null, null)) | |
184 return; | |
185 | |
186 var width = event.data.width; | |
187 var height = event.data.height; | |
188 iframe.width = width; | |
189 iframe.height = height; | |
190 iframe.style.marginTop = -height/2 + "px"; | |
191 iframe.style.marginLeft = -width/2 + "px"; | |
192 popupMessageReceived = true; | |
193 window.removeEventListener("message", popupMessageListener); | |
194 }; | |
195 // Firefox requires last parameter to be true to be triggered by unprivilege d pages | |
196 window.addEventListener("message", popupMessageListener, false, true); | |
197 | |
198 var popupLoadListener = function() | |
199 { | |
200 if (popupMessageReceived) | |
201 { | |
202 iframe.className = "visible"; | |
203 | |
204 var popupCloseListener = function() | |
205 { | |
206 iframe.className = glassPane.className = ""; | |
207 document.removeEventListener("click", popupCloseListener); | |
208 }; | |
209 document.addEventListener("click", popupCloseListener, false); | |
210 } | |
211 else | |
212 { | |
213 glassPane.className = ""; | |
214 window.removeEventListener("message", popupMessageListener); | |
215 } | |
216 | |
217 iframe.removeEventListener("load", popupLoadListener); | |
218 }; | |
219 iframe.addEventListener("load", popupLoadListener, false); | |
220 | |
221 iframe.src = url; | |
222 glassPane.className = "visible"; | |
223 } | |
224 | |
225 function initSocialLinks(variant) | |
226 { | |
227 var networks = ["twitter", "facebook", "gplus"]; | |
228 networks.forEach(function(network) | |
229 { | |
230 var link = E("share-" + network); | |
231 link.addEventListener("click", function(e) | |
232 { | |
233 e.preventDefault(); | |
234 openSharePopup(Utils.getDocLink("share-" + network) + "&variant=" + vari ant); | |
235 }, false); | |
236 }); | |
237 } | |
238 | |
239 function setLinks(id) | |
240 { | |
241 var element = E(id); | |
242 if (!element) | |
243 return; | |
244 | |
245 var links = element.getElementsByTagName("a"); | |
246 for (var i = 0; i < links.length; i++) | |
247 { | |
248 if (typeof arguments[i + 1] == "string") | |
249 { | |
250 links[i].href = arguments[i + 1]; | |
251 links[i].setAttribute("target", "_blank"); | |
252 } | |
253 else if (typeof arguments[i + 1] == "function") | |
254 { | |
255 links[i].href = "javascript:void(0);"; | |
256 links[i].addEventListener("click", arguments[i + 1], false); | |
257 } | |
258 } | |
259 } | |
260 | |
261 function openFilters() | |
262 { | |
263 if (typeof UI != "undefined") | |
264 UI.openFiltersDialog(); | |
265 else | |
266 { | |
267 backgroundPage.openOptions(); | |
268 } | |
269 } | |
270 | |
271 function updateToggleButton(feature, isEnabled) | |
272 { | |
273 var button = E("toggle-" + feature); | |
274 button.className = isEnabled ? "disable" : "enable"; | |
275 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" : "firstRun_action_enable"); | |
276 } | |
277 | |
278 document.addEventListener("DOMContentLoaded", onDOMLoaded, false); | |
65 })(); | 279 })(); |
66 | |
67 function init() | |
68 { | |
69 E("currentVersion").textContent = Prefs.currentVersion; | |
70 | |
71 generateLinkText(E("changeDescription")); | |
72 | |
73 for each (let subscription in FilterStorage.subscriptions) | |
74 { | |
75 if (subscription instanceof DownloadableSubscription && subscription.url != Prefs.subscriptions_exceptionsurl && !subscription.disabled) | |
76 { | |
77 E("listName").textContent = subscription.title; | |
78 | |
79 let link = E("listHomepage"); | |
80 link.setAttribute("href", subscription.homepage); | |
81 link.setAttribute("title", subscription.homepage); | |
82 | |
83 E("listNameContainer").removeAttribute("hidden"); | |
84 E("listNone").setAttribute("hidden", "true"); | |
85 break; | |
86 } | |
87 } | |
88 | |
89 if (FilterStorage.subscriptions.some(function(s) s.url == Prefs.subscriptions_ exceptionsurl)) | |
90 E("acceptableAds").removeAttribute("hidden"); | |
91 } | |
92 | |
93 function generateLinkText(element) | |
94 { | |
95 let template = element.getAttribute("_textTemplate"); | |
96 | |
97 let [, beforeLink, linkText, afterLink] = /(.*)\[link\](.*)\[\/link\](.*)/.exe c(template) || [null, "", template, ""]; | |
98 while (element.firstChild && element.firstChild.nodeType != Node.ELEMENT_NODE) | |
99 element.removeChild(element.firstChild); | |
100 while (element.lastChild && element.lastChild.nodeType != Node.ELEMENT_NODE) | |
101 element.removeChild(element.lastChild); | |
102 if (!element.firstChild) | |
103 return; | |
104 | |
105 element.firstChild.textContent = linkText; | |
106 element.insertBefore(document.createTextNode(beforeLink), element.firstChild); | |
107 element.appendChild(document.createTextNode(afterLink)); | |
108 } | |
109 | |
110 function openFilters() | |
111 { | |
112 if (Utils.isFennec) | |
113 { | |
114 let topWnd = window.QueryInterface(Ci.nsIInterfaceRequestor) | |
115 .getInterface(Ci.nsIWebNavigation) | |
116 .QueryInterface(Ci.nsIDocShellTreeItem) | |
117 .rootTreeItem | |
118 .QueryInterface(Ci.nsIInterfaceRequestor) | |
119 .getInterface(Ci.nsIDOMWindow); | |
120 if (topWnd.wrappedJSObject) | |
121 topWnd = topWnd.wrappedJSObject; | |
122 | |
123 // window.close() closes the entire window (bug 642604), make sure to close | |
124 // only a single tab instead. | |
125 if ("BrowserUI" in topWnd) | |
126 { | |
127 topWnd.BrowserUI.showPanel("addons-container"); | |
128 function showOptions() | |
129 { | |
130 if (!topWnd.ExtensionsView.getElementForAddon(Utils.addonID)) | |
131 Utils.runAsync(showOptions); | |
132 else | |
133 topWnd.ExtensionsView.showOptions(Utils.addonID); | |
134 } | |
135 showOptions(); | |
136 } | |
137 } | |
138 else | |
139 UI.openFiltersDialog(); | |
140 } | |
141 | |
LEFT | RIGHT |