-
Notifications
You must be signed in to change notification settings - Fork 239
/
panel.js
447 lines (415 loc) · 15.4 KB
/
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import { getStackTraceOrigins } from '@duckduckgo/content-scope-scripts/src/utils'
const table = document.querySelector('#request-table')
const clearButton = document.getElementById('clear')
const refreshButton = document.getElementById('refresh')
const protectionButton = document.getElementById('protection')
const tabPicker = document.getElementById('tab-picker')
const tdsOption = document.getElementById('tds')
function sendMessage (messageType, options, callback) {
chrome.runtime.sendMessage({ messageType, options }, callback)
}
/**
* Add a new request row to the panel table without checking for duplicates.
*
* @param {HTMLTableRowElement} row
*/
function addNewRequestRow (row) {
const counter = row.querySelector('.action-count')
panelConfig.currentCounter = counter
panelConfig.lastRowTemplate = row.cloneNode(true)
table.appendChild(row)
}
/**
* Add a new request row to the panel table, checking for duplicates.
*
* @param {HTMLTableRowElement} row
*/
function addRequestRow (row) {
const prevRowTemplate = panelConfig.lastRowTemplate
if (prevRowTemplate) {
// if duplicate request lines would be printed, we instead show a
// counter increment
if (prevRowTemplate.innerHTML === row.innerHTML) {
incrementCurrentRequestCounter()
} else {
addNewRequestRow(row)
}
} else {
addNewRequestRow(row)
}
}
/**
* Increment the counter for the current request type.
*/
function incrementCurrentRequestCounter () {
const counter = panelConfig.currentCounter
const prevCount = parseInt(counter.textContent.replaceAll(/[ [\]]/g, '') || '1')
counter.textContent = ` [${prevCount + 1}]`
}
let tabId = chrome.devtools?.inspectedWindow?.tabId || parseInt(0 + new URL(document.location.href).searchParams.get('tabId'))
// Open the messaging port and re-open if disconnected. The connection will
// disconnect for MV3 builds when the background ServiceWorker becomes inactive.
let port
function openPort () {
port = chrome.runtime.connect({ name: 'devtools' })
port.onDisconnect.addListener(openPort)
}
openPort()
// fetch the list of configurable features from the config and create toggles for them.
const loadConfigurableFeatures = new Promise((resolve) => {
sendMessage('getListContents', 'config', ({ data: config }) => {
const features = Object.keys(config.features)
features.forEach((feature) => {
const btn = document.createElement('button')
btn.id = feature
btn.innerText = `${feature}: ???`
document.querySelector('#protections').appendChild(btn)
btn.addEventListener('click', () => {
port.postMessage({
action: `toggle${feature}`,
tabId
})
reloadPage()
})
})
resolve(features)
})
})
const actionIcons = {
block: '🚫',
redirect: '➡️',
ignore: '⚠️',
none: '✅',
'ad-attribution': '🪄',
'ignore-user': '🎛️'
}
/**
* @param {HTMLElement} element
* @param {string} textName
* @param {boolean} isEnabled
*/
function setupProtectionButton (element, textName, isEnabled) {
element.innerText = `${textName}: ${isEnabled ? 'ON' : 'OFF'}`
element.classList.add(`protection-button-${isEnabled ? 'on' : 'off'}`)
element.classList.remove(`protection-button-${isEnabled ? 'off' : 'on'}`)
}
const actionHandlers = {
tracker: (m) => {
const { tracker, url, requestData, siteUrl, serviceWorkerInitiated } = m.message
const row = document.getElementById('request-row').content.firstElementChild.cloneNode(true)
const cells = row.querySelectorAll('td')
const toggleLink = row.querySelector('.block-toggle')
toggleLink.href = ''
if (tracker.action === 'block') {
toggleLink.innerText = 'I'
} else {
toggleLink.innerText = 'B'
}
toggleLink.addEventListener('click', (ev) => {
ev.preventDefault()
port.postMessage({
action: toggleLink.innerText,
tabId,
tracker,
requestData,
siteUrl
})
row.classList.remove(tracker.action)
row.classList.add(toggleLink.innerText === 'I' ? 'ignore' : 'block')
})
cells[1].textContent = `${serviceWorkerInitiated ? '⚙️ ' : ''}${url}`
cells[2].querySelector('.request-action').textContent = `${actionIcons[tracker.action]} ${tracker.action} (${tracker.reason})`
cells[3].textContent = tracker.fullTrackerDomain
cells[4].textContent = requestData.type
row.classList.add(tracker.action)
addRequestRow(row)
},
tabChange: (m) => {
const tab = m.message
const protectionDisabled = tab.site?.allowlisted || tab.site?.isBroken
setupProtectionButton(protectionButton, 'Protection', !protectionDisabled)
loadConfigurableFeatures.then((features) => {
features.forEach((feature) => {
const featureEnabled = tab.site?.enabledFeatures.includes(feature)
const featureButton = document.getElementById(feature)
setupProtectionButton(featureButton, feature, featureEnabled)
})
})
},
cookie: (m) => {
const { action, kind, url, requestId, type } = m.message
const rowId = `request-${requestId}`
if (document.getElementById(rowId) !== null) {
const row = document.getElementById(rowId)
const cells = row.querySelectorAll('td')
row.classList.add(kind)
cells[3].textContent = `${cells[3].textContent}, ${kind}`
} else {
const row = document.getElementById('cookie-row').content.firstElementChild.cloneNode(true)
row.id = rowId
const cells = row.querySelectorAll('td')
const cleanUrl = new URL(url)
cleanUrl.search = ''
cleanUrl.hash = ''
cells[1].textContent = cleanUrl.href
cells[2].querySelector('.request-action').textContent = `🍪 ${action}`
cells[3].textContent = kind
cells[4].textContent = type
row.classList.add(kind)
addRequestRow(row)
}
},
runtimeChecks: (m) => {
const { documentUrl, matchType, matchedStackDomain, stack, scriptOrigins } = m.message
if (!matchType) return displayProxyRow(m)
const row = document.getElementById('cookie-row').content.firstElementChild.cloneNode(true)
const cells = row.querySelectorAll('td')
cells[1].textContent = documentUrl
cells[2].querySelector('.request-action').textContent = `Runtime Checks📨 ${matchType}`
if (scriptOrigins) {
cells[3].textContent = scriptOrigins.join(',')
}
if (stack) appendCallStack(cells[3], stack, 0)
if (matchedStackDomain) {
cells[4].textContent += `Matched: ${matchedStackDomain}`
}
row.classList.add('runtimeChecks')
addRequestRow(row)
},
jsException: (m) => {
const { documentUrl, message, filename, lineno, colno, stack, scriptOrigins } = m.message
const row = document.getElementById('cookie-row').content.firstElementChild.cloneNode(true)
const cells = row.querySelectorAll('td')
cells[1].textContent = documentUrl
cells[2].querySelector('.request-action').textContent = `JS🪲 ${message}`
if (scriptOrigins) cells[3].textContent = scriptOrigins.join(',')
if (stack) appendCallStack(cells[3], stack, 0)
cells[4].textContent = `${filename}:${lineno}:${colno}`
row.classList.add('jsException')
addRequestRow(row)
},
jscookie: (m) => {
const { documentUrl, action, reason, value, stack } = m.message
const scriptOrigins = [...getStackTraceOrigins(stack)]
const row = document.getElementById('cookie-row').content.firstElementChild.cloneNode(true)
const cells = row.querySelectorAll('td')
cells[1].textContent = documentUrl
cells[2].querySelector('.request-action').textContent = `JS🍪 ${action} (${reason})`
cells[3].textContent = scriptOrigins.join(',')
appendCallStack(cells[3], stack)
cells[4].textContent = value.split(';')[0]
row.classList.add('jscookie')
addRequestRow(row)
}
}
/**
* Data that comes from DDG proxy calls
* @param {*} m message from background
* @param {*} actionName nice name to display for the action
*/
function displayProxyRow (m) {
const { documentUrl, action, kind, stack, args } = m.message
const featureAction = m.action
const row = document.getElementById('cookie-row').content.firstElementChild.cloneNode(true)
const cells = row.querySelectorAll('td')
cells[1].textContent = documentUrl
cells[2].querySelector('.request-action').textContent = `${featureAction} proxy ${action}`
const argsOut = JSON.parse(args).join(', ')
cells[3].setAttribute('colspan', 2)
cells[4].remove()
cells[3].textContent = `${kind}(${argsOut})`
appendCallStack(cells[3], stack)
row.classList.add('proxyCall', featureAction)
addRequestRow(row)
}
function appendCallStack (cell, stack, drop = 2) {
if (stack) {
const lines = stack.split('\n')
// Shift off the first lines of the stack as will be us.
for (let i = 0; i < drop; i++) lines.shift()
const details = document.createElement('details')
const summary = document.createElement('summary')
summary.textContent = 'Call stack'
details.appendChild(summary)
details.appendChild(document.createTextNode(lines.join('\n')))
cell.appendChild(details)
}
}
/**
* General panel configuration that can change during its lifetime.
*
* Values below are the defaults.
*/
const panelConfig = {
rowVisibility: {
blocked: true,
ignored: true,
ignoredFirstParty: true,
redirected: true,
cookieHTTP: true,
cookieJS: true,
jsException: true,
runtimeChecks: true,
proxyCalls: false,
noneRequest: true,
ignoreUser: true
},
rowFilter: ''
}
function shouldShowRow (row) {
// empty search box is considered to be no filter
if (panelConfig.rowFilter !== '') {
// when a filter is in effect, match against the whole rows text content
if (!row.textContent.match(panelConfig.rowFilter)) {
return false
}
}
const className = row.classList[0]
switch (className) {
case 'ignore':
if (row.querySelector('.request-action').textContent === `${actionIcons.ignore} ignore (first party)`) {
return panelConfig.rowVisibility.ignored && panelConfig.rowVisibility.ignoredFirstParty
}
return panelConfig.rowVisibility.ignored
case 'block':
return panelConfig.rowVisibility.blocked
case 'redirect':
return panelConfig.rowVisibility.redirected
case 'cookie-tracker':
case 'set-cookie-tracker':
return panelConfig.rowVisibility.cookieHTTP
case 'jscookie':
return panelConfig.rowVisibility.cookieJS
case 'jsException':
return panelConfig.rowVisibility.jsException
case 'runtimeChecks':
return panelConfig.rowVisibility.runtimeChecks
case 'proxyCall':
return panelConfig.rowVisibility.proxyCalls
case 'none':
return panelConfig.rowVisibility.noneRequest
case 'ignore-user':
return panelConfig.rowVisibility.ignoreUser
}
// always show if we don't have an appropriate toggle
return true
}
function setRowVisible (row) {
row.hidden = !shouldShowRow(row)
}
port.onMessage.addListener((message) => {
const m = JSON.parse(message)
if (m.tabId === tabId) {
if (actionHandlers[m.action]) {
actionHandlers[m.action](m)
} else if (m?.message?.isProxy) {
displayProxyRow(m)
}
if (document.querySelector('tbody').lastChild) {
setRowVisible(document.querySelector('tbody').lastChild)
}
}
})
function updateTabSelector () {
chrome.tabs.query({}, (tabs) => {
while (tabPicker.firstChild !== null) {
tabPicker.removeChild(tabPicker.firstChild)
}
const selectTabOption = document.createElement('option')
selectTabOption.value = ''
selectTabOption.innerText = '--Select Tab--'
tabPicker.appendChild(selectTabOption)
tabs.forEach((tab) => {
if (tab.url.startsWith('http')) {
const elem = document.createElement('option')
elem.value = tab.id
elem.innerText = tab.title
if (tab.id === tabId) {
elem.setAttribute('selected', true)
}
tabPicker.appendChild(elem)
}
})
})
}
if (!chrome.devtools) {
updateTabSelector()
chrome.tabs.onUpdated.addListener(updateTabSelector)
tabPicker.addEventListener('change', () => {
tabId = parseInt(tabPicker.selectedOptions[0].value)
clear()
port.postMessage({ action: 'setTab', tabId })
})
} else {
tabPicker.hidden = true
}
if (tabId) {
port.postMessage({ action: 'setTab', tabId })
}
function clear () {
while (table.lastChild) {
table.removeChild(table.lastChild)
}
panelConfig.lastRowTemplate = undefined
panelConfig.currentCounter = undefined
}
function reloadPage () {
clear()
if (chrome.devtools) {
chrome.devtools.inspectedWindow.eval('window.location.reload();')
} else {
chrome.tabs.reload(tabId)
}
}
// listeners for buttons and toggles
clearButton.addEventListener('click', clear)
refreshButton.addEventListener('click', reloadPage)
protectionButton.addEventListener('click', () => {
port.postMessage({
action: 'toggleProtection',
tabId
})
reloadPage()
})
sendMessage('getSetting', { name: 'tds-channel' }, (result) => {
console.log('setting', result)
const active = tdsOption.querySelector(`[value=${result}`)
if (active) {
active.setAttribute('selected', true)
}
})
tdsOption.addEventListener('change', (e) => {
sendMessage('updateSetting', {
name: 'tds-channel',
value: tdsOption.selectedOptions[0].value
}, () => {
sendMessage('reloadList', 'tds')
})
})
const displayFilters = document.querySelector('#table-filter').querySelectorAll('input')
displayFilters.forEach((input) => {
// initialise filters to default values
if (input.id === 'search-box') {
input.value = panelConfig.rowFilter
} else {
input.checked = panelConfig.rowVisibility[input.dataset.filterToggle]
}
// register listeners to update row visibility when filters are changed
input.addEventListener('change', () => {
if (input.id === 'search-box') {
panelConfig.rowFilter = input.value
}
if (input.dataset.filterToggle) {
panelConfig.rowVisibility[input.dataset.filterToggle] = input.checked
}
document.querySelectorAll('tbody > tr').forEach(setRowVisible)
})
})
/**
* Observes the dev settings for resizing to ensure the table head sticks correctly to the bottom of the settings.
*/
const settingsResizeObserver = new ResizeObserver(function (entries) {
const height = entries[0].contentRect.height
document.querySelector('thead').style.top = `${height}px`
})
settingsResizeObserver.observe(document.getElementById('settings-panel'))