Left: | ||
Right: |
OLD | NEW |
---|---|
1 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/; | 1 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/; |
2 | 2 |
3 function splitSelector(selector) | 3 function splitSelector(selector) |
4 { | 4 { |
5 if (selector.indexOf(",") == -1) | 5 if (selector.indexOf(",") == -1) |
6 return [selector]; | 6 return [selector]; |
7 | 7 |
8 var selectors = []; | 8 var selectors = []; |
9 var start = 0; | 9 var start = 0; |
10 var level = 0; | 10 var level = 0; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 { | 66 { |
67 return new URL(stylesheet.href).origin == this.window.location.origin; | 67 return new URL(stylesheet.href).origin == this.window.location.origin; |
68 } | 68 } |
69 catch (e) | 69 catch (e) |
70 { | 70 { |
71 // Invalid URL, assume that it is first-party. | 71 // Invalid URL, assume that it is first-party. |
72 return true; | 72 return true; |
73 } | 73 } |
74 }, | 74 }, |
75 | 75 |
76 findSelectors: function(stylesheet, selectors, filters) | 76 findSelectors: function(stylesheet, filters) |
77 { | 77 { |
78 // Explicitly ignore third-party stylesheets to ensure consistent behavior | 78 // Explicitly ignore third-party stylesheets to ensure consistent behavior |
79 // between Firefox and Chrome. | 79 // between Firefox and Chrome. |
80 if (!this.isSameOrigin(stylesheet)) | 80 if (!this.isSameOrigin(stylesheet)) |
81 return; | 81 return; |
82 | 82 |
83 var rules = stylesheet.cssRules; | 83 var rules = stylesheet.cssRules; |
84 if (!rules) | 84 if (!rules) |
85 return; | 85 return; |
86 | 86 |
87 for (var i = 0; i < rules.length; i++) | 87 for (var i = 0; i < rules.length; i++) |
88 { | 88 { |
89 var rule = rules[i]; | 89 var rule = rules[i]; |
90 if (rule.type != rule.STYLE_RULE) | 90 if (rule.type != rule.STYLE_RULE) |
91 continue; | 91 continue; |
92 | 92 |
93 var style = this.stringifyStyle(rule.style); | 93 var style = this.stringifyStyle(rule.style); |
94 for (var j = 0; j < this.patterns.length; j++) | 94 for (var j = 0; j < this.patterns.length; j++) |
95 { | 95 { |
96 var pattern = this.patterns[j]; | 96 var pattern = this.patterns[j]; |
97 if (pattern.regexp.test(style)) | 97 if (pattern.regexp.test(style)) |
98 { | 98 { |
99 var subSelectors = splitSelector(rule.selectorText); | 99 var subSelectors = splitSelector(rule.selectorText); |
100 var selectors = [] | |
Sebastian Noack
2017/02/06 14:59:21
Nit: Missing semicolon
wspee
2017/02/07 11:59:41
Done.
| |
101 | |
100 for (var k = 0; k < subSelectors.length; k++) | 102 for (var k = 0; k < subSelectors.length; k++) |
101 selectors.push(pattern.prefix + subSelectors[k] + pattern.suffix); | 103 selectors.push(pattern.prefix + subSelectors[k] + pattern.suffix); |
102 filters[pattern.text] = true; | 104 |
105 if (!(pattern.text in filters)) | |
106 filters[pattern.text] = selectors; | |
Sebastian Noack
2017/02/06 14:59:21
While changing the API here anyway, it might be a
wspee
2017/02/07 11:59:41
Done.
| |
107 else | |
108 filters[pattern.text] = filters[pattern.text].concat(selectors); | |
103 } | 109 } |
104 } | 110 } |
105 } | 111 } |
106 }, | 112 }, |
107 | 113 |
108 addSelectors: function(stylesheets) | 114 addSelectors: function(stylesheets) |
109 { | 115 { |
110 var selectors = []; | |
111 var filters = {}; | 116 var filters = {}; |
112 for (var i = 0; i < stylesheets.length; i++) | 117 for (var i = 0; i < stylesheets.length; i++) |
113 this.findSelectors(stylesheets[i], selectors, filters); | 118 this.findSelectors(stylesheets[i], filters); |
114 this.addSelectorsFunc(selectors, Object.keys(filters)); | 119 this.addSelectorsFunc(filters); |
115 }, | 120 }, |
116 | 121 |
117 onLoad: function(event) | 122 onLoad: function(event) |
118 { | 123 { |
119 var stylesheet = event.target.sheet; | 124 var stylesheet = event.target.sheet; |
120 if (stylesheet) | 125 if (stylesheet) |
121 this.addSelectors([stylesheet]); | 126 this.addSelectors([stylesheet]); |
122 }, | 127 }, |
123 | 128 |
124 load: function(callback) | 129 load: function(callback) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 apply: function() | 162 apply: function() |
158 { | 163 { |
159 if (this.patterns.length > 0) | 164 if (this.patterns.length > 0) |
160 { | 165 { |
161 var document = this.window.document; | 166 var document = this.window.document; |
162 this.addSelectors(document.styleSheets); | 167 this.addSelectors(document.styleSheets); |
163 document.addEventListener("load", this.onLoad.bind(this), true); | 168 document.addEventListener("load", this.onLoad.bind(this), true); |
164 } | 169 } |
165 } | 170 } |
166 }; | 171 }; |
OLD | NEW |