OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview Snippets implementation. | 21 * @fileOverview Snippets implementation. |
22 */ | 22 */ |
23 | 23 |
| 24 const {EventEmitter} = require("./events"); |
24 const {Filter} = require("./filterClasses"); | 25 const {Filter} = require("./filterClasses"); |
25 | 26 |
26 const singleCharacterEscapes = new Map([ | 27 const singleCharacterEscapes = new Map([ |
27 ["n", "\n"], ["r", "\r"], ["t", "\t"] | 28 ["n", "\n"], ["r", "\r"], ["t", "\t"] |
28 ]); | 29 ]); |
29 | 30 |
30 let filters = new Set(); | 31 let filters = new Set(); |
31 | 32 |
32 /** | 33 /** |
33 * Container for snippet filters | 34 * Container for snippet filters |
34 * @class | 35 * @class |
35 */ | 36 */ |
36 let Snippets = { | 37 let Snippets = Object.assign(new EventEmitter(), { |
37 /** | 38 /** |
38 * Removes all known filters | 39 * Removes all known filters |
39 */ | 40 */ |
40 clear() | 41 clear() |
41 { | 42 { |
42 filters.clear(); | 43 filters.clear(); |
| 44 |
| 45 this.emit("snippets.filtersCleared"); |
43 }, | 46 }, |
44 | 47 |
45 /** | 48 /** |
46 * Add a new snippet filter | 49 * Add a new snippet filter |
47 * @param {SnippetFilter} filter | 50 * @param {SnippetFilter} filter |
48 */ | 51 */ |
49 add(filter) | 52 add(filter) |
50 { | 53 { |
51 filters.add(filter.text); | 54 filters.add(filter.text); |
| 55 |
| 56 this.emit("snippets.filterAdded", filter); |
52 }, | 57 }, |
53 | 58 |
54 /** | 59 /** |
55 * Removes a snippet filter | 60 * Removes a snippet filter |
56 * @param {SnippetFilter} filter | 61 * @param {SnippetFilter} filter |
57 */ | 62 */ |
58 remove(filter) | 63 remove(filter) |
59 { | 64 { |
60 filters.delete(filter.text); | 65 filters.delete(filter.text); |
| 66 |
| 67 this.emit("snippets.filterRemoved", filter); |
61 }, | 68 }, |
62 | 69 |
63 /** | 70 /** |
64 * Returns a list of all scripts active on a particular domain | 71 * Returns a list of all scripts active on a particular domain |
65 * @param {string} domain | 72 * @param {string} domain |
66 * @return {string[]} | 73 * @return {string[]} |
67 */ | 74 */ |
68 getScriptsForDomain(domain) | 75 getScriptsForDomain(domain) |
69 { | 76 { |
70 let result = []; | 77 let result = []; |
71 for (let text of filters) | 78 for (let text of filters) |
72 { | 79 { |
73 let filter = Filter.fromText(text); | 80 let filter = Filter.fromText(text); |
74 if (filter.isActiveOnDomain(domain)) | 81 if (filter.isActiveOnDomain(domain)) |
75 result.push(filter.script); | 82 result.push(filter.script); |
76 } | 83 } |
77 return result; | 84 return result; |
78 } | 85 } |
79 }; | 86 }); |
80 | 87 |
81 exports.Snippets = Snippets; | 88 exports.Snippets = Snippets; |
82 | 89 |
83 /** | 90 /** |
84 * Parses a script and returns a list of all its commands and their arguments | 91 * Parses a script and returns a list of all its commands and their arguments |
85 * @param {string} script | 92 * @param {string} script |
86 * @return {Array.<string[]>} | 93 * @return {Array.<string[]>} |
87 */ | 94 */ |
88 function parseScript(script) | 95 function parseScript(script) |
89 { | 96 { |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 let value = imports[name]; | 195 let value = imports[name]; |
189 if (typeof value == "function") | 196 if (typeof value == "function") |
190 value(...args); | 197 value(...args); |
191 } | 198 } |
192 } | 199 } |
193 } | 200 } |
194 `; | 201 `; |
195 } | 202 } |
196 | 203 |
197 exports.compileScript = compileScript; | 204 exports.compileScript = compileScript; |
OLD | NEW |