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

Side by Side Diff: lib/matcher.js

Issue 8483154: Adding ABP core modules to ABP/Opera (Closed)
Patch Set: Created Oct. 9, 2012, 9:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This Source Code is subject to the terms of the Mozilla Public License
3 * version 2.0 (the "License"). You can obtain a copy of the License at
4 * http://mozilla.org/MPL/2.0/.
5 */
6
7 //
8 // This mimicks the API of the Matcher module in ABP/Firefox but users Opera's
9 // URLFilter API.
10 //
11
12 (function()
13 {
14 var WhitelistFilter = null;
15 var RegExpFilter = null;
16 var resourceTypes = [
17 "DOCUMENT", "FONT", "IMAGE", "MEDIA", "OBJECT", "OBJECT_SUBREQUEST",
18 "OTHER", "SCRIPT", "STYLESHEET", "SUBDOCUMENT", "XMLHTTPREQUEST"
19 ];
20
21 require.scopes.matcher =
22 {
23 defaultMatcher:
24 {
25 _rules: {},
26
27 _generateRule: function(filter)
Felix Dahlke 2012/10/10 12:11:22 I'd love it if the code segments below comments ("
28 {
29 if (!WhitelistFilter)
30 {
31 WhitelistFilter = require("filterClasses").WhitelistFilter;
32 RegExpFilter = require("filterClasses").RegExpFilter;
33 }
34
35 var rule = {
36 type: filter instanceof WhitelistFilter ? "allow" : "block",
37 text: filter.regexpSource,
38 options: {}
39 };
40
41 // Convert anchors, Opera requires explicit *
42
43 // |foo => foo
44 // foo => *foo
45 // *foo and ||foo stay unchanged
46 if (rule.text.substr(0, 2) != "||")
47 {
48 if (rule.text.substr(0, 1) == "|")
Felix Dahlke 2012/10/10 12:11:22 This value is used twice, so how about storing it
49 rule.text = rule.text.slice(1);
50 else if (rule.text.substr(0, 1) != "*")
51 rule.text = "*" + rule.text;
52 }
53
54 // foo| => foo
55 // foo => foo*
56 // foo* stays unchanged
57 if (rule.text.substr(-1) == "|")
Felix Dahlke 2012/10/10 12:11:22 As above, how about storing this in a variable las
58 rule.text = rule.text.slice(0, -1);
59 else if (rule.text.substr(-1) != "*")
60 rule.text = rule.text + "*";
61
62 // Translate type options
63 var resources = 0;
64 var urlfilter = opera.extension.urlfilter;
65 for (var i = 0; i < resourceTypes.length; i++)
66 {
67 var type = resourceTypes[i];
68 if (filter.contentType & RegExpFilter.typeMap[type])
69 resources = resources | urlfilter["RESOURCE_" + type];
Felix Dahlke 2012/10/10 12:11:22 How about: resources |= urlfilter["RESOURCE_" + ty
Wladimir Palant 2012/10/11 09:36:26 I thought that JavaScript doesn't support |= but i
70 }
71 rule.options.resources = resources;
72
73 // Translate domain options
74 if (filter.domainSource)
75 {
76 var domains = filter.domainSource.split(filter.domainSeparator);
77 for (var i = 0; i < domains.length; i++)
78 {
79 var domain = domains[i];
80 if (domain == "")
81 continue;
82
83 var type = "includeDomains";
84 if (domain[0] == "~")
85 {
86 type = "excludeDomains";
87 domain = domain.substr(1);
88 }
89 if (!(type in rule.options))
90 rule.options[type] = [];
91 rule.options[type].push(domain);
92 }
93 }
94
95 // Translate third-party option
96 if (filter.thirdParty !== null)
97 rule.options.thirdParty = filter.thirdParty;
98
99 return rule;
100 },
101
102 add: function(filter)
103 {
104 if (filter.text in this._rules)
105 return;
106
107 var rule = this._generateRule(filter);
108 if (filter.text == "http://")
Felix Dahlke 2012/10/10 12:11:22 Why just check for the string "http://" here? Woul
Wladimir Palant 2012/10/11 09:36:26 This is just debugging code I forgot :)
109 Cu.reportError(JSON.stringify(rule));
110 opera.extension.urlfilter[rule.type].add(rule.text, rule.options);
111 this._rules[filter.text] = rule;
112 },
113
114 remove: function(filter)
115 {
116 if (!(filter.text in this._rules))
117 return;
118
119 var rule = this._rules[filter.text];
120 opera.extension.urlfilter[rule.type].remove(rule.text);
121 delete this._rules[filter.text];
122 },
123
124 clear: function(filter)
125 {
126 for (var text in this._rules)
127 {
128 var rule = this._rules[text];
129 opera.extension.urlfilter[rule.type].remove(rule);
Felix Dahlke 2012/10/10 12:11:22 Shouldn't this be "remove(text)"?
130 }
131 this._rules = {};
132 }
133 }
134 };
135 })();
OLDNEW
« lib/adblockplus_compat.js ('K') | « lib/adblockplus_compat.js ('k') | subscriptions.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld