Index: messageResponder.js |
diff --git a/messageResponder.js b/messageResponder.js |
index 3bb5c9b4016e2d540d6377e0d05fa15d2e42e66f..87adb17a7dfcc80ae7743805fa4bbe33e306a72d 100644 |
--- a/messageResponder.js |
+++ b/messageResponder.js |
@@ -15,31 +15,30 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
-(function(global) |
{ |
- if (!global.ext) |
- global.ext = require("ext_background"); |
- |
- var port = require("messaging").port; |
- var Prefs = require("prefs").Prefs; |
- var Utils = require("utils").Utils; |
- var FilterStorage = require("filterStorage").FilterStorage; |
- var FilterNotifier = require("filterNotifier").FilterNotifier; |
- var defaultMatcher = require("matcher").defaultMatcher; |
- var ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; |
- var NotificationStorage = require("notification").Notification; |
- |
- var filterClasses = require("filterClasses"); |
- var Filter = filterClasses.Filter; |
- var BlockingFilter = filterClasses.BlockingFilter; |
- var RegExpFilter = filterClasses.RegExpFilter; |
- var Synchronizer = require("synchronizer").Synchronizer; |
- |
- var info = require("info"); |
- var subscriptionClasses = require("subscriptionClasses"); |
- var Subscription = subscriptionClasses.Subscription; |
- var DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
- var SpecialSubscription = subscriptionClasses.SpecialSubscription; |
+ if (!ext) |
+ var ext = require("ext_background"); |
+ |
+ let port = require("messaging").port; |
+ let Prefs = require("prefs").Prefs; |
+ let Utils = require("utils").Utils; |
+ let FilterStorage = require("filterStorage").FilterStorage; |
+ let FilterNotifier = require("filterNotifier").FilterNotifier; |
+ let defaultMatcher = require("matcher").defaultMatcher; |
+ let ElemHideEmulation = require("elemHideEmulation").ElemHideEmulation; |
+ let NotificationStorage = require("notification").Notification; |
+ |
+ let filterClasses = require("filterClasses"); |
+ let Filter = filterClasses.Filter; |
+ let BlockingFilter = filterClasses.BlockingFilter; |
+ let RegExpFilter = filterClasses.RegExpFilter; |
+ let Synchronizer = require("synchronizer").Synchronizer; |
+ |
+ let info = require("info"); |
+ let subscriptionClasses = require("subscriptionClasses"); |
+ let Subscription = subscriptionClasses.Subscription; |
+ let DownloadableSubscription = subscriptionClasses.DownloadableSubscription; |
+ let SpecialSubscription = subscriptionClasses.SpecialSubscription; |
// Some modules doesn't exist on Firefox. Moreover, |
// require() throws an exception on Firefox in that case. |
@@ -59,30 +58,27 @@ |
function convertObject(keys, obj) |
{ |
- var result = {}; |
- for (var i = 0; i < keys.length; i++) |
- { |
- var key = keys[i]; |
+ let result = {}; |
+ for (let key of keys) |
if (key in obj) |
result[key] = obj[key]; |
- } |
return result; |
} |
function convertSubscription(subscription) |
{ |
- var obj = convertObject(["disabled", "downloadStatus", "homepage", |
+ let obj = convertObject(["disabled", "downloadStatus", "homepage", |
"lastDownload", "title", "url"], subscription); |
obj.isDownloading = Synchronizer.isExecuting(subscription.url); |
return obj; |
} |
- var convertFilter = convertObject.bind(null, ["text"]); |
+ let convertFilter = convertObject.bind(null, ["text"]); |
- var changeListeners = new global.ext.PageMap(); |
- var listenedPreferences = Object.create(null); |
- var listenedFilterChanges = Object.create(null); |
- var messageTypes = { |
+ let changeListeners = new ext.PageMap(); |
+ let listenedPreferences = Object.create(null); |
+ let listenedFilterChanges = Object.create(null); |
+ let messageTypes = { |
"app": "app.respond", |
"filter": "filters.respond", |
"pref": "prefs.respond", |
@@ -91,14 +87,14 @@ |
function sendMessage(type, action) |
{ |
- var pages = changeListeners.keys(); |
+ let pages = changeListeners.keys(); |
if (pages.length == 0) |
return; |
- var args = []; |
- for (var i = 2; i < arguments.length; i++) |
+ let args = []; |
+ for (let i = 2; i < arguments.length; i++) |
{ |
- var arg = arguments[i]; |
+ let arg = arguments[i]; |
if (arg instanceof Subscription) |
args.push(convertSubscription(arg)); |
else if (arg instanceof Filter) |
@@ -107,11 +103,10 @@ |
args.push(arg); |
} |
- for (var j = 0; j < pages.length; j++) |
+ for (let page of pages) |
{ |
- var page = pages[j]; |
- var filters = changeListeners.get(page); |
- var actions = filters[type]; |
+ let filters = changeListeners.get(page); |
+ let actions = filters[type]; |
if (actions && actions.indexOf(action) != -1) |
{ |
page.sendMessage({ |
@@ -125,9 +120,9 @@ |
function addFilterListeners(type, actions) |
{ |
- actions.forEach(function(action) |
+ actions.forEach(action => |
{ |
- var name; |
+ let name; |
if (type == "filter" && action == "loaded") |
name = "load"; |
else |
@@ -138,9 +133,9 @@ |
listenedFilterChanges[name] = null; |
FilterNotifier.on(name, function() |
{ |
- var args = [type, action]; |
- for (var i = 0; i < arguments.length; i++) |
- args.push(arguments[i]); |
+ let args = [type, action]; |
+ for (let arg of arguments) |
+ args.push(arg); |
sendMessage.apply(null, args); |
}); |
} |
@@ -149,7 +144,7 @@ |
function getListenerFilters(page) |
{ |
- var listenerFilters = changeListeners.get(page); |
+ let listenerFilters = changeListeners.get(page); |
if (!listenerFilters) |
{ |
listenerFilters = Object.create(null); |
@@ -162,7 +157,7 @@ |
{ |
if (message.what == "issues") |
{ |
- var subscriptionInit = tryRequire("subscriptionInit"); |
+ let subscriptionInit = tryRequire("subscriptionInit"); |
return { |
filterlistsReinitialized: subscriptionInit ? subscriptionInit.reinitialized : false, |
legacySafariVersion: (info.platform == "safari" && ( |
@@ -177,7 +172,7 @@ |
if (message.what == "localeInfo") |
{ |
- var bidiDir; |
+ let bidiDir; |
if ("chromeRegistry" in Utils) |
bidiDir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr"; |
else |
@@ -212,8 +207,8 @@ |
port.on("filters.add", (message, sender) => |
{ |
- var result = require("filterValidation").parseFilter(message.text); |
- var errors = []; |
+ let result = require("filterValidation").parseFilter(message.text); |
+ let errors = []; |
if (result.error) |
errors.push(result.error.toString()); |
else if (result.filter) |
@@ -224,7 +219,7 @@ |
port.on("filters.blocked", (message, sender) => |
{ |
- var filter = defaultMatcher.matchesAny(message.url, |
+ let filter = defaultMatcher.matchesAny(message.url, |
RegExpFilter.typeMap[message.requestType], message.docDomain, |
message.thirdParty); |
@@ -235,16 +230,16 @@ |
{ |
if (message.what == "elemhideemulation") |
{ |
- var filters = []; |
- var checkWhitelisted = require("whitelisting").checkWhitelisted; |
+ let filters = []; |
+ let checkWhitelisted = require("whitelisting").checkWhitelisted; |
if (Prefs.enabled && !checkWhitelisted(sender.page, sender.frame, |
RegExpFilter.typeMap.DOCUMENT | |
RegExpFilter.typeMap.ELEMHIDE)) |
{ |
- var hostname = sender.frame.url.hostname; |
+ let hostname = sender.frame.url.hostname; |
filters = ElemHideEmulation.getRulesForDomain(hostname); |
- filters = filters.map(function(filter) |
+ filters = filters.map(filter => |
{ |
return { |
selector: filter.selector, |
@@ -255,7 +250,7 @@ |
return filters; |
} |
- var subscription = Subscription.fromURL(message.subscriptionUrl); |
+ let subscription = Subscription.fromURL(message.subscriptionUrl); |
if (!subscription) |
return []; |
@@ -264,22 +259,18 @@ |
port.on("filters.importRaw", (message, sender) => |
{ |
- var result = require("filterValidation").parseFilters(message.text); |
- var errors = []; |
- for (var i = 0; i < result.errors.length; i++) |
- { |
- var error = result.errors[i]; |
+ let result = require("filterValidation").parseFilters(message.text); |
+ let errors = []; |
+ for (let error of result.errors) |
if (error.type != "unexpected-filter-list-header") |
errors.push(error.toString()); |
- } |
if (errors.length > 0) |
return errors; |
- var seenFilter = Object.create(null); |
- for (var i = 0; i < result.filters.length; i++) |
+ let seenFilter = Object.create(null); |
+ for (let filter of result.filters) |
{ |
- var filter = result.filters[i]; |
FilterStorage.addFilter(filter); |
seenFilter[filter.text] = null; |
} |
@@ -287,15 +278,14 @@ |
if (!message.removeExisting) |
return errors; |
- for (var i = 0; i < FilterStorage.subscriptions.length; i++) |
+ for (let subscription of FilterStorage.subscriptions) |
{ |
- var subscription = FilterStorage.subscriptions[i]; |
if (!(subscription instanceof SpecialSubscription)) |
continue; |
- for (var j = subscription.filters.length - 1; j >= 0; j--) |
+ for (let j = subscription.filters.length - 1; j >= 0; j--) |
{ |
- var filter = subscription.filters[j]; |
+ let filter = subscription.filters[j]; |
if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text)) |
continue; |
@@ -315,8 +305,8 @@ |
port.on("filters.remove", (message, sender) => |
{ |
- var filter = Filter.fromText(message.text); |
- var subscription = null; |
+ let filter = Filter.fromText(message.text); |
+ let subscription = null; |
if (message.subscriptionUrl) |
subscription = Subscription.fromURL(message.subscriptionUrl); |
@@ -334,12 +324,12 @@ |
port.on("prefs.listen", (message, sender) => |
{ |
getListenerFilters(sender.page).pref = message.filter; |
- message.filter.forEach(function(preference) |
+ message.filter.forEach(preference => |
{ |
if (!(preference in listenedPreferences)) |
{ |
listenedPreferences[preference] = null; |
- Prefs.on(preference, function() |
+ Prefs.on(preference, () => |
{ |
sendMessage("pref", preference, Prefs[preference]); |
}); |
@@ -357,7 +347,7 @@ |
port.on("subscriptions.add", (message, sender) => |
{ |
- var subscription = Subscription.fromURL(message.url); |
+ let subscription = Subscription.fromURL(message.url); |
if ("title" in message) |
subscription.title = message.title; |
if ("homepage" in message) |
@@ -365,7 +355,7 @@ |
if (message.confirm) |
{ |
- ext.showOptions(function() |
+ ext.showOptions(() => |
{ |
sendMessage("app", "addSubscription", subscription); |
}); |
@@ -382,7 +372,7 @@ |
port.on("subscriptions.get", (message, sender) => |
{ |
- var subscriptions = FilterStorage.subscriptions.filter(function(s) |
+ let subscriptions = FilterStorage.subscriptions.filter(s => |
{ |
if (message.ignoreDisabled && s.disabled) |
return false; |
@@ -404,14 +394,14 @@ |
port.on("subscriptions.remove", (message, sender) => |
{ |
- var subscription = Subscription.fromURL(message.url); |
+ let subscription = Subscription.fromURL(message.url); |
if (subscription.url in FilterStorage.knownSubscriptions) |
FilterStorage.removeSubscription(subscription); |
}); |
port.on("subscriptions.toggle", (message, sender) => |
{ |
- var subscription = Subscription.fromURL(message.url); |
+ let subscription = Subscription.fromURL(message.url); |
if (subscription.url in FilterStorage.knownSubscriptions) |
{ |
if (subscription.disabled || message.keepInstalled) |
@@ -432,13 +422,10 @@ |
port.on("subscriptions.update", (message, sender) => |
{ |
- var subscriptions = message.url ? [Subscription.fromURL(message.url)] : |
+ let subscriptions = message.url ? [Subscription.fromURL(message.url)] : |
FilterStorage.subscriptions; |
- for (var i = 0; i < subscriptions.length; i++) |
- { |
- var subscription = subscriptions[i]; |
+ for (let subscription of subscriptions) |
if (subscription instanceof DownloadableSubscription) |
Synchronizer.execute(subscription, true); |
- } |
}); |
-})(this); |
+} |