Index: background.js |
diff --git a/background.js b/background.js |
index 5ef6f0d844267fa976b37e602c863c64a9f46100..d09eabd1faba863962cdc7757aa135fda0e224b9 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() |
Wladimir Palant
2017/03/09 15:05:06
Please revert to IIFE here - functions should not
kzar
2017/03/10 07:28:58
Done.
But unless I missed something passing windo
Wladimir Palant
2017/03/14 11:14:57
Not strictly necessary here, it was merely there t
kzar
2017/03/15 03:16:57
Acknowledged.
|
{ |
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 listener of listeners) |
+ listener(...args); |
} |
} |
}; |
@@ -54,17 +55,18 @@ |
{ |
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 param of params) |
{ |
- var parts = params[i].split("=", 2); |
+ let parts = param.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 +76,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,34 +96,34 @@ |
}; |
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) |
+ for (let key of Object.keys(prefs)) |
{ |
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); |
} |
}); |
- }); |
+ } |
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,36 +133,15 @@ |
} |
}; |
- modules.subscriptionClasses = { |
- Subscription: function(url) |
- { |
- this.url = url; |
- this._disabled = false; |
- this._lastDownload = 1234; |
- this.homepage = "https://easylist.adblockplus.org/"; |
- this.downloadStatus = params.downloadStatus; |
- }, |
- |
- SpecialSubscription: function(url) |
- { |
- this.url = url; |
- this.disabled = false; |
- this.filters = knownFilters.slice(); |
- } |
- }; |
- modules.subscriptionClasses.Subscription.fromURL = function(url) |
+ function Subscription(url) |
{ |
- if (url in knownSubscriptions) |
- return knownSubscriptions[url]; |
- |
- if (/^https?:\/\//.test(url)) |
- return new modules.subscriptionClasses.Subscription(url); |
- else |
- return new modules.subscriptionClasses.SpecialSubscription(url); |
- }; |
- modules.subscriptionClasses.DownloadableSubscription = modules.subscriptionClasses.Subscription; |
- |
- modules.subscriptionClasses.Subscription.prototype = |
+ this.url = url; |
+ this._disabled = false; |
+ this._lastDownload = 1234; |
+ this.homepage = "https://easylist.adblockplus.org/"; |
+ this.downloadStatus = params.downloadStatus; |
+ } |
+ Subscription.prototype = |
{ |
get disabled() |
{ |
@@ -178,18 +159,44 @@ |
set lastDownload(value) |
{ |
this._lastDownload = value; |
- modules.filterNotifier.FilterNotifier.emit("subscription.lastDownload", this); |
+ modules.filterNotifier.FilterNotifier.emit("subscription.lastDownload", |
+ this); |
} |
}; |
+ Subscription.fromURL = function(url) |
+ { |
+ if (url in knownSubscriptions) |
+ return knownSubscriptions[url]; |
+ if (/^https?:\/\//.test(url)) |
+ return new modules.subscriptionClasses.Subscription(url); |
+ return new modules.subscriptionClasses.SpecialSubscription(url); |
+ }; |
+ |
+ function SpecialSubscription(url) |
+ { |
+ this.url = url; |
+ this.disabled = false; |
+ this.filters = knownFilters.slice(); |
+ } |
+ |
+ modules.subscriptionClasses = { |
+ Subscription, |
+ SpecialSubscription, |
+ DownloadableSubscription: Subscription |
+ }; |
modules.filterStorage = { |
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,43 +205,51 @@ |
return knownSubscriptions; |
}, |
- addSubscription: function(subscription) |
+ addSubscription(subscription) |
{ |
- if (!(subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions)) |
+ let {fromURL} = 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 customFilter of customSubscription.filters) |
{ |
- if (customSubscription.filters[i].text == filter.text) |
+ if (customFilter.text == filter.text) |
return; |
} |
customSubscription.filters.push(filter); |
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 +257,43 @@ |
} |
}; |
- modules.filterClasses = { |
- BlockingFilter: function() {}, |
- Filter: function(text) |
- { |
- this.text = text; |
- this.disabled = false; |
- }, |
- RegExpFilter: function() {} |
- }; |
- modules.filterClasses.Filter.fromText = function(text) |
+ function Filter(text) |
{ |
- return new modules.filterClasses.Filter(text); |
+ this.text = text; |
+ this.disabled = false; |
+ } |
+ Filter.fromText = (text) => new Filter(text); |
+ |
+ function BlockingFilter() |
+ { |
+ } |
+ |
+ function RegExpFilter() |
+ { |
+ } |
+ RegExpFilter.typeMap = Object.create(null); |
+ |
+ modules.filterClasses = { |
+ BlockingFilter, |
+ Filter, |
+ RegExpFilter |
}; |
- 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 +303,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 +324,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; |
} |
} |
}; |
@@ -339,12 +360,12 @@ |
port: new EventEmitter() |
}; |
- window.addEventListener("message", event => |
+ window.addEventListener("message", (event) => |
{ |
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 +374,12 @@ |
if (!listeners) |
return; |
- function reply(message) |
+ function reply(responseMessage) |
{ |
event.source.postMessage({ |
type: "response", |
- messageId: messageId, |
- payload: message |
+ messageId, |
+ payload: responseMessage |
}, "*"); |
} |
@@ -369,7 +390,8 @@ |
{ |
response.then( |
reply, |
- reason => { |
+ (reason) => |
+ { |
console.error(reason); |
reply(undefined); |
} |
@@ -382,16 +404,16 @@ |
} |
}); |
- 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 +435,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 +469,7 @@ |
}, 1000); |
} |
- ext.devtools.onCreated.addListener(function(panel) |
+ ext.devtools.onCreated.addListener((panel) => |
{ |
// blocked request |
panel.sendMessage({ |
@@ -520,4 +545,4 @@ |
} |
}); |
}); |
-})(this); |
+} |