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 var scripts = []; |
34 break; | 36 if (userAgent == "chrome") |
35 } | 37 { |
36 } | 38 var backgroundPage = chrome.extension.getBackgroundPage(); |
37 | 39 var require = backgroundPage.require; |
38 if (FilterStorage.subscriptions.some(function(s) s.url == Prefs.subscriptions_
exceptionsurl)) | 40 } |
39 E("acceptableAds").removeAttribute("hidden"); | 41 else if (userAgent == "firefox") |
40 } | 42 { |
41 | 43 scripts.push("utils.js"); |
42 function generateLinkText(element) | 44 } |
43 { | 45 scripts.push("i18n.js"); |
44 let template = element.getAttribute("_textTemplate"); | 46 |
45 | 47 function loadScripts() |
46 let [, beforeLink, linkText, afterLink] = /(.*)\[link\](.*)\[\/link\](.*)/.exe
c(template) || [null, "", template, ""]; | 48 { |
47 while (element.firstChild && element.firstChild.nodeType != Node.ELEMENT_NODE) | 49 var scriptName = scripts.shift(); |
48 element.removeChild(element.firstChild); | 50 var script = document.createElement("script"); |
49 while (element.lastChild && element.lastChild.nodeType != Node.ELEMENT_NODE) | 51 script.type = (userAgent == "firefox") ? "application/x-javascript;version=1
.7" : "text/javascript"; |
50 element.removeChild(element.lastChild); | 52 script.addEventListener("load", (scripts.length == 0) ? onScriptsLoaded : lo
adScripts, false); |
51 if (!element.firstChild) | 53 script.src = scriptName; |
52 return; | 54 document.head.appendChild(script); |
53 | 55 } |
54 element.firstChild.textContent = linkText; | 56 |
55 element.insertBefore(document.createTextNode(beforeLink), element.firstChild); | 57 function onScriptsLoaded() |
56 element.appendChild(document.createTextNode(afterLink)); | 58 { |
57 } | 59 if (userAgent == "chrome") |
58 | 60 { |
59 function openFilters() | 61 window.Utils = require("utils").Utils; |
60 { | 62 window.Prefs = require("prefs").Prefs; |
61 if (Utils.isFennec) | 63 } |
62 { | 64 |
63 let topWnd = window.QueryInterface(Ci.nsIInterfaceRequestor) | 65 // Set up page title |
64 .getInterface(Ci.nsIWebNavigation) | 66 var titleId = "firstRun_title_install"; |
65 .QueryInterface(Ci.nsIDocShellTreeItem) | 67 if (userAgent == "chrome" && backgroundPage.isFirstRun |
66 .rootTreeItem | 68 || userAgent == "firefox" && UI.firstRunDone) |
67 .QueryInterface(Ci.nsIInterfaceRequestor) | 69 titleId = "firstRun_title_update"; |
68 .getInterface(Ci.nsIDOMWindow); | 70 var pageTitle = i18n.getMessage(titleId); |
69 if (topWnd.wrappedJSObject) | 71 document.title = document.getElementById("title-main").textContent = pageTit
le; |
70 topWnd = topWnd.wrappedJSObject; | 72 |
71 | 73 shade = document.getElementById("shade"); |
72 // window.close() closes the entire window (bug 642604), make sure to close | 74 shade.addEventListener("mouseover", scrollPage, false); |
73 // only a single tab instead. | 75 shade.addEventListener("mouseout", stopScroll, false); |
74 if ("BrowserUI" in topWnd) | 76 |
75 { | 77 if (userAgent == "firefox") |
76 topWnd.BrowserUI.showPanel("addons-container"); | 78 { |
77 function showOptions() | 79 var typoSettings = document.createElement("script"); |
78 { | 80 typoSettings.type = "application/x-javascript;version=1.7"; |
79 if (!topWnd.ExtensionsView.getElementForAddon(Utils.addonID)) | 81 typoSettings.src = "typoSettings.js"; |
80 Utils.runAsync(showOptions); | 82 document.head.appendChild(typoSettings); |
81 else | 83 |
82 topWnd.ExtensionsView.showOptions(Utils.addonID); | 84 var toggleTypo = document.getElementById("toggle-typo"); |
83 } | 85 updateToggleButton("typo", Prefs.correctTypos); |
84 showOptions(); | 86 toggleTypo.addEventListener("click", function(event) |
85 } | 87 { |
86 } | 88 TypoActions.setEnabled(!Prefs.correctTypos); |
87 else | 89 updateToggleButton("typo", Prefs.correctTypos); |
88 UI.openFiltersDialog(); | 90 }, false); |
89 } | 91 } |
| 92 |
| 93 var toggleMalware = document.getElementById("toggle-malware"); |
| 94 updateToggleButton("malware", false); |
| 95 toggleMalware.addEventListener("click", function(event) |
| 96 { |
| 97 // TODO |
| 98 }, false); |
| 99 |
| 100 var toggleSocial = document.getElementById("toggle-social"); |
| 101 updateToggleButton("social", false); |
| 102 toggleSocial.addEventListener("click", function(event) |
| 103 { |
| 104 // TODO |
| 105 }, false); |
| 106 |
| 107 var toggleTracking = document.getElementById("toggle-tracking"); |
| 108 updateToggleButton("tracking", false); |
| 109 toggleTracking.addEventListener("click", function(event) |
| 110 { |
| 111 // TODO |
| 112 }, false); |
| 113 |
| 114 setLinks("acceptableAdsExplanation", getDocLink("acceptable_ads_criteria"),
openFilters); |
| 115 |
| 116 window.addEventListener("resize", onWindowResize, false); |
| 117 document.addEventListener("scroll", onScroll, false); |
| 118 |
| 119 onWindowResize(); |
| 120 |
| 121 initSocialLinks(null); |
| 122 } |
| 123 |
| 124 function onScroll() |
| 125 { |
| 126 var currentHeight = document.documentElement.scrollTop + document.body.scrol
lTop + document.documentElement.clientHeight; |
| 127 shade.style.opacity = (document.documentElement.scrollHeight == currentHeigh
t) ? "0.0" : "0.5"; |
| 128 } |
| 129 |
| 130 function onWindowResize() |
| 131 { |
| 132 onScroll(); |
| 133 } |
| 134 |
| 135 function scrollPage() |
| 136 { |
| 137 if (scrollTimer) |
| 138 stopScroll(); |
| 139 |
| 140 scrollTimer = setInterval(function() |
| 141 { |
| 142 window.scrollBy(0, 5); |
| 143 }, 20); |
| 144 } |
| 145 |
| 146 function stopScroll() |
| 147 { |
| 148 clearTimeout(scrollTimer); |
| 149 scrollTimer = null; |
| 150 } |
| 151 |
| 152 function openSharePopup(url) |
| 153 { |
| 154 var iframe = document.getElementById("share-popup"); |
| 155 var glassPane = document.getElementById("glass-pane"); |
| 156 var popupMessageReceived = false; |
| 157 |
| 158 var popupMessageListener = function(event) |
| 159 { |
| 160 var originFilter = Filter.fromText("||adblockplus.org^"); |
| 161 if (!originFilter.matches(event.origin, "OTHER", null, null)) |
| 162 return; |
| 163 |
| 164 var width = event.data.width; |
| 165 var height = event.data.height; |
| 166 iframe.width = width; |
| 167 iframe.height = height; |
| 168 iframe.style.marginTop = -height/2 + "px"; |
| 169 iframe.style.marginLeft = -width/2 + "px"; |
| 170 popupMessageReceived = true; |
| 171 window.removeEventListener("message", popupMessageListener); |
| 172 }; |
| 173 // Firefox requires last parameter to be true to be triggered by unprivilege
d pages |
| 174 window.addEventListener("message", popupMessageListener, false, true); |
| 175 |
| 176 var popupLoadListener = function() |
| 177 { |
| 178 if (popupMessageReceived) |
| 179 { |
| 180 iframe.className = "visible"; |
| 181 |
| 182 var popupCloseListener = function() |
| 183 { |
| 184 iframe.className = glassPane.className = ""; |
| 185 document.removeEventListener("click", popupCloseListener); |
| 186 }; |
| 187 document.addEventListener("click", popupCloseListener, false); |
| 188 } |
| 189 else |
| 190 { |
| 191 glassPane.className = ""; |
| 192 window.removeEventListener("message", popupMessageListener); |
| 193 } |
| 194 |
| 195 iframe.removeEventListener("load", popupLoadListener); |
| 196 }; |
| 197 iframe.addEventListener("load", popupLoadListener, false); |
| 198 |
| 199 iframe.src = url; |
| 200 glassPane.className = "visible"; |
| 201 } |
| 202 |
| 203 function initSocialLinks(variant) |
| 204 { |
| 205 var networks = ["twitter", "facebook", "gplus"]; |
| 206 networks.forEach(function(network) |
| 207 { |
| 208 var link = document.getElementById("share-" + network); |
| 209 link.addEventListener("click", function(e) |
| 210 { |
| 211 e.preventDefault(); |
| 212 openSharePopup(getDocLink("share-" + network) + "&variant=" + variant); |
| 213 }, false); |
| 214 }); |
| 215 } |
| 216 |
| 217 function setLinks(id) |
| 218 { |
| 219 var element = document.getElementById(id); |
| 220 if (!element) |
| 221 return; |
| 222 |
| 223 var links = element.getElementsByTagName("a"); |
| 224 for (var i = 0; i < links.length; i++) |
| 225 { |
| 226 if (typeof arguments[i + 1] == "string") |
| 227 { |
| 228 links[i].href = arguments[i + 1]; |
| 229 links[i].setAttribute("target", "_blank"); |
| 230 } |
| 231 else if (typeof arguments[i + 1] == "function") |
| 232 { |
| 233 links[i].href = "javascript:void(0);"; |
| 234 links[i].addEventListener("click", arguments[i + 1], false); |
| 235 } |
| 236 } |
| 237 } |
| 238 |
| 239 function getDocLink(page, anchor) |
| 240 { |
| 241 return Prefs.documentation_link |
| 242 .replace(/%LINK%/g, page) |
| 243 .replace(/%LANG%/g, Utils.appLocale) + (anchor ? "#" + anchor :
""); |
| 244 } |
| 245 |
| 246 function openFilters() |
| 247 { |
| 248 if (userAgent == "firefox") |
| 249 UI.openFiltersDialog(); |
| 250 else if (userAgent == "chrome") |
| 251 { |
| 252 backgroundPage.openOptions(); |
| 253 } |
| 254 } |
| 255 |
| 256 function updateToggleButton(feature, isEnabled) |
| 257 { |
| 258 var button = document.getElementById("toggle-" + feature); |
| 259 button.className = isEnabled ? "disable" : "enable"; |
| 260 button.textContent = i18n.getMessage(isEnabled ? "firstRun_action_disable" :
"firstRun_action_enable"); |
| 261 } |
| 262 |
| 263 document.addEventListener("DOMContentLoaded", loadScripts, false); |
| 264 })(); |
OLD | NEW |