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

Side by Side Diff: firstRun.js

Issue 6180766664884224: Issue 1663 - Made first-run page use an asynchronous messaging protocol (Closed)
Patch Set: Documentation fixes Created Dec. 17, 2014, 5:46 p.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 | « ext/content.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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
(...skipping 23 matching lines...) Expand all
34 url: "https://easylist-downloads.adblockplus.org/fanboy-social.txt" 34 url: "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
35 }, 35 },
36 { 36 {
37 feature: "tracking", 37 feature: "tracking",
38 homepage: "https://easylist.adblockplus.org/", 38 homepage: "https://easylist.adblockplus.org/",
39 title: "EasyPrivacy", 39 title: "EasyPrivacy",
40 url: "https://easylist-downloads.adblockplus.org/easyprivacy.txt" 40 url: "https://easylist-downloads.adblockplus.org/easyprivacy.txt"
41 } 41 }
42 ]; 42 ];
43 43
44 function getDocLink(link, callback)
45 {
46 ext.backgroundPage.sendMessage({type: "app.doclink", args: [link]}, callback );
47 }
48
44 function onDOMLoaded() 49 function onDOMLoaded()
45 { 50 {
46 // Set up URLs 51 // Set up URLs
47 var donateLink = E("donate"); 52 getDocLink("donate", function(link)
48 donateLink.href = Utils.getDocLink("donate"); 53 {
54 E("donate").href = link;
55 });
49 56
50 var contributors = E("contributors"); 57 getDocLink("contributors", function(link)
51 contributors.href = Utils.getDocLink("contributors"); 58 {
59 E("contributors").href = link;
60 });
52 61
53 setLinks("acceptableAdsExplanation", Utils.getDocLink("acceptable_ads_criter ia"), openFilters); 62 getDocLink("acceptable_ads_criteria", function(link)
54 setLinks("share-headline", Utils.getDocLink("contribute")); 63 {
64 setLinks("acceptableAdsExplanation", link, openFilters);
65 });
55 66
56 if (typeof backgroundPage != "undefined") 67 getDocLink("contribute", function(link)
68 {
69 setLinks("share-headline", link);
70 });
71
72 ext.backgroundPage.sendMessage({type: "app.issues"}, function(issues)
57 { 73 {
58 // Show warning if data corruption was detected 74 // Show warning if data corruption was detected
59 if (backgroundPage.seenDataCorruption) 75 if (issues.seenDataCorruption)
60 { 76 {
61 E("dataCorruptionWarning").removeAttribute("hidden"); 77 E("dataCorruptionWarning").removeAttribute("hidden");
62 setLinks("dataCorruptionWarning", Utils.getDocLink("knownIssuesChrome_fi lterstorage")); 78 getDocLink("knownIssuesChrome_filterstorage", function(link)
79 {
80 setLinks("dataCorruptionWarning", link);
81 });
63 } 82 }
64 83
65 // Show warning if filterlists settings were reinitialized 84 // Show warning if filterlists settings were reinitialized
66 if (backgroundPage.filterlistsReinitialized) 85 if (issues.filterlistsReinitialized)
67 { 86 {
68 E("filterlistsReinitializedWarning").removeAttribute("hidden"); 87 E("filterlistsReinitializedWarning").removeAttribute("hidden");
69 setLinks("filterlistsReinitializedWarning", openFilters); 88 setLinks("filterlistsReinitializedWarning", openFilters);
70 } 89 }
71 } 90 });
72 91
73 // Show warning if Safari version isn't supported 92 // Show warning if Safari version isn't supported
74 var info = require("info"); 93 ext.backgroundPage.sendMessage({type: "app.info"}, function(info)
75 if (info.platform == "safari" && ( 94 {
76 Services.vc.compare(info.platformVersion, "6.0") < 0 || // beforeload br eaks websites in Safari 5 95 if (info.platform == "safari" && (
77 Services.vc.compare(info.platformVersion, "6.1") == 0 || // extensions ar e broken in 6.1 and 7.0 96 parseInt(info.platformVersion, 10) < 6 || // beforeload breaks websites in Safari 5
78 Services.vc.compare(info.platformVersion, "7.0") == 0 97 info.platformVersion == "6.1" || // extensions are broken in 6 .1 and 7.0
79 )) 98 info.platformVersion == "7.0"
80 E("legacySafariWarning").removeAttribute("hidden"); 99 ))
100 {
101 E("legacySafariWarning").removeAttribute("hidden");
102 }
103 });
81 104
82 // Set up feature buttons linked to subscriptions 105 // Set up feature buttons linked to subscriptions
83 featureSubscriptions.forEach(setToggleSubscriptionButton); 106 featureSubscriptions.forEach(initToggleSubscriptionButton);
84 var filterListener = function(action) 107 updateToggleButtons();
108 initSocialLinks();
109
110 ext.onMessage.addListener(function(message)
85 { 111 {
86 if (/^subscription\.(added|removed|disabled)$/.test(action)) 112 if (message.type == "subscriptions.listen")
87 { 113 {
88 for (var i = 0; i < featureSubscriptions.length; i++) 114 updateToggleButtons();
89 { 115 initSocialLinks();
90 var featureSubscription = featureSubscriptions[i];
91 updateToggleButton(featureSubscription.feature, isSubscriptionEnabled( featureSubscription));
92 }
93 } 116 }
94 } 117 });
95 FilterNotifier.addListener(filterListener); 118 ext.backgroundPage.sendMessage({type: "subscriptions.listen", filter: ["adde d", "removed", "disabled"]});
96 window.addEventListener("unload", function(event)
97 {
98 FilterNotifier.removeListener(filterListener);
99 }, false);
100
101 initSocialLinks();
102 } 119 }
103 120
104 function isSubscriptionEnabled(featureSubscription) 121 function initToggleSubscriptionButton(featureSubscription)
105 {
106 return featureSubscription.url in FilterStorage.knownSubscriptions
107 && !Subscription.fromURL(featureSubscription.url).disabled;
108 }
109
110 function setToggleSubscriptionButton(featureSubscription)
111 { 122 {
112 var feature = featureSubscription.feature; 123 var feature = featureSubscription.feature;
113 124
114 var element = E("toggle-" + feature); 125 var element = E("toggle-" + feature);
115 updateToggleButton(feature, isSubscriptionEnabled(featureSubscription));
116 element.addEventListener("click", function(event) 126 element.addEventListener("click", function(event)
117 { 127 {
118 var subscription = Subscription.fromURL(featureSubscription.url); 128 ext.backgroundPage.sendMessage({
119 if (isSubscriptionEnabled(featureSubscription)) 129 type: "subscriptions.toggle",
120 FilterStorage.removeSubscription(subscription); 130 url: featureSubscription.url,
121 else 131 title: featureSubscription.title,
122 { 132 homepage: featureSubscription.homepage
123 subscription.disabled = false; 133 });
124 subscription.title = featureSubscription.title;
125 subscription.homepage = featureSubscription.homepage;
126 FilterStorage.addSubscription(subscription);
127 if (!subscription.lastDownload)
128 Synchronizer.execute(subscription);
129 }
130 }, false); 134 }, false);
131 } 135 }
132 136
133 function openSharePopup(url) 137 function openSharePopup(url)
134 { 138 {
135 var iframe = E("share-popup"); 139 var iframe = E("share-popup");
136 var glassPane = E("glass-pane"); 140 var glassPane = E("glass-pane");
137 var popupMessageReceived = false; 141 var popupMessageReceived = false;
138 142
139 var popupMessageListener = function(event) 143 var popupMessageListener = function(event)
140 { 144 {
141 var originFilter = Filter.fromText("||adblockplus.org^"); 145 if (!/[.\/]adblockplus\.org$/.test(event.origin))
142 if (!originFilter.matches(event.origin, "OTHER", null, null))
143 return; 146 return;
144 147
145 var width = event.data.width; 148 var width = event.data.width;
146 var height = event.data.height; 149 var height = event.data.height;
147 iframe.width = width; 150 iframe.width = width;
148 iframe.height = height; 151 iframe.height = height;
149 iframe.style.marginTop = -height/2 + "px"; 152 iframe.style.marginTop = -height/2 + "px";
150 iframe.style.marginLeft = -width/2 + "px"; 153 iframe.style.marginLeft = -width/2 + "px";
151 popupMessageReceived = true; 154 popupMessageReceived = true;
152 window.removeEventListener("message", popupMessageListener); 155 window.removeEventListener("message", popupMessageListener);
(...skipping 27 matching lines...) Expand all
180 iframe.src = url; 183 iframe.src = url;
181 glassPane.className = "visible"; 184 glassPane.className = "visible";
182 } 185 }
183 186
184 function initSocialLinks() 187 function initSocialLinks()
185 { 188 {
186 var networks = ["twitter", "facebook", "gplus"]; 189 var networks = ["twitter", "facebook", "gplus"];
187 networks.forEach(function(network) 190 networks.forEach(function(network)
188 { 191 {
189 var link = E("share-" + network); 192 var link = E("share-" + network);
190 link.addEventListener("click", onSocialLinkClick, false); 193 var message = {
194 type: "filters.blocked",
195 url: link.getAttribute("data-script"),
196 requestType: "SCRIPT",
197 docDomain: "adblockplus.org",
198 thirdParty: true
199 };
200 ext.backgroundPage.sendMessage(message, function(blocked)
201 {
202 // Don't open the share page if the sharing script would be blocked
203 if (blocked)
204 link.removeEventListener("click", onSocialLinkClick, false);
205 else
206 link.addEventListener("click", onSocialLinkClick, false);
207 });
191 }); 208 });
192 } 209 }
193 210
194 function onSocialLinkClick(event) 211 function onSocialLinkClick(event)
195 { 212 {
196 // Don't open the share page if the sharing script would be blocked 213 event.preventDefault();
197 var filter = defaultMatcher.matchesAny(event.target.getAttribute("data-scrip t"), "SCRIPT", "adblockplus.org", true); 214
198 if (!(filter instanceof BlockingFilter)) 215 getDocLink(event.target.id, function(link)
199 { 216 {
200 event.preventDefault(); 217 openSharePopup(link);
201 openSharePopup(Utils.getDocLink(event.target.id)); 218 });
202 }
203 } 219 }
204 220
205 function setLinks(id) 221 function setLinks(id)
206 { 222 {
207 var element = E(id); 223 var element = E(id);
208 if (!element) 224 if (!element)
209 { 225 {
210 return; 226 return;
211 } 227 }
212 228
213 var links = element.getElementsByTagName("a"); 229 var links = element.getElementsByTagName("a");
214 230
215 for (var i = 0; i < links.length; i++) 231 for (var i = 0; i < links.length; i++)
216 { 232 {
217 if (typeof arguments[i + 1] == "string") 233 if (typeof arguments[i + 1] == "string")
218 { 234 {
219 links[i].href = arguments[i + 1]; 235 links[i].href = arguments[i + 1];
220 links[i].setAttribute("target", "_blank"); 236 links[i].setAttribute("target", "_blank");
221 } 237 }
222 else if (typeof arguments[i + 1] == "function") 238 else if (typeof arguments[i + 1] == "function")
223 { 239 {
224 links[i].href = "javascript:void(0);"; 240 links[i].href = "javascript:void(0);";
225 links[i].addEventListener("click", arguments[i + 1], false); 241 links[i].addEventListener("click", arguments[i + 1], false);
226 } 242 }
227 } 243 }
228 } 244 }
229 245
230 function openFilters() 246 function openFilters()
231 { 247 {
232 if (typeof UI != "undefined") 248 ext.backgroundPage.sendMessage({type: "app.options"});
233 UI.openFiltersDialog(); 249 }
234 else 250
251 function updateToggleButtons()
252 {
253 ext.backgroundPage.sendMessage({type: "subscriptions.get"}, function(subscri ptions)
235 { 254 {
236 backgroundPage.openOptions(); 255 for (var i = 0; i < featureSubscriptions.length; i++)
237 } 256 {
257 var featureSubscription = featureSubscriptions[i];
258 updateToggleButton(featureSubscription.feature, subscriptions.indexOf(fe atureSubscription.url) >= 0);
259 }
260 });
238 } 261 }
239 262
240 function updateToggleButton(feature, isEnabled) 263 function updateToggleButton(feature, isEnabled)
241 { 264 {
242 var button = E("toggle-" + feature); 265 var button = E("toggle-" + feature);
243 if (isEnabled) 266 if (isEnabled)
244 button.classList.remove("off"); 267 button.classList.remove("off");
245 else 268 else
246 button.classList.add("off"); 269 button.classList.add("off");
247 } 270 }
248 271
249 document.addEventListener("DOMContentLoaded", onDOMLoaded, false); 272 document.addEventListener("DOMContentLoaded", onDOMLoaded, false);
250 })(); 273 })();
OLDNEW
« no previous file with comments | « ext/content.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld