OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | |
5 /** | 4 /** |
6 * @constructor | 5 * @unrestricted |
7 * @extends {WebInspector.VBox} | |
8 */ | 6 */ |
9 WebInspector.BlockedURLsPane = function() | 7 WebInspector.BlockedURLsPane = class extends WebInspector.VBox { |
10 { | 8 constructor() { |
11 WebInspector.VBox.call(this, true); | 9 super(true); |
12 this.registerRequiredCSS("network/blockedURLsPane.css"); | 10 this.registerRequiredCSS('network/blockedURLsPane.css'); |
13 this.contentElement.classList.add("blocked-urls-pane"); | 11 this.contentElement.classList.add('blocked-urls-pane'); |
14 | 12 |
15 WebInspector.BlockedURLsPane._instance = this; | 13 WebInspector.BlockedURLsPane._instance = this; |
16 | 14 |
17 this._blockedURLsSetting = WebInspector.moduleSetting("blockedURLs"); | 15 this._blockedURLsSetting = WebInspector.moduleSetting('blockedURLs'); |
18 this._blockedURLsSetting.addChangeListener(this._update, this); | 16 this._blockedURLsSetting.addChangeListener(this._update, this); |
19 | 17 |
20 this._toolbar = new WebInspector.Toolbar("", this.contentElement); | 18 this._toolbar = new WebInspector.Toolbar('', this.contentElement); |
21 this._toolbar.element.addEventListener("click", (e) => e.consume()); | 19 this._toolbar.element.addEventListener('click', (e) => e.consume()); |
22 var addButton = new WebInspector.ToolbarButton(WebInspector.UIString("Add pa
ttern"), "add-toolbar-item"); | 20 var addButton = new WebInspector.ToolbarButton(WebInspector.UIString('Add pa
ttern'), 'add-toolbar-item'); |
23 addButton.addEventListener("click", this._addButtonClicked.bind(this)); | 21 addButton.addEventListener('click', this._addButtonClicked.bind(this)); |
24 this._toolbar.appendToolbarItem(addButton); | 22 this._toolbar.appendToolbarItem(addButton); |
25 var clearButton = new WebInspector.ToolbarButton(WebInspector.UIString("Remo
ve all"), "clear-toolbar-item"); | 23 var clearButton = new WebInspector.ToolbarButton(WebInspector.UIString('Remo
ve all'), 'clear-toolbar-item'); |
26 clearButton.addEventListener("click", this._removeAll.bind(this)); | 24 clearButton.addEventListener('click', this._removeAll.bind(this)); |
27 this._toolbar.appendToolbarItem(clearButton); | 25 this._toolbar.appendToolbarItem(clearButton); |
28 | 26 |
29 this._emptyElement = this.contentElement.createChild("div", "no-blocked-urls
"); | 27 this._emptyElement = this.contentElement.createChild('div', 'no-blocked-urls
'); |
30 this._emptyElement.createChild("span").textContent = WebInspector.UIString("
Requests are not blocked. "); | 28 this._emptyElement.createChild('span').textContent = WebInspector.UIString('
Requests are not blocked. '); |
31 var addLink = this._emptyElement.createChild("span", "link"); | 29 var addLink = this._emptyElement.createChild('span', 'link'); |
32 addLink.textContent = WebInspector.UIString("Add pattern."); | 30 addLink.textContent = WebInspector.UIString('Add pattern.'); |
33 addLink.href = ""; | 31 addLink.href = ''; |
34 addLink.addEventListener("click", this._addButtonClicked.bind(this), false); | 32 addLink.addEventListener('click', this._addButtonClicked.bind(this), false); |
35 this._emptyElement.addEventListener("contextmenu", this._emptyElementContext
Menu.bind(this), true); | 33 this._emptyElement.addEventListener('contextmenu', this._emptyElementContext
Menu.bind(this), true); |
36 | 34 |
37 this._listElement = this.contentElement.createChild("div", "blocked-urls-lis
t"); | 35 this._listElement = this.contentElement.createChild('div', 'blocked-urls-lis
t'); |
38 | 36 |
39 /** @type {!Map<string, number>} */ | 37 /** @type {!Map<string, number>} */ |
40 this._blockedCountForUrl = new Map(); | 38 this._blockedCountForUrl = new Map(); |
41 WebInspector.targetManager.addModelListener(WebInspector.NetworkManager, Web
Inspector.NetworkManager.Events.RequestFinished, this._onRequestFinished, this); | 39 WebInspector.targetManager.addModelListener( |
| 40 WebInspector.NetworkManager, WebInspector.NetworkManager.Events.RequestF
inished, this._onRequestFinished, this); |
42 | 41 |
43 this._updateThrottler = new WebInspector.Throttler(200); | 42 this._updateThrottler = new WebInspector.Throttler(200); |
44 | 43 |
45 this._update(); | 44 this._update(); |
46 }; | 45 } |
47 | 46 |
48 WebInspector.BlockedURLsPane.prototype = { | 47 static reset() { |
| 48 if (WebInspector.BlockedURLsPane._instance) |
| 49 WebInspector.BlockedURLsPane._instance.reset(); |
| 50 } |
| 51 |
| 52 /** |
| 53 * @param {!Event} event |
| 54 */ |
| 55 _emptyElementContextMenu(event) { |
| 56 var contextMenu = new WebInspector.ContextMenu(event); |
| 57 contextMenu.appendItem(WebInspector.UIString.capitalize('Add ^pattern'), thi
s._addButtonClicked.bind(this)); |
| 58 contextMenu.show(); |
| 59 } |
| 60 |
| 61 _addButtonClicked() { |
| 62 this._emptyElement.classList.add('hidden'); |
| 63 var element = this._createElement('', this._blockedURLsSetting.get().length)
; |
| 64 this._listElement.appendChild(element); |
| 65 element.scrollIntoViewIfNeeded(); |
| 66 this._edit('', element, this._addBlockedURL.bind(this)); |
| 67 } |
| 68 |
| 69 /** |
| 70 * @param {string} content |
| 71 * @param {!Element} element |
| 72 * @param {function(string)} onAccept |
| 73 * @private |
| 74 */ |
| 75 _edit(content, element, onAccept) { |
| 76 this._editing = true; |
| 77 |
| 78 element.classList.add('blocked-url-editing'); |
| 79 var input = element.createChild('input'); |
| 80 input.setAttribute('type', 'text'); |
| 81 input.value = content; |
| 82 input.placeholder = WebInspector.UIString('Text pattern to block matching re
quests; use * for wildcard'); |
| 83 input.addEventListener('blur', commit.bind(this), false); |
| 84 input.addEventListener('keydown', keydown.bind(this), false); |
| 85 input.focus(); |
| 86 |
49 /** | 87 /** |
| 88 * @this {WebInspector.BlockedURLsPane} |
| 89 */ |
| 90 function finish() { |
| 91 this._editing = false; |
| 92 element.removeChild(input); |
| 93 element.classList.remove('blocked-url-editing'); |
| 94 } |
| 95 |
| 96 /** |
| 97 * @this {WebInspector.BlockedURLsPane} |
| 98 */ |
| 99 function commit() { |
| 100 if (!this._editing) |
| 101 return; |
| 102 var text = input.value.trim(); |
| 103 finish.call(this); |
| 104 if (text) |
| 105 onAccept(text); |
| 106 else |
| 107 this._update(); |
| 108 } |
| 109 |
| 110 /** |
| 111 * @this {WebInspector.BlockedURLsPane} |
50 * @param {!Event} event | 112 * @param {!Event} event |
51 */ | 113 */ |
52 _emptyElementContextMenu: function(event) | 114 function keydown(event) { |
53 { | 115 if (isEnterKey(event)) { |
54 var contextMenu = new WebInspector.ContextMenu(event); | 116 event.consume(); |
55 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^pattern"),
this._addButtonClicked.bind(this)); | 117 commit.call(this); |
56 contextMenu.show(); | 118 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code |
| event.key === 'Escape') { |
57 }, | 119 event.consume(); |
58 | 120 finish.call(this); |
59 _addButtonClicked: function() | 121 this._update(); |
60 { | 122 } |
61 this._emptyElement.classList.add("hidden"); | 123 } |
62 var element = this._createElement("", this._blockedURLsSetting.get().len
gth); | 124 } |
63 this._listElement.appendChild(element); | 125 |
64 element.scrollIntoViewIfNeeded(); | 126 /** |
65 this._edit("", element, this._addBlockedURL.bind(this)); | 127 * @param {string} url |
66 }, | 128 */ |
67 | 129 _addBlockedURL(url) { |
68 /** | 130 var blocked = this._blockedURLsSetting.get(); |
69 * @param {string} content | 131 blocked.push(url); |
70 * @param {!Element} element | 132 this._blockedURLsSetting.set(blocked); |
71 * @param {function(string)} onAccept | 133 } |
72 * @private | 134 |
73 */ | 135 /** |
74 _edit: function(content, element, onAccept) | 136 * @param {number} index |
75 { | 137 */ |
76 this._editing = true; | 138 _removeBlockedURL(index) { |
77 | 139 var blocked = this._blockedURLsSetting.get(); |
78 element.classList.add("blocked-url-editing"); | 140 blocked.splice(index, 1); |
79 var input = element.createChild("input"); | 141 this._blockedURLsSetting.set(blocked); |
80 input.setAttribute("type", "text"); | 142 } |
81 input.value = content; | 143 |
82 input.placeholder = WebInspector.UIString("Text pattern to block matchin
g requests; use * for wildcard"); | 144 /** |
83 input.addEventListener("blur", commit.bind(this), false); | 145 * @param {number} index |
84 input.addEventListener("keydown", keydown.bind(this), false); | 146 * @param {string} url |
85 input.focus(); | 147 */ |
86 | 148 _changeBlockedURL(index, url) { |
87 /** | 149 var blocked = this._blockedURLsSetting.get(); |
88 * @this {WebInspector.BlockedURLsPane} | 150 blocked.splice(index, 1, url); |
89 */ | 151 this._blockedURLsSetting.set(blocked); |
90 function finish() | 152 } |
91 { | 153 |
92 this._editing = false; | 154 _removeAll() { |
93 element.removeChild(input); | 155 this._blockedURLsSetting.set([]); |
94 element.classList.remove("blocked-url-editing"); | 156 } |
95 } | 157 |
96 | 158 /** |
97 /** | 159 * @param {number} index |
98 * @this {WebInspector.BlockedURLsPane} | 160 * @param {!Event} event |
99 */ | 161 */ |
100 function commit() | 162 _contextMenu(index, event) { |
101 { | 163 var contextMenu = new WebInspector.ContextMenu(event); |
102 if (!this._editing) | 164 contextMenu.appendItem(WebInspector.UIString.capitalize('Add ^pattern'), thi
s._addButtonClicked.bind(this)); |
103 return; | 165 contextMenu.appendItem( |
104 var text = input.value.trim(); | 166 WebInspector.UIString.capitalize('Remove ^pattern'), this._removeBlocked
URL.bind(this, index)); |
105 finish.call(this); | 167 contextMenu.appendItem(WebInspector.UIString.capitalize('Remove ^all'), this
._removeAll.bind(this)); |
106 if (text) | 168 contextMenu.show(); |
107 onAccept(text); | 169 } |
108 else | 170 |
109 this._update(); | 171 /** |
110 } | 172 * @return {!Promise<?>} |
111 | 173 */ |
112 /** | 174 _update() { |
113 * @this {WebInspector.BlockedURLsPane} | 175 if (this._editing) |
114 * @param {!Event} event | 176 return Promise.resolve(); |
115 */ | 177 |
116 function keydown(event) | 178 this._listElement.removeChildren(); |
117 { | 179 var blocked = this._blockedURLsSetting.get(); |
118 if (isEnterKey(event)) { | 180 for (var index = 0; index < blocked.length; index++) |
119 event.consume(); | 181 this._listElement.appendChild(this._createElement(blocked[index], index)); |
120 commit.call(this); | 182 |
121 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.
code || event.key === "Escape") { | 183 this._emptyElement.classList.toggle('hidden', !!blocked.length); |
122 event.consume(); | 184 return Promise.resolve(); |
123 finish.call(this); | 185 } |
124 this._update(); | 186 |
125 } | 187 /** |
126 } | 188 * @param {string} url |
127 }, | 189 * @param {number} index |
128 | 190 * @return {!Element} |
129 /** | 191 */ |
130 * @param {string} url | 192 _createElement(url, index) { |
131 */ | 193 var element = createElementWithClass('div', 'blocked-url'); |
132 _addBlockedURL: function(url) | 194 |
133 { | 195 var label = element.createChild('div', 'blocked-url-text'); |
134 var blocked = this._blockedURLsSetting.get(); | 196 label.textContent = url; |
135 blocked.push(url); | 197 |
136 this._blockedURLsSetting.set(blocked); | 198 var count = this._blockedRequestsCount(url); |
137 }, | 199 var countElement = element.createChild('div', 'blocked-count monospace'); |
138 | 200 countElement.textContent = String.sprintf('[%d]', count); |
139 /** | 201 countElement.title = WebInspector.UIString( |
140 * @param {number} index | 202 count === 1 ? '%d request blocked by this pattern' : '%d requests blocke
d by this pattern', count); |
141 */ | 203 |
142 _removeBlockedURL: function(index) | 204 var removeButton = element.createChild('div', 'remove-button'); |
143 { | 205 removeButton.title = WebInspector.UIString('Remove'); |
144 var blocked = this._blockedURLsSetting.get(); | 206 removeButton.addEventListener('click', this._removeBlockedURL.bind(this, ind
ex), false); |
145 blocked.splice(index, 1); | 207 |
146 this._blockedURLsSetting.set(blocked); | 208 element.addEventListener('contextmenu', this._contextMenu.bind(this, index),
true); |
147 }, | 209 element.addEventListener( |
148 | 210 'dblclick', this._edit.bind(this, url, element, this._changeBlockedURL.b
ind(this, index)), false); |
149 /** | 211 return element; |
150 * @param {number} index | 212 } |
151 * @param {string} url | 213 |
152 */ | 214 /** |
153 _changeBlockedURL: function(index, url) | 215 * @param {string} url |
154 { | 216 * @return {number} |
155 var blocked = this._blockedURLsSetting.get(); | 217 */ |
156 blocked.splice(index, 1, url); | 218 _blockedRequestsCount(url) { |
157 this._blockedURLsSetting.set(blocked); | 219 if (!url) |
158 }, | 220 return 0; |
159 | 221 |
160 _removeAll: function() | 222 var result = 0; |
161 { | 223 for (var blockedUrl of this._blockedCountForUrl.keys()) { |
162 this._blockedURLsSetting.set([]); | 224 if (this._matches(url, blockedUrl)) |
163 }, | 225 result += this._blockedCountForUrl.get(blockedUrl); |
164 | 226 } |
165 /** | 227 return result; |
166 * @param {number} index | 228 } |
167 * @param {!Event} event | 229 |
168 */ | 230 /** |
169 _contextMenu: function(index, event) | 231 * @param {string} pattern |
170 { | 232 * @param {string} url |
171 var contextMenu = new WebInspector.ContextMenu(event); | 233 * @return {boolean} |
172 contextMenu.appendItem(WebInspector.UIString.capitalize("Add ^pattern"),
this._addButtonClicked.bind(this)); | 234 */ |
173 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^pattern
"), this._removeBlockedURL.bind(this, index)); | 235 _matches(pattern, url) { |
174 contextMenu.appendItem(WebInspector.UIString.capitalize("Remove ^all"),
this._removeAll.bind(this)); | 236 var pos = 0; |
175 contextMenu.show(); | 237 var parts = pattern.split('*'); |
176 }, | 238 for (var index = 0; index < parts.length; index++) { |
177 | 239 var part = parts[index]; |
178 /** | 240 if (!part.length) |
179 * @return {!Promise<?>} | 241 continue; |
180 */ | 242 pos = url.indexOf(part, pos); |
181 _update: function() | 243 if (pos === -1) |
182 { | 244 return false; |
183 if (this._editing) | 245 pos += part.length; |
184 return Promise.resolve(); | 246 } |
185 | 247 return true; |
186 this._listElement.removeChildren(); | 248 } |
187 var blocked = this._blockedURLsSetting.get(); | 249 |
188 for (var index = 0; index < blocked.length; index++) | 250 reset() { |
189 this._listElement.appendChild(this._createElement(blocked[index], in
dex)); | 251 this._blockedCountForUrl.clear(); |
190 | 252 } |
191 this._emptyElement.classList.toggle("hidden", !!blocked.length); | 253 |
192 return Promise.resolve(); | 254 /** |
193 }, | 255 * @param {!WebInspector.Event} event |
194 | 256 */ |
195 /** | 257 _onRequestFinished(event) { |
196 * @param {string} url | 258 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data); |
197 * @param {number} index | 259 if (request.wasBlocked()) { |
198 * @return {!Element} | 260 var count = this._blockedCountForUrl.get(request.url) || 0; |
199 */ | 261 this._blockedCountForUrl.set(request.url, count + 1); |
200 _createElement: function(url, index) | 262 this._updateThrottler.schedule(this._update.bind(this)); |
201 { | 263 } |
202 var element = createElementWithClass("div", "blocked-url"); | 264 } |
203 | |
204 var label = element.createChild("div", "blocked-url-text"); | |
205 label.textContent = url; | |
206 | |
207 var count = this._blockedRequestsCount(url); | |
208 var countElement = element.createChild("div", "blocked-count monospace")
; | |
209 countElement.textContent = String.sprintf("[%d]", count); | |
210 countElement.title = WebInspector.UIString(count === 1 ? "%d request blo
cked by this pattern" : "%d requests blocked by this pattern", count); | |
211 | |
212 var removeButton = element.createChild("div", "remove-button"); | |
213 removeButton.title = WebInspector.UIString("Remove"); | |
214 removeButton.addEventListener("click", this._removeBlockedURL.bind(this,
index), false); | |
215 | |
216 element.addEventListener("contextmenu", this._contextMenu.bind(this, ind
ex), true); | |
217 element.addEventListener("dblclick", this._edit.bind(this, url, element,
this._changeBlockedURL.bind(this, index)), false); | |
218 return element; | |
219 }, | |
220 | |
221 /** | |
222 * @param {string} url | |
223 * @return {number} | |
224 */ | |
225 _blockedRequestsCount: function(url) | |
226 { | |
227 if (!url) | |
228 return 0; | |
229 | |
230 var result = 0; | |
231 for (var blockedUrl of this._blockedCountForUrl.keys()) { | |
232 if (this._matches(url, blockedUrl)) | |
233 result += this._blockedCountForUrl.get(blockedUrl); | |
234 } | |
235 return result; | |
236 }, | |
237 | |
238 /** | |
239 * @param {string} pattern | |
240 * @param {string} url | |
241 * @return {boolean} | |
242 */ | |
243 _matches: function(pattern, url) | |
244 { | |
245 var pos = 0; | |
246 var parts = pattern.split("*"); | |
247 for (var index = 0; index < parts.length; index++) { | |
248 var part = parts[index]; | |
249 if (!part.length) | |
250 continue; | |
251 pos = url.indexOf(part, pos); | |
252 if (pos === -1) | |
253 return false; | |
254 pos += part.length; | |
255 } | |
256 return true; | |
257 }, | |
258 | |
259 reset: function() | |
260 { | |
261 this._blockedCountForUrl.clear(); | |
262 }, | |
263 | |
264 /** | |
265 * @param {!WebInspector.Event} event | |
266 */ | |
267 _onRequestFinished: function(event) | |
268 { | |
269 var request = /** @type {!WebInspector.NetworkRequest} */ (event.data); | |
270 if (request.wasBlocked()) { | |
271 var count = this._blockedCountForUrl.get(request.url) || 0; | |
272 this._blockedCountForUrl.set(request.url, count + 1); | |
273 this._updateThrottler.schedule(this._update.bind(this)); | |
274 } | |
275 }, | |
276 | |
277 __proto__: WebInspector.VBox.prototype | |
278 }; | 265 }; |
279 | 266 |
280 | |
281 /** @type {?WebInspector.BlockedURLsPane} */ | 267 /** @type {?WebInspector.BlockedURLsPane} */ |
282 WebInspector.BlockedURLsPane._instance = null; | 268 WebInspector.BlockedURLsPane._instance = null; |
283 | 269 |
284 WebInspector.BlockedURLsPane.reset = function() | 270 |
285 { | 271 /** |
286 if (WebInspector.BlockedURLsPane._instance) | 272 * @implements {WebInspector.ActionDelegate} |
287 WebInspector.BlockedURLsPane._instance.reset(); | 273 * @unrestricted |
| 274 */ |
| 275 WebInspector.BlockedURLsPane.ActionDelegate = class { |
| 276 /** |
| 277 * @override |
| 278 * @param {!WebInspector.Context} context |
| 279 * @param {string} actionId |
| 280 * @return {boolean} |
| 281 */ |
| 282 handleAction(context, actionId) { |
| 283 WebInspector.viewManager.showView('network.blocked-urls'); |
| 284 return true; |
| 285 } |
288 }; | 286 }; |
289 | |
290 /** | |
291 * @constructor | |
292 * @implements {WebInspector.ActionDelegate} | |
293 */ | |
294 WebInspector.BlockedURLsPane.ActionDelegate = function() | |
295 { | |
296 }; | |
297 | |
298 WebInspector.BlockedURLsPane.ActionDelegate.prototype = { | |
299 /** | |
300 * @override | |
301 * @param {!WebInspector.Context} context | |
302 * @param {string} actionId | |
303 * @return {boolean} | |
304 */ | |
305 handleAction: function(context, actionId) | |
306 { | |
307 WebInspector.viewManager.showView("network.blocked-urls"); | |
308 return true; | |
309 } | |
310 }; | |
311 | |
OLD | NEW |