OLD | NEW |
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 Loading... |
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 init: function() |
| 40 { |
| 41 WhitelistFilter = require("filterClasses").WhitelistFilter; |
| 42 RegExpFilter = require("filterClasses").RegExpFilter; |
| 43 }, |
| 44 |
39 defaultMatcher: | 45 defaultMatcher: |
40 { | 46 { |
41 _rules: {}, | 47 _rules: {}, |
| 48 _domainExceptions: {}, |
| 49 _domainExceptionsTimeout: null, |
42 | 50 |
43 /** | 51 /** |
44 * Converts rule text from Adblock Plus format (implicit * at beginning | 52 * 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 | 53 * and end of the rule unless there is an anchor) to Opera format (* has |
46 * to be specified explicitly). | 54 * to be specified explicitly). |
47 */ | 55 */ |
48 _getRuleText: function(/**Filter*/ filter) /**String*/ | 56 _getRuleText: function(/**Filter*/ filter) /**String*/ |
49 { | 57 { |
50 var text = filter.regexpSource; | 58 var text = filter.regexpSource; |
51 | 59 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 /** | 140 /** |
133 * Converts an Adblock Plus filter to a rule that can be processed by | 141 * Converts an Adblock Plus filter to a rule that can be processed by |
134 * Opera. The following options will be set: | 142 * Opera. The following options will be set: |
135 * | 143 * |
136 * type: urlfilter to be used, either "allow" or "block" | 144 * type: urlfilter to be used, either "allow" or "block" |
137 * text: rule text | 145 * text: rule text |
138 * options: rule options | 146 * options: rule options |
139 */ | 147 */ |
140 _generateRule: function(/**Filter*/ filter) /**Object*/ | 148 _generateRule: function(/**Filter*/ filter) /**Object*/ |
141 { | 149 { |
142 if (!WhitelistFilter) | |
143 { | |
144 WhitelistFilter = require("filterClasses").WhitelistFilter; | |
145 RegExpFilter = require("filterClasses").RegExpFilter; | |
146 } | |
147 | |
148 var rule = { | 150 var rule = { |
149 type: filter instanceof WhitelistFilter ? "allow" : "block", | 151 type: filter instanceof WhitelistFilter ? "allow" : "block", |
150 text: this._getRuleText(filter), | 152 text: this._getRuleText(filter), |
151 options: { | 153 options: { |
152 resources: this._getRuleTypes(filter) | 154 resources: this._getRuleTypes(filter) |
153 } | 155 } |
154 }; | 156 }; |
155 this._getRuleDomains(filter, rule.options); | 157 this._getRuleDomains(filter, rule.options); |
156 this._getRuleThirdParty(filter, rule.options); | 158 this._getRuleThirdParty(filter, rule.options); |
157 return rule; | 159 return rule; |
158 }, | 160 }, |
159 | 161 |
| 162 /** |
| 163 * Checks whether the filter is a domain exception and adds/removes it |
| 164 * according to the add parameter. Returns true if a domain exception |
| 165 * has been processed, false otherwise. |
| 166 */ |
| 167 _processDomainException: function(/**Filter*/ filter, /**Boolean*/ add) /*
*Boolean*/ |
| 168 { |
| 169 if (!(filter instanceof WhitelistFilter) || |
| 170 filter.contentType != RegExpFilter.typeMap.DOCUMENT || |
| 171 filter.domainSource) |
| 172 { |
| 173 return false; |
| 174 } |
| 175 |
| 176 var match = /^\|\|([^\/]+)\^$/.exec(filter.regexpSource); |
| 177 if (!match) |
| 178 return false; |
| 179 |
| 180 var domain = match[1]; |
| 181 if (add) |
| 182 this._domainExceptions[domain] = true; |
| 183 else |
| 184 delete this._domainExceptions[domain]; |
| 185 |
| 186 this._updateDomainExceptions(); |
| 187 return true; |
| 188 }, |
| 189 |
| 190 /** |
| 191 * Updates domain exceptions rule, execution happens delayed to prevent |
| 192 * multiple subsequent updates. |
| 193 */ |
| 194 _updateDomainExceptions: function() |
| 195 { |
| 196 if (this._domainExceptionsTimeout) |
| 197 window.clearTimeout(this._domainExceptionsTimeout); |
| 198 |
| 199 this._domainExceptionsTimeout = window.setTimeout(function() |
| 200 { |
| 201 this._domainExceptionsTimeout = null; |
| 202 opera.extension.urlfilter.allow.remove("*:*"); |
| 203 |
| 204 var domains = Object.keys(this._domainExceptions); |
| 205 if (domains.length) |
| 206 { |
| 207 opera.extension.urlfilter.allow.add("*:*", { |
| 208 includeDomains: domains |
| 209 }); |
| 210 } |
| 211 }.bind(this), 0); |
| 212 }, |
| 213 |
160 add: function(filter) | 214 add: function(filter) |
161 { | 215 { |
| 216 if (this._processDomainException(filter, true)) |
| 217 return; |
| 218 |
162 if (filter.text in this._rules) | 219 if (filter.text in this._rules) |
163 return; | 220 return; |
164 | 221 |
165 if (!filter.regexpSource) // Regular expressions aren't supported | 222 if (!filter.regexpSource) // Regular expressions aren't supported |
166 return; | 223 return; |
167 | 224 |
168 var rule = this._generateRule(filter); | 225 var rule = this._generateRule(filter); |
169 opera.extension.urlfilter[rule.type].add(rule.text, rule.options); | 226 opera.extension.urlfilter[rule.type].add(rule.text, rule.options); |
170 this._rules[filter.text] = rule; | 227 this._rules[filter.text] = rule; |
171 }, | 228 }, |
172 | 229 |
173 remove: function(filter) | 230 remove: function(filter) |
174 { | 231 { |
| 232 if (this._processDomainException(filter, false)) |
| 233 return; |
| 234 |
175 if (!(filter.text in this._rules)) | 235 if (!(filter.text in this._rules)) |
176 return; | 236 return; |
177 | 237 |
178 var rule = this._rules[filter.text]; | 238 var rule = this._rules[filter.text]; |
179 opera.extension.urlfilter[rule.type].remove(rule.text); | 239 opera.extension.urlfilter[rule.type].remove(rule.text); |
180 delete this._rules[filter.text]; | 240 delete this._rules[filter.text]; |
181 }, | 241 }, |
182 | 242 |
183 clear: function() | 243 clear: function() |
184 { | 244 { |
185 for (var text in this._rules) | 245 for (var text in this._rules) |
186 { | 246 { |
187 var rule = this._rules[text]; | 247 var rule = this._rules[text]; |
188 opera.extension.urlfilter[rule.type].remove(rule.text); | 248 opera.extension.urlfilter[rule.type].remove(rule.text); |
189 } | 249 } |
190 this._rules = {}; | 250 this._rules = {}; |
| 251 |
| 252 this._domainExceptions = {}; |
| 253 this._updateDomainExceptions(); |
191 } | 254 } |
192 } | 255 } |
193 }; | 256 }; |
194 })(); | 257 })(); |
OLD | NEW |