forked from adblockplus/adblockplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevtools-panel.js
360 lines (307 loc) · 9.48 KB
/
devtools-panel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
const {getMessage} = browser.i18n;
const promisedPlatform = browser.runtime.sendMessage({
type: "app.get",
what: "platform"
});
const maxTitleLength = 1000;
let lastFilterQuery = null;
browser.runtime.sendMessage({type: "types.get"})
.then(filterTypes =>
{
const filterTypesElem = document.getElementById("filter-type");
const filterStyleElem = document.createElement("style");
for (const type of filterTypes)
{
filterStyleElem.innerHTML +=
`#items[data-filter-type=${type}] tr:not([data-type=${type}])` +
"{display: none;}";
const optionNode = document.createElement("option");
optionNode.appendChild(document.createTextNode(type));
filterTypesElem.appendChild(optionNode);
}
document.body.appendChild(filterStyleElem);
});
function generateFilter(request, domainSpecific)
{
let filter = request.url.replace(/^[\w-]+:\/+(?:www\.)?/, "||");
const options = [];
if (request.type == "POPUP")
{
options.push("popup");
if (request.url == "about:blank")
domainSpecific = true;
}
if (request.type == "CSP")
options.push("csp");
if (domainSpecific)
options.push("domain=" + request.docDomain);
if (options.length > 0)
filter += "$" + options.join(",");
return filter;
}
function createActionButton(action, stringId, filter)
{
const button = document.createElement("span");
button.textContent = getMessage(stringId);
button.classList.add("action");
button.addEventListener("click", () =>
{
browser.runtime.sendMessage({
type: "filters." + action,
text: filter
});
}, false);
return button;
}
function onUrlClick(event)
{
if (event.button != 0)
return;
// Firefox doesn't support the openResource API yet
if (!("openResource" in browser.devtools.panels))
return;
browser.devtools.panels.openResource(event.target.href);
event.preventDefault();
}
function getTitleText(str)
{
return promisedPlatform
.then((platform) =>
{
// Firefox doesn't wrap tooltip strings without whitespace characters
// so we have to split up the string into individual lines
// https://bugzilla.mozilla.org/show_bug.cgi?id=805039
if (platform === "gecko")
{
const maxLineCount = maxTitleLength / 50;
let lines = str.match(/.{1,50}/g);
if (lines.length > maxLineCount)
{
// Text is too long to display in full so we cut out the middle part
lines = [
...lines.slice(0, maxLineCount / 2),
"…",
...lines.slice(-(maxLineCount / 2))
];
}
return lines.join("\n");
}
if (str.length < maxTitleLength + 3)
return str;
// Text is too long to display in full so we cut out the middle part
return [
str.slice(0, maxTitleLength / 2),
"…",
str.slice(-(maxTitleLength / 2))
].join("\n");
});
}
function createRecord(request, filter, template)
{
const row = document.importNode(template, true);
row.dataset.type = request.type;
row.querySelector(".domain").textContent = request.docDomain;
row.querySelector(".type").textContent = request.type;
const urlElement = row.querySelector(".url");
const actionWrapper = row.querySelector(".action-wrapper");
if (request.url)
{
ext.i18n.setElementText(
urlElement, "devtools_request_url",
[request.url, request.rewrittenUrl]
);
const originalUrl = urlElement.querySelector("[data-i18n-index='0']");
originalUrl.classList.add("url");
getTitleText(request.url).then((title) =>
{
originalUrl.setAttribute("title", title);
});
originalUrl.setAttribute("href", request.url);
originalUrl.setAttribute("target", "_blank");
if (request.type != "POPUP")
{
originalUrl.addEventListener("click", onUrlClick);
}
if (request.rewrittenUrl)
{
const rewrittenUrl = urlElement.querySelector("[data-i18n-index='1'");
rewrittenUrl.classList.add("url-rewritten");
getTitleText(request.rewrittenUrl).then((title) =>
{
rewrittenUrl.setAttribute("title", title);
});
rewrittenUrl.setAttribute("href", request.rewrittenUrl);
rewrittenUrl.setAttribute("target", "_blank");
rewrittenUrl.addEventListener("click", onUrlClick);
}
else
{
urlElement.innerHTML = "";
urlElement.appendChild(originalUrl);
}
}
else
{
urlElement.innerHTML = " ";
}
if (filter)
{
const filterElement = row.querySelector(".filter");
const originElement = row.querySelector(".origin");
getTitleText(filter.text).then((title) =>
{
filterElement.setAttribute("title", title);
});
filterElement.textContent = filter.text;
row.dataset.state = filter.whitelisted ? "whitelisted" : "blocked";
if (filter.subscription)
originElement.textContent = filter.subscription;
else
{
if (filter.userDefined)
originElement.textContent = getMessage("devtools_filter_origin_custom");
else
originElement.textContent = getMessage("devtools_filter_origin_none");
originElement.classList.add("unnamed");
}
if (!filter.whitelisted && request.type != "ELEMHIDE")
{
actionWrapper.appendChild(createActionButton(
"add", "devtools_action_unblock", "@@" + generateFilter(request, false)
));
}
if (filter.userDefined)
{
actionWrapper.appendChild(createActionButton(
"remove", "devtools_action_remove", filter.text
));
}
}
else
{
actionWrapper.appendChild(createActionButton(
"add", "devtools_action_block",
generateFilter(request, request.specificOnly)
));
}
if (lastFilterQuery && shouldFilterRow(row, lastFilterQuery))
row.classList.add("filtered-by-search");
return row;
}
function shouldFilterRow(row, query)
{
const elementsToSearch = [
row.getElementsByClassName("filter"),
row.getElementsByClassName("origin"),
row.getElementsByClassName("type"),
row.getElementsByClassName("url")
];
for (const elements of elementsToSearch)
{
for (const element of elements)
{
if (element.innerText.search(query) != -1)
return false;
}
}
return true;
}
function performSearch(table, query)
{
for (const row of table.rows)
{
if (shouldFilterRow(row, query))
row.classList.add("filtered-by-search");
else
row.classList.remove("filtered-by-search");
}
}
function cancelSearch(table)
{
for (const row of table.rows)
row.classList.remove("filtered-by-search");
}
document.addEventListener("DOMContentLoaded", () =>
{
const container = document.getElementById("items");
const table = container.querySelector("tbody");
const template = document.querySelector("template").content.firstElementChild;
document.querySelector("[data-i18n='devtools_footer'] > a")
.addEventListener("click", () =>
{
ext.devtools.inspectedWindow.reload();
}, false);
document.getElementById("filter-state").addEventListener("change", (event) =>
{
container.dataset.filterState = event.target.value;
}, false);
document.getElementById("filter-type").addEventListener("change", (event) =>
{
container.dataset.filterType = event.target.value;
}, false);
ext.onMessage.addListener((message) =>
{
switch (message.type)
{
case "add-record":
table.appendChild(createRecord(message.request, message.filter,
template));
break;
case "update-record":
const oldRow = table.getElementsByTagName("tr")[message.index];
const newRow = createRecord(message.request, message.filter, template);
oldRow.parentNode.replaceChild(newRow, oldRow);
if (!message.initialize)
{
newRow.classList.add("changed");
container.classList.add("has-changes");
}
break;
case "remove-record":
const row = table.getElementsByTagName("tr")[message.index];
row.parentNode.removeChild(row);
container.classList.add("has-changes");
break;
case "reset":
table.innerHTML = "";
container.classList.remove("has-changes");
break;
}
});
window.addEventListener("message", (event) =>
{
switch (event.data.type)
{
case "performSearch":
performSearch(table, event.data.queryString);
lastFilterQuery = event.data.queryString;
break;
case "cancelSearch":
cancelSearch(table);
lastFilterQuery = null;
break;
}
});
// Since Chrome 54 the themeName is accessible, for earlier versions we must
// assume the default theme is being used.
// https://bugs.chromium.org/p/chromium/issues/detail?id=608869
const theme = ext.devtools.panels.themeName || "default";
document.body.classList.add(theme);
}, false);