Index: background.js |
=================================================================== |
rename from files/background.js |
rename to background.js |
--- a/files/background.js |
+++ b/background.js |
@@ -1,112 +1,146 @@ |
-/*opera.extension.windows.addEventListener( "focus", function(){ |
- opera.postError('Focus: ' + opera.extension.tabs.getSelected().url); |
+var FilterNotifier = require("filterNotifier").FilterNotifier; |
+var FilterStorage = require("filterStorage").FilterStorage; |
+var Synchronizer = require("synchronizer").Synchronizer; |
+var Subscription = require("subscriptionClasses").Subscription; |
+var DownloadableSubscription = require("subscriptionClasses").DownloadableSubscription; |
+ |
+var isFirstRun = false; |
+FilterNotifier.addListener(function(action) |
+{ |
+ if (action == "load") |
+ { |
+ importOldData(); |
+ if (!window.localStorage.currentVersion) |
+ { |
+ isFirstRun = true; |
+ executeFirstRunActions(); |
+ } |
+ window.localStorage.currentVersion = require("info").addonVersion; |
+ } |
}); |
-opera.extension.windows.addEventListener( "change", function(){ |
- opera.postError('Change: ' + opera.extension.tabs.getSelected()); |
-}); |
-opera.extension.windows.addEventListener( "create", function(){ |
- opera.postError('Create: ' + opera.extension.tabs.getSelected()); |
-}); |
-opera.extension.windows.addEventListener( "update", function(){ |
- opera.postError('Update: ' + opera.extension.tabs.getSelected()); |
-});*/ |
-opera.extension.onmessage = function (event) { |
- button.update(); |
- //opera.postError('BACKGROUND: got a ' + event.data.request + ' request'); |
- switch(event.data.request) { |
- case 'css': |
- //opera.postError('BACKGROUND: got reply from ' + event.data.url); |
- if(whitelist.check(event.data.url)) |
- event.source.postMessage({request: event.data.request, reply: css.personal().join("\n"), id: 'OAB_Perso'}); |
- else |
- event.source.postMessage({request: event.data.request, reply: '', id: 'OAB_Perso'}); |
- |
- for(key in sources.css) { //Sends them one by one to avoid contamination in cases where one is corrupted |
- if(css.status(key) && whitelist.check(event.data.url)) |
- event.source.postMessage({request: event.data.request, reply: css.get(key).join("\n"), id: 'OAB_' + key}); |
- else |
- event.source.postMessage({request: event.data.request, reply: '', id: 'OAB_' + key}); |
- } |
- break; |
- case 'ping': |
- //opera.postError('BACKGROUND: ping received'); |
- opera.extension.broadcastMessage({request: 'ping', type: event.data.type}); |
- break; |
- case 'list': |
- if(event.data.enable) |
- lists.enable(event.data.key); |
- else |
- lists.disable(event.data.key); |
- break; |
- case 'whitelist': //request: whitelist // type: add | remove // rule: RULE |
- if(event.data.type == 'add') |
- opera.extension.urlfilter.allow.add(event.data.rule); |
- else if(event.data.type == 'remove') |
- opera.extension.urlfilter.allow.remove(event.data.rule); |
- break; |
- /*case 'button': |
- if(event.data.enable) |
- button.enable(); |
- else |
- button.disable();*/ |
- } |
+function importOldData() |
+{ |
+ // TODO: Remove this once most people have the update |
+ if ("version" in widget.preferences) |
+ { |
+ var oldLists = { |
+ "fanboy": "https://secure.fanboy.co.nz/fanboy-adblock.txt", |
+ "fanboy-ru": "https://secure.fanboy.co.nz/fanboy-russian.txt", |
+ "fanboy-es": "https://secure.fanboy.co.nz/fanboy-espanol.txt", |
+ "fanboy-ja": "https://secure.fanboy.co.nz/fanboy-japanese.txt", |
+ "fanboy-cz": "https://secure.fanboy.co.nz/fanboy-czech.txt", |
+ "fanboy-tr": "https://secure.fanboy.co.nz/fanboy-turkish.txt", |
+ "fanboy-pl": "https://secure.fanboy.co.nz/fanboy-polish.txt", |
+ "fanboy-vi": "https://secure.fanboy.co.nz/fanboy-vietnam.txt", |
+ "fanboy-sv": "https://secure.fanboy.co.nz/fanboy-swedish.txt", |
+ "fanboy-ko": "https://secure.fanboy.co.nz/fanboy-korean.txt", |
+ "fanboy-ann": "https://secure.fanboy.co.nz/fanboy-addon.txt", |
+ "easy": "https://easylist-downloads.adblockplus.org/easylist.txt", |
+ "easy-ru": "https://ruadlist.googlecode.com/svn/trunk/advblock.txt", |
+ "easy-de": "https://easylist-downloads.adblockplus.org/easylistgermany.txt", |
+ "easy-fr": "http://lian.info.tm/liste_fr.txt", |
+ "easy-zh": "https://adblock-chinalist.googlecode.com/svn/trunk/adblock.txt", |
+ "easy-id": "https://indonesianadblockrules.googlecode.com/hg/subscriptions/abpindo.txt", |
+ "easy-bg": "http://stanev.org/abp/adblock_bg.txt", |
+ "easy-ja": "https://adblock-plus-japanese-filter.googlecode.com/hg/abp_jp.txt", |
+ "easy-el": "https://www.void.gr/kargig/void-gr-filters.txt", |
+ "easy-pl": "http://adblocklist.org/adblock-pxf-polish.txt", |
+ "easy-nl": "https://dutchadblockfilters.googlecode.com/svn/trunk/AdBlock_Dutch_hide.txt", |
+ "easy-ro": "http://www.zoso.ro/pages/rolist.txt", |
+ "easy-fi": "http://www.wiltteri.net/wiltteri.txt", |
+ "easy-es": "http://abp.mozilla-hispano.org/nauscopio/filtros.txt", |
+ "easy-hu": "http://ajnasz.hu/adblock/recent", |
+ "adblock-lv": "https://gitorious.org/adblock-latvian/adblock-latvian/blobs/raw/master/lists/latvian-list.txt", |
+ "easy-priv": "https://easylist-downloads.adblockplus.org/easyprivacy.txt", |
+ "easy-ru-priv": "https://ruadlist.googlecode.com/svn/trunk/cntblock.txt" |
+ }; |
+ |
+ for (var key in oldLists) |
+ { |
+ if (key in widget.preferences) |
+ { |
+ // Only add subscriptions that were enabled in old version |
+ if (widget.preferences[key] == "true") |
+ { |
+ var subscription = Subscription.fromURL(oldLists[key]); |
+ if (subscription && !(subscription.url in FilterStorage.knownSubscriptions)) |
+ { |
+ FilterStorage.addSubscription(subscription); |
+ Synchronizer.execute(subscription); |
Felix Dahlke
2012/10/11 13:10:02
This line causes an exception.
|
+ } |
+ } |
+ |
+ delete widget.preferences[key]; |
+ delete widget.preferences[key + "-content"]; |
+ delete widget.preferences[key + "-time"]; |
+ } |
+ } |
+ |
+ // TODO: Import custom filters? |
+ |
+ delete widget.preferences.version; |
+ } |
} |
-if(window.widget.version != preferences.string('version')) { |
- if(!preferences.exist('version')) { // New install |
- lists.enable('easy'); |
- } |
- else { //Update from previous versions |
- if(preferences.float('version') < 0.60) { //Updating from versions before Next, only enabled lists are ported |
- var enabledLists = new Array(); |
- for(key in sources.all()) { //Notes all enabled lists |
- if(preferences.bool(key)) |
- enabledLists.push(key); |
- } |
- var enabledCss = new Array(); |
- for(key in sources.css) { //Notes all enabled lists |
- if(preferences.bool(key)) |
- enabledCss.push(key); |
- } |
- var whitelist = lists.whitelist.get(); |
- |
- widget.preferences.clear(); //Removes all old preferences |
- |
- for(i in enabledLists) { //Enable all previously enabled lists |
- lists.enable(enabledLists[i]); |
- } |
- for(i in enabledCss) { //Enable all previously enabled lists |
- css.enable(enabledCss[i], false); |
- } |
- lists.whitelist.set(whitelist); |
- } |
- else if(preferences.float('version') <= 0.71) { //Updating from versions before lists merge and change |
- widget.preferences.clear(); //Removes all old preferences |
- // As no stable version exist between 0.52 and 0.71, the settings are not ported and simply removed from the extension |
- } |
- |
- } |
- //Updates the version |
- preferences.string('version', window.widget.version); |
+function executeFirstRunActions() |
+{ |
+ // Don't do anything if the user has a subscription already |
+ var hasSubscriptions = FilterStorage.subscriptions.some(function(subscription) {return subscription instanceof DownloadableSubscription}); |
+ if (hasSubscriptions) |
+ return; |
+ |
+ // Load subscriptions data |
+ var request = new XMLHttpRequest(); |
+ request.open("GET", "subscriptions.xml"); |
+ request.onload = function() |
+ { |
+ var subscriptions = request.responseXML.documentElement.getElementsByTagName("subscription"); |
+ var selectedItem = null; |
+ var selectedPrefix = null; |
+ var matchCount = 0; |
+ for (var i = 0; i < subscriptions.length; i++) |
+ { |
+ var subscription = subscriptions[i]; |
+ if (!selectedItem) |
+ selectedItem = subscription; |
+ |
+ var prefix = require("utils").Utils.checkLocalePrefixMatch(subscription.getAttribute("prefixes")); |
+ if (prefix) |
+ { |
+ if (!selectedPrefix || selectedPrefix.length < prefix.length) |
+ { |
+ selectedItem = subscription; |
+ selectedPrefix = prefix; |
+ matchCount = 1; |
+ } |
+ else if (selectedPrefix && selectedPrefix.length == prefix.length) |
+ { |
+ matchCount++; |
+ |
+ // If multiple items have a matching prefix of the same length: |
+ // Select one of the items randomly, probability should be the same |
+ // for all items. So we replace the previous match here with |
+ // probability 1/N (N being the number of matches). |
+ if (Math.random() * matchCount < 1) |
+ { |
+ selectedItem = subscription; |
+ selectedPrefix = prefix; |
+ } |
+ } |
+ } |
+ } |
+ |
+ var subscription = (selectedItem ? Subscription.fromURL(selectedItem.getAttribute("url")) : null); |
+ if (subscription) |
+ { |
+ subscription.disabled = false; |
+ subscription.title = selectedItem.getAttribute("title"); |
+ subscription.homepage = selectedItem.getAttribute("homepage"); |
+ if (subscription instanceof DownloadableSubscription && !subscription.lastDownload) |
+ Synchronizer.execute(subscription); |
+ FilterStorage.addSubscription(subscription); |
+ } |
+ }; |
+ request.send(null); |
} |
- |
-function updateAll() { |
- //opera.postError('updateAll() called'); |
- for(key in sources.css) { |
- if(css.status(key) && getTime() - css.time(key) > preferences.int('interval')) { //If the list is enabled |
- css.update(key); |
- //opera.postError(key + ' updated'); |
- } |
- } |
- for(key in sources.all()) { |
- if(lists.status(key) && getTime() - lists.time(key) > preferences.int('interval')) { //If the list is enabled |
- lists.update(key); |
- //opera.postError(key + ' updated'); |
- } |
- } |
- window.setTimeout('updateAll()', preferences.int('interval') / 12); //Verifies required updates every 1/12 of the update interval |
-} |
- |
-lists.setup(); //Enables all lists |
-updateAll(); //Sets update intervals |