 Issue 29375899:
  Issue 4871 - Start using ESLint for adblockplusui  (Closed)
    
  
    Issue 29375899:
  Issue 4871 - Start using ESLint for adblockplusui  (Closed) 
  | Index: background.js | 
| diff --git a/background.js b/background.js | 
| index 5ef6f0d844267fa976b37e602c863c64a9f46100..825741b06b5e5311d32382f8f47889097f31c367 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 listener of listeners) | 
| + listener(...args); | 
| } | 
| } | 
| }; | 
| @@ -54,17 +55,16 @@ | 
| { | 
| if (window.location.search) | 
| { | 
| - var params = window.location.search.substr(1).split("&"); | 
| - for (var i = 0; i < params.length; i++) | 
| + for (let param of window.location.search.substr(1).split("&")) | 
| 
Thomas Greiner
2017/03/01 17:39:32
Detail: Putting that long statement within the for
 
kzar
2017/03/07 12:48:31
Done.
 | 
| { | 
| - 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 +74,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 +94,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 +131,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 +157,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 +203,51 @@ | 
| return knownSubscriptions; | 
| }, | 
| - addSubscription: function(subscription) | 
| + addSubscription(subscription) | 
| { | 
| - if (!(subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions)) | 
| + let {fromURL} = modules.subscriptionClasses.Subscription; | 
| 
Thomas Greiner
2017/03/01 17:39:34
Detail: You can now refer to `Subscription` direct
 
kzar
2017/03/07 12:48:31
Done.
 | 
| + 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 +255,46 @@ | 
| } | 
| }; | 
| - modules.filterClasses = { | 
| - BlockingFilter: function() {}, | 
| - Filter: function(text) | 
| - { | 
| - this.text = text; | 
| - this.disabled = false; | 
| - }, | 
| - RegExpFilter: function() {} | 
| - }; | 
| - modules.filterClasses.Filter.fromText = function(text) | 
| + function Filter(text) | 
| + { | 
| + this.text = text; | 
| + this.disabled = false; | 
| + } | 
| + Filter.fromText = text => | 
| 
Thomas Greiner
2017/03/01 17:39:34
Detail: Mind writing this as follows?
Filter.from
 
kzar
2017/03/07 12:48:30
Done.
 | 
| { | 
| return new modules.filterClasses.Filter(text); | 
| }; | 
| - modules.filterClasses.RegExpFilter.typeMap = Object.create(null); | 
| - modules.filterValidation = | 
| + function BlockingFilter() | 
| + { | 
| + } | 
| + | 
| + function RegExpFilter() | 
| + { | 
| + } | 
| + RegExpFilter.typeMap = Object.create(null); | 
| + | 
| + modules.filterClasses = { | 
| + BlockingFilter, | 
| + Filter, | 
| + RegExpFilter | 
| + }; | 
| + | 
| + 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 +304,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 +325,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 +366,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 +375,12 @@ | 
| if (!listeners) | 
| return; | 
| - function reply(message) | 
| + function reply(responseMessage) | 
| { | 
| event.source.postMessage({ | 
| type: "response", | 
| - messageId: messageId, | 
| - payload: message | 
| + messageId, | 
| + payload: responseMessage | 
| }, "*"); | 
| } | 
| @@ -369,29 +391,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 +434,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 +468,7 @@ | 
| }, 1000); | 
| } | 
| - ext.devtools.onCreated.addListener(function(panel) | 
| + ext.devtools.onCreated.addListener(panel => | 
| { | 
| // blocked request | 
| panel.sendMessage({ | 
| @@ -520,4 +544,4 @@ | 
| } | 
| }); | 
| }); | 
| -})(this); | 
| +} |