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

Side by Side Diff: background.js

Issue 8680033: Made sure to enable acceptable ads and to open the first-run page (Closed)
Patch Set: Created Oct. 25, 2012, 4:20 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 | « no previous file | lib/adblockplus_compat.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 with(require("filterClasses")) 1 with(require("filterClasses"))
2 { 2 {
3 this.Filter = Filter; 3 this.Filter = Filter;
4 this.RegExpFilter = RegExpFilter; 4 this.RegExpFilter = RegExpFilter;
5 this.BlockingFilter = BlockingFilter; 5 this.BlockingFilter = BlockingFilter;
6 this.WhitelistFilter = WhitelistFilter; 6 this.WhitelistFilter = WhitelistFilter;
7 } 7 }
8 with(require("subscriptionClasses")) 8 with(require("subscriptionClasses"))
9 { 9 {
10 this.Subscription = Subscription; 10 this.Subscription = Subscription;
11 this.DownloadableSubscription = DownloadableSubscription; 11 this.DownloadableSubscription = DownloadableSubscription;
12 } 12 }
13 var FilterStorage = require("filterStorage").FilterStorage; 13 var FilterStorage = require("filterStorage").FilterStorage;
14 var ElemHide = require("elemHide").ElemHide; 14 var ElemHide = require("elemHide").ElemHide;
15 var defaultMatcher = require("matcher").defaultMatcher; 15 var defaultMatcher = require("matcher").defaultMatcher;
16 var Prefs = require("prefs").Prefs;
16 var Synchronizer = require("synchronizer").Synchronizer; 17 var Synchronizer = require("synchronizer").Synchronizer;
18 var Utils = require("utils").Utils;
17 19
18 // Some types cannot be distinguished 20 // Some types cannot be distinguished
19 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 21 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
20 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 22 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
21 23
22 var isFirstRun = false; 24 var isFirstRun = false;
23 require("filterNotifier").FilterNotifier.addListener(function(action) 25 require("filterNotifier").FilterNotifier.addListener(function(action)
24 { 26 {
25 if (action == "load") 27 if (action == "load")
26 { 28 {
27 importOldData(); 29 importOldData();
28 if (!localStorage["currentVersion"]) 30
31 var addonVersion = require("info").addonVersion;
32 var prevVersion = localStorage["currentVersion"];
33 if (prevVersion != addonVersion)
29 { 34 {
30 isFirstRun = true; 35 isFirstRun = !prevVersion;
31 executeFirstRunActions(); 36 localStorage["currentVersion"] = addonVersion;
37 addSubscription(prevVersion);
32 } 38 }
33 localStorage["currentVersion"] = require("info").addonVersion;
34 } 39 }
35 }); 40 });
36 41
37 // Special-case domains for which we cannot use style-based hiding rules. 42 // Special-case domains for which we cannot use style-based hiding rules.
38 // See http://crbug.com/68705. 43 // See http://crbug.com/68705.
39 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; 44 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"];
40 45
41 // Sets options to defaults, upgrading old options from previous versions as nec essary 46 // Sets options to defaults, upgrading old options from previous versions as nec essary
42 function setDefaultOptions() 47 function setDefaultOptions()
43 { 48 {
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 if (/^https?:/.test(key)) 272 if (/^https?:/.test(key))
268 delete localStorage[key]; 273 delete localStorage[key];
269 } 274 }
270 catch (e) 275 catch (e)
271 { 276 {
272 reportError(e); 277 reportError(e);
273 } 278 }
274 } 279 }
275 280
276 /** 281 /**
277 * This function is called first time the extension runs after installation. 282 * This function is called on an extension update. It will add the default
278 * It will add the default filter subscription. 283 * filter subscription if necessary.
279 */ 284 */
280 function executeFirstRunActions() 285 function addSubscription(prevVersion)
281 { 286 {
282 // Don't do anything if the user has a subscription already 287 // Add "acceptable ads" subscription for new users and users updating from old ABP versions.
283 var hasSubscriptions = FilterStorage.subscriptions.some(function(subscription) {return subscription instanceof DownloadableSubscription}); 288 var addAcceptable = (!prevVersion || Services.vc.compare(prevVersion, "2.1") < 0);
284 if (hasSubscriptions) 289 if (addAcceptable)
290 {
291 addAcceptable = !FilterStorage.subscriptions.some(function(subscription)
292 {
293 return subscription.url == Prefs.subscriptions_exceptionsurl;
294 });
295 }
296
297 // Don't add subscription if the user has a subscription already
298 var addSubscription = !FilterStorage.subscriptions.some(function(subscription)
299 {
300 return subscription instanceof DownloadableSubscription &&
301 subscription.url != Prefs.subscriptions_exceptionsurl;
302 });
303
304 // If this isn't the first run, only add subscription if the user has no custo m filters
305 if (addSubscription && prevVersion)
306 {
307 addSubscription = !FilterStorage.subscriptions.some(function(subscription)
308 {
309 return subscription.url != Prefs.subscriptions_exceptionsurl &&
310 subscription.filters.length;
311 });
312 }
313
314 // Add "acceptable ads" subscription
315 if (addAcceptable)
316 {
317 var subscription = Subscription.fromURL(Prefs.subscriptions_exceptionsurl);
318 if (subscription)
319 {
320 subscription.title = "Allow non-intrusive advertising";
321 FilterStorage.addSubscription(subscription);
322 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
323 Synchronizer.execute(subscription);
324 }
325 else
326 addAcceptable = false;
327 }
328
329 if (!addSubscription && !addAcceptable)
285 return; 330 return;
286 331
287 // Load subscriptions data 332 function notifyUser()
288 var request = new XMLHttpRequest();
289 request.open("GET", "subscriptions.xml");
290 request.onload = function()
291 { 333 {
292 var subscriptions = request.responseXML.documentElement.getElementsByTagName ("subscription"); 334 chrome.tabs.create({
293 var selectedItem = null; 335 url: chrome.extension.getURL("firstRun.html")
294 var selectedPrefix = null; 336 });
295 var matchCount = 0; 337 }
296 for (var i = 0; i < subscriptions.length; i++) 338
339 if (addSubscription)
340 {
341 // Load subscriptions data
342 var request = new XMLHttpRequest();
343 request.open("GET", "subscriptions.xml");
344 request.addEventListener("load", function()
297 { 345 {
298 var subscription = subscriptions[i]; 346 var node = Utils.chooseFilterSubscription(request.responseXML.getElementsB yTagName("subscription"));
299 if (!selectedItem) 347 var subscription = (node ? Subscription.fromURL(node.getAttribute("url")) : null);
300 selectedItem = subscription; 348 if (subscription)
349 {
350 FilterStorage.addSubscription(subscription);
351 subscription.disabled = false;
352 subscription.title = node.getAttribute("title");
353 subscription.homepage = node.getAttribute("homepage");
354 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
355 Synchronizer.execute(subscription);
301 356
302 var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.ge tAttribute("prefixes")); 357 notifyUser();
303 if (prefix)
304 {
305 if (!selectedPrefix || selectedPrefix.length < prefix.length)
306 {
307 selectedItem = subscription;
308 selectedPrefix = prefix;
309 matchCount = 1;
310 }
311 else if (selectedPrefix && selectedPrefix.length == prefix.length)
312 {
313 matchCount++;
314
315 // If multiple items have a matching prefix of the same length:
316 // Select one of the items randomly, probability should be the same
317 // for all items. So we replace the previous match here with
318 // probability 1/N (N being the number of matches).
319 if (Math.random() * matchCount < 1)
320 {
321 selectedItem = subscription;
322 selectedPrefix = prefix;
323 }
324 }
325 } 358 }
326 } 359 }, false);
327 360 request.send(null);
328 var subscription = (selectedItem ? Subscription.fromURL(selectedItem.getAttr ibute("url")) : null); 361 }
329 if (subscription) 362 else
330 { 363 notifyUser();
331 subscription.disabled = false;
332 subscription.title = selectedItem.getAttribute("title");
333 subscription.homepage = selectedItem.getAttribute("homepage");
334 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
335 Synchronizer.execute(subscription);
336 FilterStorage.addSubscription(subscription);
337 }
338
339 subscription = Subscription.fromURL("https://easylist-downloads.adblockplus. org/chrome_supplement.txt");
340 subscription.disabled = false;
341 subscription.title = "Recommended filters for Google Chrome"
342 if (subscription instanceof DownloadableSubscription && !subscription.lastDo wnload)
343 Synchronizer.execute(subscription);
344 FilterStorage.addSubscription(subscription);
345 };
346 request.send(null);
347 } 364 }
348 365
349 // Set up context menu for user selection of elements to block 366 // Set up context menu for user selection of elements to block
350 function showContextMenu() 367 function showContextMenu()
351 { 368 {
352 chrome.contextMenus.removeAll(function() 369 chrome.contextMenus.removeAll(function()
353 { 370 {
354 if(typeof localStorage["shouldShowBlockElementMenu"] == "string" && localSto rage["shouldShowBlockElementMenu"] == "true") 371 if(typeof localStorage["shouldShowBlockElementMenu"] == "string" && localSto rage["shouldShowBlockElementMenu"] == "true")
355 { 372 {
356 chrome.contextMenus.create({'title': chrome.i18n.getMessage('block_element '), 'contexts': ['image', 'video', 'audio'], 'onclick': function(info, tab) 373 chrome.contextMenus.create({'title': chrome.i18n.getMessage('block_element '), 'contexts': ['image', 'video', 'audio'], 'onclick': function(info, tab)
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 refreshIconAndContextMenu(windows[i].tabs[j]); 538 refreshIconAndContextMenu(windows[i].tabs[j]);
522 }); 539 });
523 540
524 // Update icon if a tab changes location 541 // Update icon if a tab changes location
525 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 542 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
526 { 543 {
527 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 544 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
528 if(changeInfo.status == "loading") 545 if(changeInfo.status == "loading")
529 refreshIconAndContextMenu(tab); 546 refreshIconAndContextMenu(tab);
530 }); 547 });
OLDNEW
« no previous file with comments | « no previous file | lib/adblockplus_compat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld