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: Error handling improvements Created July 14, 2015, 9:55 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 | « 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,8 @@
var Filter = filterClasses.Filter;
var BlockingFilter = filterClasses.BlockingFilter;
var Synchronizer = require("synchronizer").Synchronizer;
+ var parseFilters = require("filterValidation").parseFilters;
+ var parseFilter = require("filterValidation").parseFilter;
var subscriptionClasses = require("subscriptionClasses");
var Subscription = subscriptionClasses.Subscription;
@@ -46,6 +48,8 @@
var convertSubscription = convertObject.bind(null, ["disabled",
"downloadStatus", "homepage", "lastSuccess", "title", "url"]);
+ var convertFilterParsingError = convertObject.bind(null, ["type",
Thomas Greiner 2015/07/14 10:58:53 This function is no longer used.
saroyanm 2015/07/14 11:19:26 Done.
+ "reason", "selector"]);
var convertFilter = convertObject.bind(null, ["text"]);
var changeListeners = null;
@@ -55,6 +59,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 +106,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)
{
@@ -195,12 +203,58 @@
callback(subscription.filters.map(convertFilter));
break;
+ case "filters.importRaw":
+ var result = parseFilters(message.text);
+ var errors = result.errors.filter(function(error)
+ {
+ return error.type != "unexpected-filter-list-header";
+ });
+
+ if (errors.length > 0)
+ {
+ sendMessage("app", "error", [errors.join("\n")], sender.page);
Thomas Greiner 2015/07/14 10:58:53 Just a suggestion: Since you're passing an array a
saroyanm 2015/07/14 11:19:26 Good point. Done.
Thomas Greiner 2015/07/14 11:40:08 This won't work because this would send the Filter
saroyanm 2015/07/14 13:15:47 Done.
+ 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;
+
+ var filters = subscription.filters.slice();
+ for (var j = 0; j < filters.length; j++)
+ {
+ var filter = 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;
else
delete listenerFilters.filter;
break;
+ case "filters.parse":
+ var result = parseFilter(message.text);
+ if (result.error)
+ sendMessage("app", "error", [result.error.toString()], sender.page);
+ else
+ FilterStorage.addFilter(result.filter);
+ break;
case "filters.remove":
var filter = Filter.fromText(message.text);
var subscription = null;
« no previous file with comments | « locale/en-US/options.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld