Index: background.js |
=================================================================== |
--- a/background.js |
+++ b/background.js |
@@ -8,23 +8,27 @@ importAll("matcher", this); |
importAll("prefs", this); |
importAll("synchronizer", this); |
importAll("utils", this); |
// Some types cannot be distinguished |
RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; |
RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OTHER; |
+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
|
FilterNotifier.addListener(function(action) |
{ |
if (action == "load") |
{ |
importOldData(); |
if (!localStorage["currentVersion"]) |
+ { |
+ isFirstRun = true; |
executeFirstRunActions(); |
+ } |
localStorage["currentVersion"] = require("info").addonVersion; |
} |
}); |
// Special-case domains for which we cannot use style-based hiding rules. |
// See http://crbug.com/68705. |
var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; |
@@ -44,17 +48,17 @@ function setDefaultOptions() |
// If user had older version installed, get rid of old option |
if ("specialCaseYouTube" in localStorage) |
delete localStorage.specialCaseYouTube; |
if ("experimental" in localStorage) |
delete localStorage.experimental; |
} |
// Upgrade options before we do anything else. |
-setDefaultOptions(); |
+setDefaultOptions(); |
/** |
* Checks whether a page is whitelisted. |
* @param {String} url |
* @param {String} [type] content type to be checked, default is "DOCUMENT" |
* @return {Filter} filter that matched the URL or null if not whitelisted |
*/ |
function isWhitelisted(url, type) |
@@ -347,16 +351,81 @@ function showContextMenu() |
{ |
if(info.srcUrl) |
chrome.tabs.sendRequest(tab.id, {reqtype: "clickhide-new-filter", filter: info.srcUrl}); |
}}); |
} |
}); |
} |
+/** |
+ * Opens Options window or focuses an existing one. |
+ * @param {Function} callback function to be called with the window object of |
+ * the Options window |
+ */ |
+function openOptions(callback) |
+{ |
+ function findOptions(selectTab) |
+ { |
+ var views = chrome.extension.getViews({type: "tab"}); |
+ for (var i = 0; i < views.length; i++) |
+ if ("startSubscriptionSelection" in views[i]) |
+ return views[i]; |
+ |
+ return null; |
+ } |
+ |
+ function selectOptionsTab() |
+ { |
+ chrome.windows.getAll({populate: true}, function(windows) |
+ { |
+ var url = chrome.extension.getURL("options.html"); |
+ for (var i = 0; i < windows.length; i++) |
+ for (var j = 0; j < windows[i].tabs.length; j++) |
+ if (windows[i].tabs[j].url == url) |
+ chrome.tabs.update(windows[i].tabs[j].id, {selected: true}); |
+ }); |
+ } |
+ |
+ var view = findOptions(); |
+ if (view) |
+ { |
+ selectOptionsTab(); |
+ callback(view); |
+ } |
+ else |
+ { |
+ var onLoad = function() |
+ { |
+ var view = findOptions(); |
+ if (view) |
+ callback(view); |
+ }; |
+ |
+ chrome.tabs.create({url: chrome.extension.getURL("options.html")}, function(tab) |
+ { |
+ if (tab.status == "complete") |
+ onLoad(); |
+ else |
+ { |
+ var id = tab.id; |
+ var listener = function(tabId, changeInfo, tab) |
+ { |
+ if (tabId == id && changeInfo.status == "complete") |
+ { |
+ chrome.tabs.onUpdated.removeListener(listener); |
+ onLoad(); |
+ } |
+ }; |
+ chrome.tabs.onUpdated.addListener(listener); |
+ } |
+ }); |
+ } |
+} |
+ |
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) |
{ |
switch (request.reqtype) |
{ |
case "get-settings": |
var hostDomain = null; |
var selectors = null; |
@@ -419,63 +488,20 @@ chrome.extension.onRequest.addListener(f |
case "add-filters": |
if (request.filters && request.filters.length) |
{ |
for (var i = 0; i < request.filters.length; i++) |
FilterStorage.addFilter(Filter.fromText(request.filters[i])); |
} |
break; |
case "add-subscription": |
- function doAddSubscription(selectTab) |
+ openOptions(function(view) |
{ |
- var views = chrome.extension.getViews({type: "tab"}); |
- var view = null; |
- for (var i = 0; i < views.length; i++) |
- if ("startSubscriptionSelection" in views[i]) |
- view = views[i]; |
- |
- if (view) |
- { |
- view.startSubscriptionSelection(request.title, request.url); |
- if (selectTab) |
- { |
- chrome.windows.getAll({populate: true}, function(windows) |
- { |
- var url = chrome.extension.getURL("options.html"); |
- for (var i = 0; i < windows.length; i++) |
- for (var j = 0; j < windows[i].tabs.length; j++) |
- if (windows[i].tabs[j].url == url) |
- return chrome.tabs.update(windows[i].tabs[j].id, {selected: true}); |
- }); |
- } |
- } |
- return view; |
- } |
- |
- if (!doAddSubscription(true)) |
- { |
- chrome.tabs.create({url: chrome.extension.getURL("options.html")}, function(tab) |
- { |
- if (tab.status == "complete") |
- doAddSubscription() |
- else |
- { |
- var id = tab.id; |
- var listener = function(tabId, changeInfo, tab) |
- { |
- if (tabId == id && changeInfo.status == "complete") |
- { |
- chrome.tabs.onUpdated.removeListener(listener); |
- doAddSubscription(); |
- } |
- }; |
- chrome.tabs.onUpdated.addListener(listener); |
- } |
- }); |
- } |
+ view.startSubscriptionSelection(request.title, request.url); |
+ }); |
break; |
case "forward": |
chrome.tabs.sendRequest(sender.tab.id, request.request, sendResponse); |
break; |
default: |
sendResponse({}); |
break; |
} |