Index: background.js |
diff --git a/background.js b/background.js |
index 5ef6f0d844267fa976b37e602c863c64a9f46100..6dbb745b89e9ed32152db8746b707423f11beb49 100644 |
--- a/background.js |
+++ b/background.js |
@@ -15,37 +15,38 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
-(function(global) |
+"use strict"; |
+ |
{ |
function EventEmitter() |
{ |
this._listeners = Object.create(null); |
} |
EventEmitter.prototype = { |
- on: function(name, listener) |
+ on(name, listener) |
{ |
if (name in this._listeners) |
this._listeners[name].push(listener); |
else |
this._listeners[name] = [listener]; |
}, |
- off: function(name, listener) |
+ off(name, listener) |
{ |
- var listeners = this._listeners[name]; |
+ let listeners = this._listeners[name]; |
if (listeners) |
{ |
- var idx = listeners.indexOf(listener); |
+ let idx = listeners.indexOf(listener); |
if (idx != -1) |
listeners.splice(idx, 1); |
} |
}, |
- emit: function(name) |
+ emit(name, ...args) |
{ |
- var listeners = this._listeners[name]; |
+ let listeners = this._listeners[name]; |
if (listeners) |
{ |
- for (var i = 0; i < listeners.length; i++) |
- listeners[i].apply(null, Array.prototype.slice.call(arguments, 1)); |
+ for (let i = 0; i < listeners.length; i++) |
+ listeners[i](...args); |
} |
} |
}; |
@@ -54,17 +55,17 @@ |
{ |
if (window.location.search) |
{ |
- var params = window.location.search.substr(1).split("&"); |
- for (var i = 0; i < params.length; i++) |
+ let params = window.location.search.substr(1).split("&"); |
+ for (let i = 0; i < params.length; i++) |
{ |
- var parts = params[i].split("=", 2); |
+ let parts = params[i].split("=", 2); |
if (parts.length == 2 && parts[0] in data) |
data[parts[0]] = decodeURIComponent(parts[1]); |
} |
} |
} |
- var params = { |
+ let params = { |
blockedURLs: "", |
filterlistsReinitialized: false, |
addSubscription: false, |
@@ -74,15 +75,15 @@ |
}; |
updateFromURL(params); |
- var modules = {}; |
- global.require = function(module) |
+ let modules = {}; |
+ window.require = function(module) |
{ |
return modules[module]; |
}; |
modules.utils = { |
Utils: { |
- getDocLink: function(link) |
+ getDocLink(link) |
{ |
return "https://adblockplus.org/redirect?link=" + encodeURIComponent(link); |
}, |
@@ -94,21 +95,21 @@ |
}; |
modules.prefs = {Prefs: new EventEmitter()}; |
- var prefs = { |
+ let prefs = { |
notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [], |
notifications_showui: params.showNotificationUI, |
shouldShowBlockElementMenu: true, |
show_devtools_panel: true, |
subscriptions_exceptionsurl: "https://easylist-downloads.adblockplus.org/exceptionrules.txt" |
}; |
- Object.keys(prefs).forEach(function(key) |
+ Object.keys(prefs).forEach(key => |
{ |
Object.defineProperty(modules.prefs.Prefs, key, { |
- get: function() |
+ get() |
{ |
return prefs[key]; |
}, |
- set: function(value) |
+ set(value) |
{ |
prefs[key] = value; |
modules.prefs.Prefs.emit(key); |
@@ -118,10 +119,10 @@ |
modules.notification = { |
Notification: { |
- toggleIgnoreCategory: function(category) |
+ toggleIgnoreCategory(category) |
{ |
- var categories = prefs.notifications_ignoredcategories; |
- var index = categories.indexOf(category); |
+ let categories = prefs.notifications_ignoredcategories; |
+ let index = categories.indexOf(category); |
if (index == -1) |
categories.push(category); |
else |
@@ -131,6 +132,7 @@ |
} |
}; |
+ /* eslint-disable object-shorthand */ |
Sebastian Noack
2017/02/21 12:21:20
Note that the object-shorthand rule has an ignoreC
kzar
2017/02/23 11:10:22
Done.
|
modules.subscriptionClasses = { |
Subscription: function(url) |
{ |
@@ -155,10 +157,11 @@ |
if (/^https?:\/\//.test(url)) |
return new modules.subscriptionClasses.Subscription(url); |
- else |
- return new modules.subscriptionClasses.SpecialSubscription(url); |
+ return new modules.subscriptionClasses.SpecialSubscription(url); |
}; |
- modules.subscriptionClasses.DownloadableSubscription = modules.subscriptionClasses.Subscription; |
+ /* eslint-enable object-shorthand */ |
+ modules.subscriptionClasses.DownloadableSubscription = |
+ modules.subscriptionClasses.Subscription; |
modules.subscriptionClasses.Subscription.prototype = |
{ |
@@ -178,7 +181,8 @@ |
set lastDownload(value) |
{ |
this._lastDownload = value; |
- modules.filterNotifier.FilterNotifier.emit("subscription.lastDownload", this); |
+ modules.filterNotifier.FilterNotifier.emit("subscription.lastDownload", |
+ this); |
} |
}; |
@@ -187,9 +191,13 @@ |
FilterStorage: { |
get subscriptions() |
{ |
- var subscriptions = []; |
- for (var url in modules.filterStorage.FilterStorage.knownSubscriptions) |
- subscriptions.push(modules.filterStorage.FilterStorage.knownSubscriptions[url]); |
+ let subscriptions = []; |
+ for (let url in modules.filterStorage.FilterStorage.knownSubscriptions) |
+ { |
+ subscriptions.push( |
+ modules.filterStorage.FilterStorage.knownSubscriptions[url] |
+ ); |
+ } |
return subscriptions; |
}, |
@@ -198,27 +206,34 @@ |
return knownSubscriptions; |
}, |
- addSubscription: function(subscription) |
+ addSubscription(subscription) |
{ |
- if (!(subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions)) |
+ let {fromURL} = modules.subscriptionClasses.Subscription; |
+ let {FilterStorage} = modules.filterStorage; |
+ |
+ if (!(subscription.url in FilterStorage.knownSubscriptions)) |
{ |
- knownSubscriptions[subscription.url] = modules.subscriptionClasses.Subscription.fromURL(subscription.url); |
- modules.filterNotifier.FilterNotifier.emit("subscription.added", subscription); |
+ knownSubscriptions[subscription.url] = fromURL(subscription.url); |
+ modules.filterNotifier.FilterNotifier.emit("subscription.added", |
+ subscription); |
} |
}, |
- removeSubscription: function(subscription) |
+ removeSubscription(subscription) |
{ |
- if (subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions) |
+ let {FilterStorage} = modules.filterStorage; |
+ |
+ if (subscription.url in FilterStorage.knownSubscriptions) |
{ |
delete knownSubscriptions[subscription.url]; |
- modules.filterNotifier.FilterNotifier.emit("subscription.removed", subscription); |
+ modules.filterNotifier.FilterNotifier.emit("subscription.removed", |
+ subscription); |
} |
}, |
- addFilter: function(filter) |
+ addFilter(filter) |
{ |
- for (var i = 0; i < customSubscription.filters.length; i++) |
+ for (let i = 0; i < customSubscription.filters.length; i++) |
{ |
if (customSubscription.filters[i].text == filter.text) |
return; |
@@ -227,14 +242,15 @@ |
modules.filterNotifier.FilterNotifier.emit("filter.added", filter); |
}, |
- removeFilter: function(filter) |
+ removeFilter(filter) |
{ |
- for (var i = 0; i < customSubscription.filters.length; i++) |
+ for (let i = 0; i < customSubscription.filters.length; i++) |
{ |
if (customSubscription.filters[i].text == filter.text) |
{ |
customSubscription.filters.splice(i, 1); |
- modules.filterNotifier.FilterNotifier.emit("filter.removed", filter); |
+ modules.filterNotifier.FilterNotifier.emit("filter.removed", |
+ filter); |
return; |
} |
} |
@@ -242,36 +258,38 @@ |
} |
}; |
+ /* eslint-disable object-shorthand */ |
modules.filterClasses = { |
- BlockingFilter: function() {}, |
+ BlockingFilter: () => {}, |
Filter: function(text) |
{ |
this.text = text; |
this.disabled = false; |
}, |
- RegExpFilter: function() {} |
+ RegExpFilter: () => {} |
}; |
+ /* eslint-enable object-shorthand */ |
modules.filterClasses.Filter.fromText = function(text) |
{ |
return new modules.filterClasses.Filter(text); |
}; |
modules.filterClasses.RegExpFilter.typeMap = Object.create(null); |
- modules.filterValidation = |
+ modules.filterValidation = |
{ |
- parseFilter: function(text) |
+ parseFilter(text) |
{ |
if (params.filterError) |
return {error: "Invalid filter"}; |
return {filter: modules.filterClasses.Filter.fromText(text)}; |
}, |
- parseFilters: function(text) |
+ parseFilters(text) |
{ |
if (params.filterError) |
return {errors: ["Invalid filter"]}; |
return { |
filters: text.split("\n") |
- .filter(function(filter) {return !!filter;}) |
+ .filter(filter => !!filter) |
.map(modules.filterClasses.Filter.fromText), |
errors: [] |
}; |
@@ -281,19 +299,19 @@ |
modules.synchronizer = { |
Synchronizer: { |
_downloading: false, |
- execute: function(subscription, manual) |
+ execute(subscription, manual) |
{ |
modules.synchronizer.Synchronizer._downloading = true; |
modules.filterNotifier.FilterNotifier.emit( |
"subscription.downloading", subscription |
); |
- setTimeout(function() |
+ setTimeout(() => |
{ |
modules.synchronizer.Synchronizer._downloading = false; |
subscription.lastDownload = Date.now() / 1000; |
}, 500); |
}, |
- isExecuting: function(url) |
+ isExecuting(url) |
{ |
return modules.synchronizer.Synchronizer._downloading; |
} |
@@ -302,13 +320,12 @@ |
modules.matcher = { |
defaultMatcher: { |
- matchesAny: function(url, requestType, docDomain, thirdParty) |
+ matchesAny(url, requestType, docDomain, thirdParty) |
{ |
- var blocked = params.blockedURLs.split(","); |
+ let blocked = params.blockedURLs.split(","); |
if (blocked.indexOf(url) >= 0) |
return new modules.filterClasses.BlockingFilter(); |
- else |
- return null; |
+ return null; |
} |
} |
}; |
@@ -344,7 +361,7 @@ |
if (event.data.type != "message") |
return; |
let message = event.data.payload; |
- let messageId = event.data.messageId; |
+ let {messageId} = event.data; |
let sender = { |
page: new ext.Page(event.source) |
}; |
@@ -353,12 +370,12 @@ |
if (!listeners) |
return; |
- function reply(message) |
+ function reply(responseMessage) |
{ |
event.source.postMessage({ |
type: "response", |
- messageId: messageId, |
- payload: message |
+ messageId, |
+ payload: responseMessage |
}, "*"); |
} |
@@ -369,29 +386,28 @@ |
{ |
response.then( |
reply, |
- reason => { |
+ reason => |
+ { |
console.error(reason); |
reply(undefined); |
} |
); |
} |
else if (typeof response != "undefined") |
- { |
reply(response); |
- } |
} |
}); |
- global.Services = { |
+ window.Services = { |
vc: { |
- compare: function(v1, v2) |
+ compare(v1, v2) |
{ |
return parseFloat(v1) - parseFloat(v2); |
} |
} |
}; |
- var filters = [ |
+ let filters = [ |
"@@||alternate.de^$document", |
"@@||der.postillion.com^$document", |
"@@||taz.de^$document", |
@@ -413,24 +429,27 @@ |
"###ad-bereich2-08", |
"###ad-bereich2-skyscrapper" |
]; |
- var knownFilters = filters.map(modules.filterClasses.Filter.fromText); |
+ let knownFilters = filters.map(modules.filterClasses.Filter.fromText); |
- var subscriptions = [ |
+ let subscriptions = [ |
"https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt", |
"https://easylist-downloads.adblockplus.org/exceptionrules.txt", |
"https://easylist-downloads.adblockplus.org/fanboy-social.txt", |
"~user~786254" |
]; |
- var knownSubscriptions = Object.create(null); |
- for (var subscriptionUrl of subscriptions) |
- knownSubscriptions[subscriptionUrl] = modules.subscriptionClasses.Subscription.fromURL(subscriptionUrl); |
- var customSubscription = knownSubscriptions["~user~786254"]; |
+ let knownSubscriptions = Object.create(null); |
+ for (let subscriptionUrl of subscriptions) |
+ { |
+ knownSubscriptions[subscriptionUrl] = |
+ modules.subscriptionClasses.Subscription.fromURL(subscriptionUrl); |
+ } |
+ let customSubscription = knownSubscriptions["~user~786254"]; |
if (params.addSubscription) |
{ |
// We don't know how long it will take for the page to fully load |
// so we'll post the message after one second |
- setTimeout(function() |
+ setTimeout(() => |
{ |
window.postMessage({ |
type: "message", |
@@ -444,7 +463,7 @@ |
}, 1000); |
} |
- ext.devtools.onCreated.addListener(function(panel) |
+ ext.devtools.onCreated.addListener(panel => |
{ |
// blocked request |
panel.sendMessage({ |
@@ -520,4 +539,4 @@ |
} |
}); |
}); |
-})(this); |
+} |