Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: messageResponder.js

Issue 29370999: Issue 4783 - Use modern JavaScript syntax for the messageResponder (Closed)
Patch Set: Created Jan. 11, 2017, 8:41 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: messageResponder.js
diff --git a/messageResponder.js b/messageResponder.js
index 3bb5c9b4016e2d540d6377e0d05fa15d2e42e66f..2027f687c13f8bea97868314b43766fdd46e9671 100644
--- a/messageResponder.js
+++ b/messageResponder.js
@@ -15,31 +15,31 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-(function(global)
+(global =>
Sebastian Noack 2017/01/11 16:29:08 Do we even need an IIFE with ES6? Wouldn't a simpl
kzar 2017/01/13 08:44:49 Good point, Done.
Sebastian Noack 2017/01/13 11:54:48 I just noticed, you'd also have to use strict mode
kzar 2017/01/16 04:41:49 Done.
{
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;
+ 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 +59,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 global.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 +88,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 +104,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 +121,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 +134,9 @@
listenedFilterChanges[name] = null;
FilterNotifier.on(name, function()
kzar 2017/01/11 08:50:44 (I originally used an arrow function here but that
{
- 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 +145,7 @@
function getListenerFilters(page)
{
- var listenerFilters = changeListeners.get(page);
+ let listenerFilters = changeListeners.get(page);
if (!listenerFilters)
{
listenerFilters = Object.create(null);
@@ -162,7 +158,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 +173,7 @@
if (message.what == "localeInfo")
{
- var bidiDir;
+ let bidiDir;
if ("chromeRegistry" in Utils)
bidiDir = Utils.chromeRegistry.isLocaleRTL("adblockplus") ? "rtl" : "ltr";
else
@@ -212,8 +208,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 +220,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 +231,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 +251,7 @@
return filters;
}
- var subscription = Subscription.fromURL(message.subscriptionUrl);
+ let subscription = Subscription.fromURL(message.subscriptionUrl);
if (!subscription)
return [];
@@ -264,22 +260,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 +279,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 +306,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 +325,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 +348,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 +356,7 @@
if (message.confirm)
{
- ext.showOptions(function()
+ ext.showOptions(() =>
{
sendMessage("app", "addSubscription", subscription);
});
@@ -382,7 +373,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 +395,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 +423,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);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld