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

Unified Diff: messageResponder.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Mockup simplification and validation update Created July 14, 2015, 6:17 p.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
« background.js ('K') | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: messageResponder.js
===================================================================
--- a/messageResponder.js
+++ b/messageResponder.js
@@ -30,6 +30,7 @@
var Filter = filterClasses.Filter;
var BlockingFilter = filterClasses.BlockingFilter;
var Synchronizer = require("synchronizer").Synchronizer;
+ var filterValidation = require("filterValidation");
var subscriptionClasses = require("subscriptionClasses");
var Subscription = subscriptionClasses.Subscription;
@@ -55,6 +56,23 @@
"subscription": "subscriptions.listen"
};
+ function sendMessage(type, action, args, page)
+ {
+ var pages = page ? [page] : changeListeners.keys();
+ for (var i = 0; i < pages.length; i++)
+ {
+ var filters = changeListeners.get(pages[i]);
+ if (filters[type] && filters[type].indexOf(action) >= 0)
+ {
+ pages[i].sendMessage({
+ type: messageTypes[type],
+ action: action,
+ args: args
+ });
+ }
+ }
+ }
+
function onFilterChange(action)
{
if (action == "load")
@@ -85,21 +103,8 @@
else
return arg;
});
-
- var pages = changeListeners.keys();
- for (var i = 0; i < pages.length; i++)
- {
- var filters = changeListeners.get(pages[i]);
- if (filters[type] && filters[type].indexOf(action) >= 0)
- {
- pages[i].sendMessage({
- type: messageTypes[type],
- action: action,
- args: args
- });
- }
- }
- };
+ sendMessage(type, action, args);
+ }
global.ext.onMessage.addListener(function(message, sender, callback)
{
@@ -178,7 +183,11 @@
break;
case "filters.add":
var filter = Filter.fromText(message.text);
- FilterStorage.addFilter(filter);
+ var result = filterValidation.parseFilter(message.text);
+ if (result.error && result.error.type != "empty-filter")
Thomas Greiner 2015/07/15 08:32:38 Look at the else-case. This means that empty filte
Sebastian Noack 2015/07/15 09:00:45 There is no error type "empty-filter". As I said,
saroyanm 2015/07/15 10:47:11 I think it was misunderstanding, what I understood
Sebastian Noack 2015/07/15 11:19:59 What I meant is, that you should merely leave the
+ sendMessage("app", "error", [result.error.toString()], sender.page);
+ else
+ FilterStorage.addFilter(result.filter);
break;
case "filters.blocked":
var filter = defaultMatcher.matchesAny(message.url, message.requestType,
@@ -195,6 +204,48 @@
callback(subscription.filters.map(convertFilter));
break;
+ case "filters.importRaw":
+ var result = filterValidation.parseFilters(message.text);
+ var errors = [];
+ for (var i = 0; i < result.errors.length; i++)
+ {
+ var error = result.errors[i];
+ if (error.type != "unexpected-filter-list-header" &&
+ error.type != "empty-filter")
+ errors.push(error.toString());
+ }
+
+ if (errors.length > 0)
+ {
+ sendMessage("app", "error", errors, sender.page);
+ return;
+ }
+
+ var seenFilter = Object.create(null);
+ for (var i = 0; i < result.filters.length; i++)
+ {
+ var filter = result.filters[i];
+ FilterStorage.addFilter(filter);
+ seenFilter[filter.text] = null;
+ }
+
+ for (var i = 0; i < FilterStorage.subscriptions.length; i++)
+ {
+ var subscription = FilterStorage.subscriptions[i];
+ if (!(subscription instanceof SpecialSubscription))
+ continue;
+
+ for (var j = subscription.filters.length - 1; j >= 0; j--)
+ {
+ var filter = subscription.filters[j];
+ if (/^@@\|\|([^\/:]+)\^\$document$/.test(filter.text))
+ continue;
+
+ if (!(filter.text in seenFilter))
+ FilterStorage.removeFilter(filter);
+ }
+ }
+ break;
case "filters.listen":
if (message.filter)
listenerFilters.filter = message.filter;
« background.js ('K') | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld