Commit 013e59d1 authored by Thomas Greiner's avatar Thomas Greiner

Issue 1528 - Implemented backend for general tab of new options page

parent 1db98897
......@@ -59,3 +59,16 @@ The behavior of this page is affected by a number of URL parameters:
`true` should trigger warnings referring to issues detected by Adblock Plus.
* `blockedURLs`: a comma-separated list of URLs that should be considered
blocked (necessary to test the check for blocked scripts in sharing buttons).
options.html
------------
This is the implementation of the Adblock Plus options page which is the primary
UI for changing settings and for managing filter lists.
The behavior of this page is affected by a number of URL parameters:
* `addonVersion`: sets addon version application parameter that is used for
creating the link to the version-specific release notes
* `addSubscription=true`: this parameter should trigger a dialog for adding
subscriptions as initiated by clicking on an "abp:subscribe" link
......@@ -31,12 +31,6 @@
}
}
var subscriptions =[
"https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
"https://easylist-downloads.adblockplus.org/exceptionrules.txt",
"https://easylist-downloads.adblockplus.org/fanboy-social.txt"
];
var modules = {};
global.require = function(module)
{
......@@ -56,6 +50,12 @@
}
};
modules.prefs = {
Prefs: {
"subscriptions_exceptionsurl": "https://easylist-downloads.adblockplus.org/exceptionrules.txt"
}
}
modules.subscriptionClasses = {
Subscription: function(url)
{
......@@ -65,11 +65,19 @@
this.lastDownload = 1234;
},
SpecialSubscription: function() {}
SpecialSubscription: function(url)
{
this.url = url;
this.disabled = false;
this.filters = knownFilters.slice();
}
};
modules.subscriptionClasses.Subscription.fromURL = function(url)
{
return new modules.subscriptionClasses.Subscription(url);
if (/^https?:\/\//.test(url))
return new modules.subscriptionClasses.Subscription(url);
else
return new modules.subscriptionClasses.SpecialSubscription(url);
};
modules.subscriptionClasses.DownloadableSubscription = modules.subscriptionClasses.Subscription;
......@@ -77,41 +85,72 @@
FilterStorage: {
get subscriptions()
{
return subscriptions.map(modules.subscriptionClasses.Subscription.fromURL);
var subscriptions = [];
for (var url in modules.filterStorage.FilterStorage.knownSubscriptions)
subscriptions.push(modules.filterStorage.FilterStorage.knownSubscriptions[url]);
return subscriptions;
},
get knownSubscriptions()
{
var result = {};
for (var i = 0; i < subscriptions.length; i++)
result[subscriptions[i]] = modules.subscriptionClasses.Subscription.fromURL(subscriptions[i]);
return result;
return knownSubscriptions;
},
addSubscription: function(subscription)
{
var index = subscriptions.indexOf(subscription.url);
if (index < 0)
if (!(subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions))
{
subscriptions.push(subscription.url);
knownSubscriptions[subscription.url] = modules.subscriptionClasses.Subscription.fromURL(subscription.url);
modules.filterNotifier.FilterNotifier.triggerListeners("subscription.added", subscription);
}
},
removeSubscription: function(subscription)
{
var index = subscriptions.indexOf(subscription.url);
if (index >= 0)
if (subscription.url in modules.filterStorage.FilterStorage.knownSubscriptions)
{
subscriptions.splice(index, 1);
delete knownSubscriptions[subscription.url];
modules.filterNotifier.FilterNotifier.triggerListeners("subscription.removed", subscription);
}
},
addFilter: function(filter)
{
for (var i = 0; i < customSubscription.filters.length; i++)
{
if (customSubscription.filters[i].text == filter.text)
return;
}
customSubscription.filters.push(filter);
modules.filterNotifier.FilterNotifier.triggerListeners("filter.added", filter);
},
removeFilter: function(filter)
{
for (var i = 0; i < customSubscription.filters.length; i++)
{
if (customSubscription.filters[i].text == filter.text)
{
customSubscription.filters.splice(i, 1);
modules.filterNotifier.FilterNotifier.triggerListeners("filter.removed", filter);
return;
}
}
}
}
};
modules.filterClasses = {
BlockingFilter: function() {}
BlockingFilter: function() {},
Filter: function(text)
{
this.text = text;
this.disabled = false;
}
};
modules.filterClasses.Filter.fromText = function(text)
{
return new modules.filterClasses.Filter(text);
};
modules.synchronizer = {
......@@ -163,7 +202,9 @@
platform: "gecko",
platformVersion: "34.0",
application: "firefox",
applicationVersion: "34.0"
applicationVersion: "34.0",
addonName: "adblockplus",
addonVersion: "2.6.7"
};
updateFromURL(modules.info);
......@@ -176,8 +217,46 @@
}
};
var filters = [
"@@||alternate.de^$document",
"@@||der.postillion.com^$document",
"@@||taz.de^$document",
"@@||amazon.de^$document"
];
var knownFilters = filters.map(modules.filterClasses.Filter.fromText);
var subscriptions = [
"https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
"https://easylist-downloads.adblockplus.org/exceptionrules.txt",
"https://easylist-downloads.adblockplus.org/fanboy-social.txt",
"~user~786254"
];
var knownSubscriptions = Object.create(null);
for (var subscriptionUrl of subscriptions)
knownSubscriptions[subscriptionUrl] = modules.subscriptionClasses.Subscription.fromURL(subscriptionUrl);
var customSubscription = knownSubscriptions["~user~786254"];
var issues = {seenDataCorruption: false, filterlistsReinitialized: false};
updateFromURL(issues);
global.seenDataCorruption = issues.seenDataCorruption;
global.filterlistsReinitialized = issues.filterlistsReinitialized;
var events = {addSubscription: false};
updateFromURL(events);
if (events.addSubscription)
{
// We don't know how long it will take for the page to fully load
// so we'll post the message after one second
setTimeout(function()
{
window.postMessage({
type: "message",
payload: {
title: "Custom subscription",
url: "http://example.com/custom.txt",
type: "add-subscription"
}
}, "*");
}, 1000);
}
})(this);
......@@ -76,8 +76,12 @@
global.ext.PageMap = PageMap;
global.ext.showOptions = function()
global.ext.showOptions = function(callback)
{
window.open("http://example.com/options.html", "_blank");
if (top.location.href.indexOf("options.html") == -1)
window.open("options.html", "_blank");
if (callback)
callback();
};
})(this);
......@@ -20,11 +20,15 @@
if (!global.ext)
global.ext = require("ext_background");
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 BlockingFilter = require("filterClasses").BlockingFilter;
var filterClasses = require("filterClasses");
var Filter = filterClasses.Filter;
var BlockingFilter = filterClasses.BlockingFilter;
var Synchronizer = require("synchronizer").Synchronizer;
var subscriptionClasses = require("subscriptionClasses");
......@@ -32,15 +36,18 @@
var DownloadableSubscription = subscriptionClasses.DownloadableSubscription;
var SpecialSubscription = subscriptionClasses.SpecialSubscription;
var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"];
function convertSubscription(subscription)
function convertObject(keys, obj)
{
var result = {};
for (var i = 0; i < subscriptionKeys.length; i++)
result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]]
for (var i = 0; i < keys.length; i++)
result[keys[i]] = obj[keys[i]];
return result;
}
var convertSubscription = convertObject.bind(null, ["disabled",
"downloadStatus", "homepage", "lastSuccess", "title", "url"]);
var convertFilter = convertObject.bind(null, ["text"]);
var changeListeners = null;
var messageTypes = {
"app": "app.listen",
......@@ -50,6 +57,9 @@
function onFilterChange(action)
{
if (action == "load")
action = "filter.loaded";
var parts = action.split(".", 2);
var type;
if (parts.length == 1)
......@@ -70,6 +80,8 @@
{
if (arg instanceof Subscription)
return convertSubscription(arg);
else if (arg instanceof Filter)
return convertFilter(arg);
else
return arg;
});
......@@ -91,8 +103,37 @@
global.ext.onMessage.addListener(function(message, sender, callback)
{
var listenerFilters = null;
switch (message.type)
{
case "app.listen":
case "filters.listen":
case "subscriptions.listen":
if (!changeListeners)
{
changeListeners = new global.ext.PageMap();
FilterNotifier.addListener(onFilterChange);
}
listenerFilters = changeListeners.get(sender.page);
if (!listenerFilters)
{
listenerFilters = Object.create(null);
changeListeners.set(sender.page, listenerFilters);
}
break;
}
switch (message.type)
{
case "add-subscription":
ext.showOptions(function()
{
var subscription = Subscription.fromURL(message.url);
subscription.title = message.title;
onFilterChange("addSubscription", subscription);
});
break;
case "app.get":
if (message.what == "issues")
{
......@@ -118,13 +159,80 @@
callback({locale: Utils.appLocale, bidiDir: bidiDir});
}
else if (message.what == "addonVersion")
{
callback(require("info").addonVersion);
}
else
callback(null);
break;
case "app.listen":
if (message.filter)
listenerFilters.app = message.filter;
else
delete listenerFilters.app;
break;
case "app.open":
if (message.what == "options")
ext.showOptions();
break;
case "filters.add":
var filter = Filter.fromText(message.text);
FilterStorage.addFilter(filter);
break;
case "filters.blocked":
var filter = defaultMatcher.matchesAny(message.url, message.requestType,
message.docDomain, message.thirdParty);
callback(filter instanceof BlockingFilter);
break;
case "filters.get":
var subscription = Subscription.fromURL(message.subscriptionUrl);
if (!subscription)
{
callback([]);
break;
}
callback(subscription.filters.map(convertFilter));
break;
case "filters.listen":
if (message.filter)
listenerFilters.filter = message.filter;
else
delete listenerFilters.filter;
break;
case "filters.remove":
var filter = Filter.fromText(message.text);
var subscription = null;
if (message.subscriptionUrl)
subscription = Subscription.fromURL(message.subscriptionUrl);
if (!subscription)
FilterStorage.removeFilter(filter);
else
FilterStorage.removeFilter(filter, subscription, message.index);
break;
case "prefs.get":
callback(Prefs[message.key]);
break;
case "subscriptions.add":
if (message.url in FilterStorage.knownSubscriptions)
return;
var subscription = Subscription.fromURL(message.url);
if (!subscription)
return;
subscription.disabled = false;
if ("title" in message)
subscription.title = message.title;
if ("homepage" in message)
subscription.homepage = message.homepage;
FilterStorage.addSubscription(subscription);
if (subscription instanceof DownloadableSubscription && !subscription.lastDownload)
Synchronizer.execute(subscription);
break;
case "subscriptions.get":
var subscriptions = FilterStorage.subscriptions.filter(function(s)
{
......@@ -138,9 +246,16 @@
});
callback(subscriptions.map(convertSubscription));
break;
case "filters.blocked":
var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty);
callback(filter instanceof BlockingFilter);
case "subscriptions.listen":
if (message.filter)
listenerFilters.subscription = message.filter;
else
delete listenerFilters.subscription;
break;
case "subscriptions.remove":
var subscription = Subscription.fromURL(message.url);
if (subscription.url in FilterStorage.knownSubscriptions)
FilterStorage.removeSubscription(subscription);
break;
case "subscriptions.toggle":
var subscription = Subscription.fromURL(message.url);
......@@ -156,25 +271,6 @@
Synchronizer.execute(subscription);
}
break;
case "subscriptions.listen":
if (!changeListeners)
{
changeListeners = new global.ext.PageMap();
FilterNotifier.addListener(onFilterChange);
}
var filters = changeListeners.get(sender.page);
if (!filters)
{
filters = Object.create(null);
changeListeners.set(sender.page, filters);
}
if (message.filter)
filters.subscription = message.filter;
else
delete filters.subscription;
break;
}
});
})(this);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment