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

Side by Side Diff: lib/matcher.js

Issue 8790183: Added handling of whitelisting rules like @@||example.com^$document (Closed)
Patch Set: Created Nov. 14, 2012, 12:30 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | options.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of the Adblock Plus extension, 2 * This file is part of the Adblock Plus extension,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 12 matching lines...) Expand all
23 (function() 23 (function()
24 { 24 {
25 // Make sure that filters don't apply to extension pages. We shouldn't allow 25 // Make sure that filters don't apply to extension pages. We shouldn't allow
26 // users to break our options page unintentionally, recovering is very hard 26 // users to break our options page unintentionally, recovering is very hard
27 // if they do. 27 // if they do.
28 opera.extension.urlfilter.allow.add("widget://*"); 28 opera.extension.urlfilter.allow.add("widget://*");
29 29
30 var WhitelistFilter = null; 30 var WhitelistFilter = null;
31 var RegExpFilter = null; 31 var RegExpFilter = null;
32 var resourceTypes = [ 32 var resourceTypes = [
33 "DOCUMENT", "FONT", "IMAGE", "MEDIA", "OBJECT", "OBJECT_SUBREQUEST", 33 "FONT", "IMAGE", "MEDIA", "OBJECT", "OBJECT_SUBREQUEST",
34 "OTHER", "SCRIPT", "STYLESHEET", "SUBDOCUMENT", "XMLHTTPREQUEST" 34 "OTHER", "SCRIPT", "STYLESHEET", "SUBDOCUMENT", "XMLHTTPREQUEST"
35 ]; 35 ];
36 36
37 require.scopes.matcher = 37 require.scopes.matcher =
38 { 38 {
39 defaultMatcher: 39 defaultMatcher:
40 { 40 {
41 _rules: {}, 41 _rules: {},
42 _domainExceptions: {},
43 _domainExceptionsTimeout: null,
42 44
43 /** 45 /**
44 * Converts rule text from Adblock Plus format (implicit * at beginning 46 * Converts rule text from Adblock Plus format (implicit * at beginning
45 * and end of the rule unless there is an anchor) to Opera format (* has 47 * and end of the rule unless there is an anchor) to Opera format (* has
46 * to be specified explicitly). 48 * to be specified explicitly).
47 */ 49 */
48 _getRuleText: function(/**Filter*/ filter) /**String*/ 50 _getRuleText: function(/**Filter*/ filter) /**String*/
49 { 51 {
50 var text = filter.regexpSource; 52 var text = filter.regexpSource;
51 53
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 /** 134 /**
133 * Converts an Adblock Plus filter to a rule that can be processed by 135 * Converts an Adblock Plus filter to a rule that can be processed by
134 * Opera. The following options will be set: 136 * Opera. The following options will be set:
135 * 137 *
136 * type: urlfilter to be used, either "allow" or "block" 138 * type: urlfilter to be used, either "allow" or "block"
137 * text: rule text 139 * text: rule text
138 * options: rule options 140 * options: rule options
139 */ 141 */
140 _generateRule: function(/**Filter*/ filter) /**Object*/ 142 _generateRule: function(/**Filter*/ filter) /**Object*/
141 { 143 {
142 if (!WhitelistFilter)
143 {
144 WhitelistFilter = require("filterClasses").WhitelistFilter;
145 RegExpFilter = require("filterClasses").RegExpFilter;
146 }
147
148 var rule = { 144 var rule = {
149 type: filter instanceof WhitelistFilter ? "allow" : "block", 145 type: filter instanceof WhitelistFilter ? "allow" : "block",
150 text: this._getRuleText(filter), 146 text: this._getRuleText(filter),
151 options: { 147 options: {
152 resources: this._getRuleTypes(filter) 148 resources: this._getRuleTypes(filter)
153 } 149 }
154 }; 150 };
155 this._getRuleDomains(filter, rule.options); 151 this._getRuleDomains(filter, rule.options);
156 this._getRuleThirdParty(filter, rule.options); 152 this._getRuleThirdParty(filter, rule.options);
157 return rule; 153 return rule;
158 }, 154 },
159 155
156 /**
157 * Extracts the domain to which the exception applies if any.
158 */
159 _getDomainFromException: function(/**Filter*/ filter) /**String*/
160 {
161 if (filter.domainSource)
162 return null;
163
164 var match = /^\|\|([^\/]+)\^$/.exec(filter.regexpSource);
165 if (!match)
166 return null;
167
168 return match[1];
169 },
170
171 /**
172 * Adds a global exception for a particular document domain.
173 */
174 _addDomainException: function(/**String*/ domain)
175 {
176 this._domainExceptions[domain] = true;
177
178 // Update domain exceptions rule delayed to prevent multiple subsequent
179 // updates.
180 if (this._domainExceptionsTimeout)
181 window.clearTimeout(this._domainExceptionsTimeout);
182 this._domainExceptionsTimeout = window.setTimeout(this._updateDomainExce ptions.bind(this), 0);
183 },
184
185 /**
186 * Removes a global exception for a particular document domain.
187 */
188 _removeDomainException: function(/**String*/ domain)
189 {
190 delete this._domainExceptions[domain];
191
192 // Update domain exceptions rule delayed to prevent multiple subsequent
193 // updates.
194 if (this._domainExceptionsTimeout)
195 window.clearTimeout(this._domainExceptionsTimeout);
196 this._domainExceptionsTimeout = window.setTimeout(this._updateDomainExce ptions.bind(this), 0);
197 },
198
199 /**
200 * Updates domain exceptions rule (should be called delayed).
201 */
202 _updateDomainExceptions: function()
203 {
204 this._domainExceptionsTimeout = null;
205 opera.extension.urlfilter.allow.remove("*:*");
206
207 var domains = Object.keys(this._domainExceptions);
208 if (domains.length)
209 {
210 opera.extension.urlfilter.allow.add("*:*", {
211 includeDomains: domains
212 });
213 }
214 },
215
160 add: function(filter) 216 add: function(filter)
161 { 217 {
218 if (!WhitelistFilter)
Felix Dahlke 2012/11/14 13:03:51 Not a new problem, but can't we do this earlier? B
Wladimir Palant 2012/11/14 13:46:53 Yes, it's ugly. Problem is that matcher.js needs t
219 {
220 WhitelistFilter = require("filterClasses").WhitelistFilter;
221 RegExpFilter = require("filterClasses").RegExpFilter;
222 }
223
224 if (filter instanceof WhitelistFilter && filter.contentType == RegExpFil ter.typeMap.DOCUMENT)
Felix Dahlke 2012/11/14 13:03:51 I'd like a function for both this check and the _g
Wladimir Palant 2012/11/14 13:46:53 Ok, I changed the solution to have maximal code re
225 {
226 var domain = this._getDomainFromException(filter);
227 if (domain)
228 this._addDomainException(domain);
229 }
230
162 if (filter.text in this._rules) 231 if (filter.text in this._rules)
163 return; 232 return;
164 233
165 if (!filter.regexpSource) // Regular expressions aren't supported 234 if (!filter.regexpSource) // Regular expressions aren't supported
166 return; 235 return;
167 236
168 var rule = this._generateRule(filter); 237 var rule = this._generateRule(filter);
169 opera.extension.urlfilter[rule.type].add(rule.text, rule.options); 238 opera.extension.urlfilter[rule.type].add(rule.text, rule.options);
170 this._rules[filter.text] = rule; 239 this._rules[filter.text] = rule;
171 }, 240 },
172 241
173 remove: function(filter) 242 remove: function(filter)
174 { 243 {
244 if (filter instanceof WhitelistFilter && filter.contentType == RegExpFil ter.typeMap.DOCUMENT)
245 {
246 var domain = this._getDomainFromException(filter);
247 if (domain)
248 this._removeDomainException(domain);
249 }
250
175 if (!(filter.text in this._rules)) 251 if (!(filter.text in this._rules))
176 return; 252 return;
177 253
178 var rule = this._rules[filter.text]; 254 var rule = this._rules[filter.text];
179 opera.extension.urlfilter[rule.type].remove(rule.text); 255 opera.extension.urlfilter[rule.type].remove(rule.text);
180 delete this._rules[filter.text]; 256 delete this._rules[filter.text];
181 }, 257 },
182 258
183 clear: function() 259 clear: function()
184 { 260 {
185 for (var text in this._rules) 261 for (var text in this._rules)
186 { 262 {
187 var rule = this._rules[text]; 263 var rule = this._rules[text];
188 opera.extension.urlfilter[rule.type].remove(rule.text); 264 opera.extension.urlfilter[rule.type].remove(rule.text);
189 } 265 }
190 this._rules = {}; 266 this._rules = {};
267
268 for (var domain in this._domainExceptions)
269 this._removeDomainException(domain);
270 this._domainExceptions = {};
191 } 271 }
192 } 272 }
193 }; 273 };
194 })(); 274 })();
OLDNEW
« no previous file with comments | « no previous file | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld