OLD | NEW |
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 init() | 18 "use strict"; |
| 19 |
| 20 (function() |
19 { | 21 { |
20 generateLinkText(E("changeDescription")); | 22 var shade; |
21 | 23 var scrollTimer; |
22 for each (let subscription in FilterStorage.subscriptions) | 24 |
23 { | 25 // determine platform |
24 if (subscription instanceof DownloadableSubscription && subscription.url !=
Prefs.subscriptions_exceptionsurl && !subscription.disabled) | 26 var userAgent = ""; |
25 { | 27 if (navigator.userAgent.indexOf("Gecko/") > -1) |
26 E("listName").textContent = subscription.title; | 28 userAgent = "firefox"; |
27 | 29 else if (navigator.userAgent.indexOf("Chrome/") > -1) |
28 let link = E("listHomepage"); | 30 userAgent = "chrome"; |
29 link.setAttribute("href", subscription.homepage); | 31 |
30 link.setAttribute("title", subscription.homepage); | 32 if (userAgent !== "") |
31 | 33 document.documentElement.className = userAgent; |
32 E("listNameContainer").removeAttribute("hidden"); | 34 |
33 E("listNone").setAttribute("hidden", "true"); | 35 // load subscriptions for features |
34 break; | 36 var featureSubscriptions = {}; |
35 } | 37 (function() |
36 } | 38 { |
37 | 39 // load feature subscriptions |
38 if (FilterStorage.subscriptions.some(function(s) s.url == Prefs.subscriptions_
exceptionsurl)) | 40 var request = new XMLHttpRequest(); |
39 E("acceptableAds").removeAttribute("hidden"); | 41 request.open("GET", "featureSubscriptions.xml", false); // TODO: check in Ch
rome |
40 } | 42 request.send(); |
41 | 43 var subscriptions = request.responseXML.getElementsByTagName("subscription")
; |
42 function generateLinkText(element) | 44 for (var i = 0, len = subscriptions.length; i < len; i++) |
43 { | 45 { |
44 let template = element.getAttribute("_textTemplate"); | 46 var subscription = subscriptions[i]; |
45 | 47 featureSubscriptions[subscription.getAttribute("feature")] = { |
46 let [, beforeLink, linkText, afterLink] = /(.*)\[link\](.*)\[\/link\](.*)/.exe
c(template) || [null, "", template, ""]; | 48 "url": subscription.getAttribute("url"), |
47 while (element.firstChild && element.firstChild.nodeType != Node.ELEMENT_NODE) | 49 "title": subscription.getAttribute("title") |
48 element.removeChild(element.firstChild); | 50 }; |
49 while (element.lastChild && element.lastChild.nodeType != Node.ELEMENT_NODE) | 51 } |
50 element.removeChild(element.lastChild); | 52 })(); |
51 if (!element.firstChild) | 53 |
52 return; | 54 var scripts = []; |
53 | 55 if (userAgent == "chrome") |
54 element.firstChild.textContent = linkText; | 56 { |
55 element.insertBefore(document.createTextNode(beforeLink), element.firstChild); | 57 var backgroundPage = chrome.extension.getBackgroundPage(); |
56 element.appendChild(document.createTextNode(afterLink)); | 58 var require = backgroundPage.require; |
57 } | 59 } |
58 | 60 else if (userAgent == "firefox") |
59 function openFilters() | 61 { |
60 { | 62 scripts.push("utils.js"); |
61 if (Utils.isFennec) | 63 } |
62 { | 64 scripts.push("i18n.js"); |
63 let topWnd = window.QueryInterface(Ci.nsIInterfaceRequestor) | 65 |
64 .getInterface(Ci.nsIWebNavigation) | 66 function loadScripts() |
65 .QueryInterface(Ci.nsIDocShellTreeItem) | 67 { |
66 .rootTreeItem | 68 var scriptName = scripts.shift(); |
67 .QueryInterface(Ci.nsIInterfaceRequestor) | 69 var script = document.createElement("script"); |
68 .getInterface(Ci.nsIDOMWindow); | 70 script.type = (userAgent == "firefox") ? "application/x-javascript;version=1
.7" : "text/javascript"; |
69 if (topWnd.wrappedJSObject) | 71 script.addEventListener("load", (scripts.length == 0) ? onScriptsLoaded : lo
adScripts, false); |
70 topWnd = topWnd.wrappedJSObject; | 72 script.src = scriptName; |
71 | 73 document.head.appendChild(script); |
72 // window.close() closes the entire window (bug 642604), make sure to close | 74 } |
73 // only a single tab instead. | 75 |
74 if ("BrowserUI" in topWnd) | 76 function onScriptsLoaded() |
75 { | 77 { |
76 topWnd.BrowserUI.showPanel("addons-container"); | 78 if (userAgent == "chrome") |
77 function showOptions() | 79 { |
78 { | 80 window.Utils = require("utils").Utils; |
79 if (!topWnd.ExtensionsView.getElementForAddon(Utils.addonID)) | 81 window.Prefs = require("prefs").Prefs; |
80 Utils.runAsync(showOptions); | 82 } |
81 else | 83 |
82 topWnd.ExtensionsView.showOptions(Utils.addonID); | 84 // Set up page title |
83 } | 85 var titleId = "firstRun_title_install"; |
84 showOptions(); | 86 if (userAgent == "chrome" && backgroundPage.isFirstRun |
85 } | 87 || userAgent == "firefox" && UI.firstRunDone) |
86 } | 88 titleId = "firstRun_title_update"; |
87 else | 89 var pageTitle = i18n.getMessage(titleId); |
88 UI.openFiltersDialog(); | 90 document.title = document.getElementById("title-main").textContent = pageTit
le; |
89 } | 91 |
| 92 shade = document.getElementById("shade"); |
| 93 shade.addEventListener("mouseover", scrollPage, false); |
| 94 shade.addEventListener("mouseout", stopScroll, false); |
| 95 |
| 96 if (userAgent == "firefox") |
| 97 { |
| 98 var typoSettings = document.createElement("script"); |
| 99 typoSettings.type = "application/x-javascript;version=1.7"; |
| 100 typoSettings.src = "typoSettings.js"; |
| 101 document.head.appendChild(typoSettings); |
| 102 |
| 103 var toggleTypo = document.getElementById("toggle-typo"); |
| 104 updateToggleButton("typo", Prefs.correctTypos); |
| 105 Prefs.addListener(function(name) |
| 106 { |
| 107 if (name == "correctTypos") |
| 108 updateToggleButton("typo", Prefs.correctTypos); |
| 109 }); |
| 110 toggleTypo.addEventListener("click", function(event) |
| 111 { |
| 112 TypoActions.setEnabled(!Prefs.correctTypos); |
| 113 }, false); |
| 114 } |
| 115 |
| 116 setToggleSubscriptionButton("malware"); |
| 117 setToggleSubscriptionButton("social"); |
| 118 setToggleSubscriptionButton("tracking"); |
| 119 |
| 120 setLinks("acceptableAdsExplanation", getDocLink("acceptable_ads_criteria"),
openFilters); |
| 121 |
| 122 window.addEventListener("resize", onWindowResize, false); |
| 123 document.addEventListener("scroll", onScroll, false); |
| 124 |
| 125 onWindowResize(); |
| 126 |
| 127 initSocialLinks(null); |
| 128 } |
| 129 |
| 130 function onScroll() |
| 131 { |
| 132 var currentHeight = document.documentElement.scrollTop + document.body.scrol
lTop + document.documentElement.clientHeight; |
| 133 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh
t) ? "0.0" : "0.5"; |
| 134 } |
| 135 |
| 136 function onWindowResize() |
| 137 { |
| 138 onScroll(); |
| 139 } |
| 140 |
| 141 // TODO: check in Chrome |
| 142 function getSubscription(featureSubscription) |
| 143 { |
| 144 var subscriptions = FilterStorage.subscriptions; |
| 145 for (var i = 0, len = subscriptions.length; i < len; i++) |
| 146 { |
| 147 var subscription = subscriptions[i]; |
| 148 if (subscription.url == featureSubscription.url && subscription instanceof
DownloadableSubscription) |
| 149 return subscription; |
| 150 } |
| 151 |
| 152 return null; |
| 153 } |
| 154 |
| 155 function isSubscriptionEnabled(featureSubscription) |
| 156 { |
| 157 var subscription = getSubscription(featureSubscription); |
| 158 return subscription != null && !subscription.disabled; |
| 159 } |
| 160 |
| 161 // TODO: check in Chrome |
| 162 function setToggleSubscriptionButton(feature) |
| 163 { |
| 164 var featureSubscription = featureSubscriptions[feature]; |
| 165 |
| 166 var element = document.getElementById("toggle-" + feature); |
| 167 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription)); |
| 168 FilterNotifier.addListener(function(action) |
| 169 { |
| 170 if (/^(filter|subscription)\.(added|removed|disabled)$/.test(action)) |
| 171 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription)); |
| 172 }); |
| 173 element.addEventListener("click", function(event) |
| 174 { |
| 175 var subscription = getSubscription(featureSubscription); |
| 176 if (subscription == null) |
| 177 { |
| 178 subscription = Subscription.fromURL(featureSubscription.url); |
| 179 subscription.disabled = false; |
| 180 subscription.title = featureSubscription.title; |
| 181 subscription.homepage = featureSubscription.homepage; |
| 182 FilterStorage.addSubscription(subscription); |
| 183 if (subscription instanceof DownloadableSubscription && !subscription.la
stDownload) |
| 184 Synchronizer.execute(subscription); |
| 185 } |
| 186 else if (!subscription.disabled) |
| 187 FilterStorage.removeSubscription(subscription); |
| 188 else |
| 189 subscription.disabled = false; |
| 190 }, false); |
| 191 } |
| 192 |
| 193 function scrollPage() |
| 194 { |
| 195 if (scrollTimer) |
| 196 stopScroll(); |
| 197 |
| 198 scrollTimer = setInterval(function() |
| 199 { |
| 200 window.scrollBy(0, 5); |
| 201 }, 20); |
| 202 } |
| 203 |
| 204 function stopScroll() |
| 205 { |
| 206 clearTimeout(scrollTimer); |
| 207 scrollTimer = null; |
| 208 } |
| 209 |
| 210 function openSharePopup(url) |
| 211 { |
| 212 var iframe = document.getElementById("share-popup"); |
| 213 var glassPane = document.getElementById("glass-pane"); |
| 214 var popupMessageReceived = false; |
| 215 |
| 216 var popupMessageListener = function(event) |
| 217 { |
| 218 var originFilter = Filter.fromText("||adblockplus.org^"); |
| 219 if (!originFilter.matches(event.origin, "OTHER", null, null)) |
| 220 return; |
| 221 |
| 222 var width = event.data.width; |
| 223 var height = event.data.height; |
| 224 iframe.width = width; |
| 225 iframe.height = height; |
| 226 iframe.style.marginTop = -height/2 + "px"; |
| 227 iframe.style.marginLeft = -width/2 + "px"; |
| 228 popupMessageReceived = true; |
| 229 window.removeEventListener("message", popupMessageListener); |
| 230 }; |
| 231 // Firefox requires last parameter to be true to be triggered by unprivilege
d pages |
| 232 window.addEventListener("message", popupMessageListener, false, true); |
| 233 |
| 234 var popupLoadListener = function() |
| 235 { |
| 236 if (popupMessageReceived) |
| 237 { |
| 238 iframe.className = "visible"; |
| 239 |
| 240 var popupCloseListener = function() |
| 241 { |
| 242 iframe.className = glassPane.className = ""; |
| 243 document.removeEventListener("click", popupCloseListener); |
| 244 }; |
| 245 document.addEventListener("click", popupCloseListener, false); |
| 246 } |
| 247 else |
| 248 { |
| 249 glassPane.className = ""; |
| 250 window.removeEventListener("message", popupMessageListener); |
| 251 } |
| 252 |
| 253 iframe.removeEventListener("load", popupLoadListener); |
| 254 }; |
| 255 iframe.addEventListener("load", popupLoadListener, false); |
| 256 |
| 257 iframe.src = url; |
| 258 glassPane.className = "visible"; |
| 259 } |
| 260 |
| 261 function initSocialLinks(variant) |
| 262 { |
| 263 var networks = ["twitter", "facebook", "gplus"]; |
| 264 networks.forEach(function(network) |
| 265 { |
| 266 var link = document.getElementById("share-" + network); |
| 267 link.addEventListener("click", function(e) |
| 268 { |
| 269 e.preventDefault(); |
| 270 openSharePopup(getDocLink("share-" + network) + "&variant=" + variant); |
| 271 }, false); |
| 272 }); |
| 273 } |
| 274 |
| 275 function setLinks(id) |
| 276 { |
| 277 var element = document.getElementById(id); |
| 278 if (!element) |
| 279 return; |
| 280 |
| 281 var links = element.getElementsByTagName("a"); |
| 282 for (var i = 0; i < links.length; i++) |
| 283 { |
| 284 if (typeof arguments[i + 1] == "string") |
| 285 { |
| 286 links[i].href = arguments[i + 1]; |
| 287 links[i].setAttribute("target", "_blank"); |
| 288 } |
| 289 else if (typeof arguments[i + 1] == "function") |
| 290 { |
| 291 links[i].href = "javascript:void(0);"; |
| 292 links[i].addEventListener("click", arguments[i + 1], false); |
| 293 } |
| 294 } |
| 295 } |
| 296 |
| 297 function getDocLink(page, anchor) |
| 298 { |
| 299 return Prefs.documentation_link |
| 300 .replace(/%LINK%/g, page) |
| 301 .replace(/%LANG%/g, Utils.appLocale) + (anchor ? "#" + anchor :
""); |
| 302 } |
| 303 |
| 304 function openFilters() |
| 305 { |
| 306 if (userAgent == "firefox") |
| 307 UI.openFiltersDialog(); |
| 308 else if (userAgent == "chrome") |
| 309 { |
| 310 backgroundPage.openOptions(); |
| 311 } |
| 312 } |
| 313 |
| 314 function updateToggleButton(feature, isEnabled) |
| 315 { |
| 316 var button = document.getElementById("toggle-" + feature); |
| 317 button.className = isEnabled ? "disable" : "enable"; |
| 318 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" :
"firstRun_action_enable"); |
| 319 } |
| 320 |
| 321 document.addEventListener("DOMContentLoaded", loadScripts, false); |
| 322 })(); |
OLD | NEW |