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

Side by Side Diff: background.js

Issue 8403145: First attempt at creating a first-run page (Closed)
Patch Set: Now with downscaling for smaller screens Created Oct. 1, 2012, 5:56 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
1 importAll("filterClasses", this); 1 importAll("filterClasses", this);
2 importAll("subscriptionClasses", this); 2 importAll("subscriptionClasses", this);
3 importAll("filterStorage", this); 3 importAll("filterStorage", this);
4 importAll("elemHide", this); 4 importAll("elemHide", this);
5 importAll("filterListener", this); 5 importAll("filterListener", this);
6 importAll("filterNotifier", this); 6 importAll("filterNotifier", this);
7 importAll("matcher", this); 7 importAll("matcher", this);
8 importAll("prefs", this); 8 importAll("prefs", this);
9 importAll("synchronizer", this); 9 importAll("synchronizer", this);
10 importAll("utils", this); 10 importAll("utils", this);
11 11
12 // Some types cannot be distinguished 12 // Some types cannot be distinguished
13 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 13 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
14 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 14 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
15 15
16 var isFirstRun = false;
Felix Dahlke 2012/10/02 12:36:04 It seems like this variable is never read, so I gu
Wladimir Palant 2012/10/17 10:25:39 Yes, that's code from ABP/Chrome used by the first
16 FilterNotifier.addListener(function(action) 17 FilterNotifier.addListener(function(action)
17 { 18 {
18 if (action == "load") 19 if (action == "load")
19 { 20 {
20 importOldData(); 21 importOldData();
21 if (!localStorage["currentVersion"]) 22 if (!localStorage["currentVersion"])
23 {
24 isFirstRun = true;
22 executeFirstRunActions(); 25 executeFirstRunActions();
26 }
23 localStorage["currentVersion"] = require("info").addonVersion; 27 localStorage["currentVersion"] = require("info").addonVersion;
24 } 28 }
25 }); 29 });
26 30
27 // Special-case domains for which we cannot use style-based hiding rules. 31 // Special-case domains for which we cannot use style-based hiding rules.
28 // See http://crbug.com/68705. 32 // See http://crbug.com/68705.
29 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; 33 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"];
30 34
31 // Sets options to defaults, upgrading old options from previous versions as nec essary 35 // Sets options to defaults, upgrading old options from previous versions as nec essary
32 function setDefaultOptions() 36 function setDefaultOptions()
33 { 37 {
34 function defaultOptionValue(opt, val) 38 function defaultOptionValue(opt, val)
35 { 39 {
36 if(!(opt in localStorage)) 40 if(!(opt in localStorage))
37 localStorage[opt] = val; 41 localStorage[opt] = val;
38 } 42 }
39 43
40 defaultOptionValue("shouldShowIcon", "true"); 44 defaultOptionValue("shouldShowIcon", "true");
41 defaultOptionValue("shouldShowBlockElementMenu", "true"); 45 defaultOptionValue("shouldShowBlockElementMenu", "true");
42 defaultOptionValue("disableInlineTextAds", "false"); 46 defaultOptionValue("disableInlineTextAds", "false");
43 47
44 // If user had older version installed, get rid of old option 48 // If user had older version installed, get rid of old option
45 if ("specialCaseYouTube" in localStorage) 49 if ("specialCaseYouTube" in localStorage)
46 delete localStorage.specialCaseYouTube; 50 delete localStorage.specialCaseYouTube;
47 if ("experimental" in localStorage) 51 if ("experimental" in localStorage)
48 delete localStorage.experimental; 52 delete localStorage.experimental;
49 } 53 }
50 54
51 // Upgrade options before we do anything else. 55 // Upgrade options before we do anything else.
52 setDefaultOptions(); 56 setDefaultOptions();
53 57
54 /** 58 /**
55 * Checks whether a page is whitelisted. 59 * Checks whether a page is whitelisted.
56 * @param {String} url 60 * @param {String} url
57 * @param {String} [type] content type to be checked, default is "DOCUMENT" 61 * @param {String} [type] content type to be checked, default is "DOCUMENT"
58 * @return {Filter} filter that matched the URL or null if not whitelisted 62 * @return {Filter} filter that matched the URL or null if not whitelisted
59 */ 63 */
60 function isWhitelisted(url, type) 64 function isWhitelisted(url, type)
61 { 65 {
62 // Ignore fragment identifier 66 // Ignore fragment identifier
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 { 349 {
346 chrome.contextMenus.create({'title': chrome.i18n.getMessage('block_element '), 'contexts': ['image', 'video', 'audio'], 'onclick': function(info, tab) 350 chrome.contextMenus.create({'title': chrome.i18n.getMessage('block_element '), 'contexts': ['image', 'video', 'audio'], 'onclick': function(info, tab)
347 { 351 {
348 if(info.srcUrl) 352 if(info.srcUrl)
349 chrome.tabs.sendRequest(tab.id, {reqtype: "clickhide-new-filter", fi lter: info.srcUrl}); 353 chrome.tabs.sendRequest(tab.id, {reqtype: "clickhide-new-filter", fi lter: info.srcUrl});
350 }}); 354 }});
351 } 355 }
352 }); 356 });
353 } 357 }
354 358
359 /**
360 * Opens Options window or focuses an existing one.
361 * @param {Function} callback function to be called with the window object of
362 * the Options window
363 */
364 function openOptions(callback)
365 {
366 function findOptions(selectTab)
367 {
368 var views = chrome.extension.getViews({type: "tab"});
369 for (var i = 0; i < views.length; i++)
370 if ("startSubscriptionSelection" in views[i])
371 return views[i];
372
373 return null;
374 }
375
376 function selectOptionsTab()
377 {
378 chrome.windows.getAll({populate: true}, function(windows)
379 {
380 var url = chrome.extension.getURL("options.html");
381 for (var i = 0; i < windows.length; i++)
382 for (var j = 0; j < windows[i].tabs.length; j++)
383 if (windows[i].tabs[j].url == url)
384 chrome.tabs.update(windows[i].tabs[j].id, {selected: true});
385 });
386 }
387
388 var view = findOptions();
389 if (view)
390 {
391 selectOptionsTab();
392 callback(view);
393 }
394 else
395 {
396 var onLoad = function()
397 {
398 var view = findOptions();
399 if (view)
400 callback(view);
401 };
402
403 chrome.tabs.create({url: chrome.extension.getURL("options.html")}, function( tab)
404 {
405 if (tab.status == "complete")
406 onLoad();
407 else
408 {
409 var id = tab.id;
410 var listener = function(tabId, changeInfo, tab)
411 {
412 if (tabId == id && changeInfo.status == "complete")
413 {
414 chrome.tabs.onUpdated.removeListener(listener);
415 onLoad();
416 }
417 };
418 chrome.tabs.onUpdated.addListener(listener);
419 }
420 });
421 }
422 }
423
355 chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 424 chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
356 { 425 {
357 switch (request.reqtype) 426 switch (request.reqtype)
358 { 427 {
359 case "get-settings": 428 case "get-settings":
360 var hostDomain = null; 429 var hostDomain = null;
361 var selectors = null; 430 var selectors = null;
362 431
363 // HACK: We don't know which frame sent us the message, try to find it 432 // HACK: We don't know which frame sent us the message, try to find it
364 // in webRequest's frame data. 433 // in webRequest's frame data.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 } 486 }
418 break; 487 break;
419 case "add-filters": 488 case "add-filters":
420 if (request.filters && request.filters.length) 489 if (request.filters && request.filters.length)
421 { 490 {
422 for (var i = 0; i < request.filters.length; i++) 491 for (var i = 0; i < request.filters.length; i++)
423 FilterStorage.addFilter(Filter.fromText(request.filters[i])); 492 FilterStorage.addFilter(Filter.fromText(request.filters[i]));
424 } 493 }
425 break; 494 break;
426 case "add-subscription": 495 case "add-subscription":
427 function doAddSubscription(selectTab) 496 openOptions(function(view)
428 { 497 {
429 var views = chrome.extension.getViews({type: "tab"}); 498 view.startSubscriptionSelection(request.title, request.url);
430 var view = null; 499 });
431 for (var i = 0; i < views.length; i++)
432 if ("startSubscriptionSelection" in views[i])
433 view = views[i];
434
435 if (view)
436 {
437 view.startSubscriptionSelection(request.title, request.url);
438 if (selectTab)
439 {
440 chrome.windows.getAll({populate: true}, function(windows)
441 {
442 var url = chrome.extension.getURL("options.html");
443 for (var i = 0; i < windows.length; i++)
444 for (var j = 0; j < windows[i].tabs.length; j++)
445 if (windows[i].tabs[j].url == url)
446 return chrome.tabs.update(windows[i].tabs[j].id, {selected: true});
447 });
448 }
449 }
450 return view;
451 }
452
453 if (!doAddSubscription(true))
454 {
455 chrome.tabs.create({url: chrome.extension.getURL("options.html")}, funct ion(tab)
456 {
457 if (tab.status == "complete")
458 doAddSubscription()
459 else
460 {
461 var id = tab.id;
462 var listener = function(tabId, changeInfo, tab)
463 {
464 if (tabId == id && changeInfo.status == "complete")
465 {
466 chrome.tabs.onUpdated.removeListener(listener);
467 doAddSubscription();
468 }
469 };
470 chrome.tabs.onUpdated.addListener(listener);
471 }
472 });
473 }
474 break; 500 break;
475 case "forward": 501 case "forward":
476 chrome.tabs.sendRequest(sender.tab.id, request.request, sendResponse); 502 chrome.tabs.sendRequest(sender.tab.id, request.request, sendResponse);
477 break; 503 break;
478 default: 504 default:
479 sendResponse({}); 505 sendResponse({});
480 break; 506 break;
481 } 507 }
482 }); 508 });
483 509
484 // Show icon as page action for all tabs that already exist 510 // Show icon as page action for all tabs that already exist
485 chrome.windows.getAll({populate: true}, function(windows) 511 chrome.windows.getAll({populate: true}, function(windows)
486 { 512 {
487 for (var i = 0; i < windows.length; i++) 513 for (var i = 0; i < windows.length; i++)
488 for (var j = 0; j < windows[i].tabs.length; j++) 514 for (var j = 0; j < windows[i].tabs.length; j++)
489 refreshIconAndContextMenu(windows[i].tabs[j]); 515 refreshIconAndContextMenu(windows[i].tabs[j]);
490 }); 516 });
491 517
492 // Update icon if a tab changes location 518 // Update icon if a tab changes location
493 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 519 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
494 { 520 {
495 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 521 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
496 if(changeInfo.status == "loading") 522 if(changeInfo.status == "loading")
497 refreshIconAndContextMenu(tab); 523 refreshIconAndContextMenu(tab);
498 }); 524 });
OLDNEW

Powered by Google App Engine
This is Rietveld