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

Side by Side Diff: html/static/js/firstRun.js

Issue 10989023: First-run page on Adblock Plus for IE (Closed)
Patch Set: Created June 25, 2013, 10:26 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
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 /* Mock Start */
19 var Messages = {
20 "firstRun_feature_betterSurfing": "Enjoy the web without annoying ads",
21 "firstRun_feature_videoAds": "Watch videos without commercial interruptions",
22 "firstRun_features_heading": "With Adblock Plus you can:",
23 "firstRun_share1": "Do you think Adblock Plus is worth sharing? Please do!",
24 "firstRun_share2": "Adblock Plus is free and open source. Please support us:",
25 "firstRun_share2_donate": "Donate",
26 "firstRun_share2_or": "or",
27 "firstRun_title_install": "Adblock Plus has been installed"
28 };
29
30 var AdblockPlus = {
31 require: function()
32 {
33 return {
34 Filter: {
35 fromText: function()
36 {
37 return {
38 matches: function()
39 {
40 return true;
41 }
42 };
43 }
44 },
45 Prefs: {
46 documentation_link: "https://adblockplus.org/redirect?link=%LINK%&lang=% LANG%"
47 },
48 Utils: {
49 appLocale: "en"
50 }
51 };
52 },
53 getMessage: function(key)
54 {
55 return Messages[key] || "[" + key + "]";
56 }
57 };
58 /* Mock End */
59
60 var require = AdblockPlus.require;
61 var Prefs = require("prefs").Prefs;
62 var Utils = require("utils").Utils;
63 var Filter = require("filterClasses").Filter;
64
65 function addListener(object, type, listener, useCapture)
Felix Dahlke 2013/07/11 11:11:14 Since you're always passing useCapture=false, I gu
Thomas Greiner 2013/07/12 10:52:52 Done.
66 {
67 if ("addEventListener" in object)
68 object.addEventListener(type, listener, useCapture);
69 else
70 {
71 object.attachEvent("on" + type, function()
72 {
73 listener(event);
74 });
75 }
76 }
77
78 function removeListener(object, type, listener)
79 {
80 if ("removeEventListener" in object)
81 object.removeEventListener(type, listener);
82 else
83 object.detachEvent(type, listener);
84 }
85
86 function openSharePopup(url)
87 {
88 var iframe = document.getElementById("share-popup");
89 var glassPane = document.getElementById("glass-pane");
90 var popupMessageReceived = false;
91
92 var popupMessageListener = function(event)
93 {
94 var originFilter = Filter.fromText("||adblockplus.org^");
95 if (!originFilter.matches(event.origin, "OTHER", null, null))
96 return;
97
98 var data = JSON.parse(event.data);
99 iframe.width = data.width;
100 iframe.height = data.height;
101 popupMessageReceived = true;
102 // all IE versions require the use of detachEvent for message event to work
103 window.detachEvent("onmessage", popupMessageListener);
104 };
105 // all IE versions require the use of attachEvent for message event to work
106 window.attachEvent("onmessage", popupMessageListener);
Felix Dahlke 2013/07/11 11:11:14 So you're not using addEventListener here because
Thomas Greiner 2013/07/12 10:52:52 Done.
107
108 var timeout = 125;
Felix Dahlke 2013/07/11 11:11:14 I think the timeout calculating here is a bit more
Thomas Greiner 2013/07/12 10:52:52 Done.
109 var popupLoadListener = function()
110 {
111 if (popupMessageReceived)
112 {
113 iframe.className = "visible";
114
115 var popupCloseListener = function()
116 {
117 iframe.className = glassPane.className = "";
118 removeListener(document, "click", popupCloseListener);
119 };
120 addListener(document, "click", popupCloseListener, false);
121 }
122 else
123 {
124 if (timeout > 2000)
125 {
126 glassPane.className = "";
127 removeListener(window, "message", popupMessageListener);
128 }
129 else
130 {
131 timeout *= 2;
132 setTimeout(popupLoadListener, timeout);
133 }
134 }
135
136 removeListener(iframe, "load", popupLoadListener);
137 };
138 addListener(iframe, "load", popupLoadListener, false);
139
140 iframe.src = url;
141 glassPane.className = "visible";
142 }
143
144 function initSocialLinks(variant)
145 {
146 // Share popup doesn't work in <IE9 so don't show it
147 if (/MSIE [6-8]/.test(navigator.appVersion))
148 return;
149
150 var networks = ["twitter", "facebook", "gplus"];
151 networks.forEach(function(network)
152 {
153 var link = document.getElementById("share-" + network + "-" + variant);
154 addListener(link, "click", function(e)
155 {
156 e.preventDefault();
157 openSharePopup(getDocLink("share-" + network) + "&variant=" + variant);
158 }, false);
159 });
160 }
161
162 function getDocLink(page)
163 {
164 return Prefs.documentation_link
165 .replace(/%LINK%/g, page)
166 .replace(/%LANG%/g, Utils.appLocale);
167 }
168
169 function initTranslations()
170 {
171 // Map message ID to HTML element ID
172 var mapping = {
173 "title-main": "firstRun_title_install",
174 "i18n-features-heading": "firstRun_features_heading",
175 "i18n-feature-betterSurfing": "firstRun_feature_betterSurfing",
176 "i18n-feature-videoAds": "firstRun_feature_videoAds",
177 "share-text1": "firstRun_share1",
178 "share-text2": "firstRun_share2",
179 "share-donate": "firstRun_share2_donate",
180 "share2-connection": "firstRun_share2_or"
181 };
182
183 document.title = AdblockPlus.getMessage("firstRun_title_install");
184 for (var i in mapping)
185 {
186 var element = document.getElementById(i);
187 element.innerText = AdblockPlus.getMessage(mapping[i]);
188 }
189 }
190
191 function init()
192 {
193 // Choose a share text variant randomly
194 var variant = Math.floor(Math.random() * 2) + 1;
195 var classList = document.documentElement.className.split(" ");
196 classList.push("share-variant-" + variant);
197 document.documentElement.className = classList.join(" ");
198
199 initTranslations();
200 initSocialLinks(variant);
201
202 var donateLink = document.getElementById("share-donate");
203 donateLink.href = getDocLink("donate") + "&variant=" + variant;
204 }
205
206 addListener(window, "load", init, false);
OLDNEW

Powered by Google App Engine
This is Rietveld