Index: messageResponder.js |
=================================================================== |
--- a/messageResponder.js |
+++ b/messageResponder.js |
@@ -28,8 +28,11 @@ |
var filterClasses = require("filterClasses"); |
var Filter = filterClasses.Filter; |
+ var WhitelistFilter = filterClasses.WhitelistFilter |
Thomas Greiner
2015/07/13 15:54:23
This line is no longer necessary. Same applies for
saroyanm
2015/07/14 10:20:59
Done.
|
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 +49,8 @@ |
var convertSubscription = convertObject.bind(null, ["disabled", |
"downloadStatus", "homepage", "lastSuccess", "title", "url"]); |
+ var convertFilterParsingError = convertObject.bind(null, ["type", |
+ "reason", "selector"]); |
var convertFilter = convertObject.bind(null, ["text"]); |
var changeListeners = null; |
@@ -55,6 +60,15 @@ |
"subscription": "subscriptions.listen" |
}; |
+ function sendMessage(page, type, action, args) |
Thomas Greiner
2015/07/13 15:54:22
This function is not providing any added value. It
saroyanm
2015/07/14 10:20:58
Done.
|
+ { |
+ page.sendMessage({ |
+ type: type, |
+ action: action, |
+ args: args |
+ }); |
+ } |
+ |
function onFilterChange(action) |
{ |
if (action == "load") |
@@ -91,16 +105,25 @@ |
{ |
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(pages[i], messageTypes[type], action, args); |
} |
}; |
+ function sendError(error, page) |
Thomas Greiner
2015/07/13 15:54:22
This function signature is inconsistent because yo
|
+ { |
+ var type = "app"; |
+ var action = "error"; |
+ error = convertFilterParsingError(error); |
Thomas Greiner
2015/07/13 15:54:22
Since `FilterParsingError.toString()` does not exi
|
+ if (!page) |
+ { |
+ var pages = changeListeners.keys(); |
+ for (var i = 0; i < pages.length; i++) |
+ sendMessage(pages[i], messageTypes[type], action, error); |
+ } |
+ else |
+ sendMessage(page, messageTypes[type], action, [error]); |
+ } |
+ |
global.ext.onMessage.addListener(function(message, sender, callback) |
{ |
var listenerFilters = null; |
@@ -195,12 +218,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) |
+ { |
+ sendError(errors.join("\n"), sender.page); |
Thomas Greiner
2015/07/13 15:54:22
Note that after making the changes above you proba
saroyanm
2015/07/14 10:20:58
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) |
+ sendError(result.error, sender.page); |
Thomas Greiner
2015/07/13 15:54:22
Note that after making the changes above you proba
saroyanm
2015/07/14 10:20:59
Done.
|
+ else |
+ FilterStorage.addFilter(result.filter); |
+ break; |
case "filters.remove": |
var filter = Filter.fromText(message.text); |
var subscription = null; |