-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
475 lines (400 loc) · 15 KB
/
Copy pathpopup.js
File metadata and controls
475 lines (400 loc) · 15 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/**
* Browser Diagnose Tool - Optimierte Version 2.0
*
* Features:
* - Paralleles Auslesen von LocalStorage, SessionStorage und Cookies
* - Optimiertes Löschen mit Promise.allSettled()
* - Chunked Display für große Datenmengen
* - Performance-Tracking
*/
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const scanBtn = document.getElementById('scan-btn');
const clearBtn = document.getElementById('clear-btn');
const exportBtn = document.getElementById('export-btn');
const statusMessage = document.getElementById('status-message');
const loadingElement = document.getElementById('loading');
const errorElement = document.getElementById('error-message');
const resultsElement = document.getElementById('results');
const currentDomainElement = document.getElementById('current-domain');
const performanceInfo = document.getElementById('performance-info');
// State
let scanData = null;
let currentUrl = null;
let currentTabId = null;
// === Injected Functions (werden in die Webseite injiziert) ===
function getStorageData() {
try {
const localStorageData = {};
const sessionStorageData = {};
// LocalStorage auslesen
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
try {
localStorageData[key] = localStorage.getItem(key);
} catch (e) {
localStorageData[key] = '[Fehler beim Lesen]';
}
}
// SessionStorage auslesen
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
try {
sessionStorageData[key] = sessionStorage.getItem(key);
} catch (e) {
sessionStorageData[key] = '[Fehler beim Lesen]';
}
}
return {
localStorage: localStorageData,
sessionStorage: sessionStorageData
};
} catch (error) {
return {
localStorage: {},
sessionStorage: {},
error: error.message
};
}
}
function clearStorageData() {
try {
const localCount = localStorage.length;
const sessionCount = sessionStorage.length;
localStorage.clear();
sessionStorage.clear();
return {
success: true,
cleared: { localStorage: localCount, sessionStorage: sessionCount }
};
} catch (error) {
return { success: false, error: error.message };
}
}
// === UI Helper Functions ===
function showLoading(message = 'Scanne Seite...') {
loadingElement.querySelector('p').textContent = message;
loadingElement.style.display = 'block';
resultsElement.style.display = 'none';
errorElement.style.display = 'none';
}
function hideLoading() {
loadingElement.style.display = 'none';
}
function showError() {
errorElement.style.display = 'block';
resultsElement.style.display = 'none';
hideLoading();
}
function showResults() {
resultsElement.style.display = 'block';
errorElement.style.display = 'none';
hideLoading();
}
function showStatus(text, type = 'success') {
statusMessage.textContent = text;
statusMessage.className = 'status ' + type;
statusMessage.style.display = 'block';
setTimeout(() => {
statusMessage.style.display = 'none';
}, 4000);
}
function clearDisplay() {
document.getElementById('localstorage-data').innerHTML = '<div class="empty-state">Keine Daten</div>';
document.getElementById('sessionstorage-data').innerHTML = '<div class="empty-state">Keine Daten</div>';
document.getElementById('cookies-data').innerHTML = '<div class="empty-state">Keine Daten</div>';
document.getElementById('localStorage-count').textContent = '0';
document.getElementById('sessionStorage-count').textContent = '0';
document.getElementById('cookies-count').textContent = '0';
document.getElementById('ls-count').textContent = '0';
document.getElementById('ss-count').textContent = '0';
document.getElementById('ck-count').textContent = '0';
scanData = null;
exportBtn.disabled = true;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = String(text);
return div.innerHTML;
}
function truncateValue(value, maxLength = 100) {
const str = String(value);
if (str.length <= maxLength) return escapeHtml(str);
return escapeHtml(str.substring(0, maxLength)) + '...';
}
// === Initialization ===
async function initialize() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab && tab.url) {
if (tab.url.startsWith('chrome://') || tab.url.startsWith('chrome-extension://') || tab.url.startsWith('about:')) {
currentDomainElement.textContent = 'Nicht verfügbar (geschützte Seite)';
scanBtn.disabled = true;
clearBtn.disabled = true;
return;
}
currentUrl = new URL(tab.url);
currentTabId = tab.id;
currentDomainElement.textContent = currentUrl.hostname;
} else {
currentDomainElement.textContent = 'Keine aktive Seite';
scanBtn.disabled = true;
clearBtn.disabled = true;
}
} catch (error) {
console.error('Initialization error:', error);
currentDomainElement.textContent = 'Fehler beim Laden';
}
}
// === Main Functions ===
async function scanPage() {
const startTime = performance.now();
scanData = null;
exportBtn.disabled = true;
showLoading('Scanne Seite...');
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || !tab.url || tab.url.startsWith('chrome://') || tab.url.startsWith('chrome-extension://')) {
showError();
return;
}
const tabId = tab.id;
// PARALLEL: Storage und Cookies gleichzeitig abrufen
const [storageResults, cookies] = await Promise.all([
// 1. Storage auslesen via Script Injection
chrome.scripting.executeScript({
target: { tabId: tabId },
func: getStorageData
}).then(results => {
if (results && results[0] && results[0].result) {
return results[0].result;
}
return { localStorage: {}, sessionStorage: {} };
}).catch(err => {
console.warn('Storage read error:', err);
return { localStorage: {}, sessionStorage: {} };
}),
// 2. Cookies abrufen
chrome.cookies.getAll({ url: tab.url }).catch(err => {
console.warn('Cookie read error:', err);
return [];
})
]);
const endTime = performance.now();
const duration = (endTime - startTime).toFixed(0);
// Daten zusammenführen
const allData = {
url: tab.url,
domain: currentUrl ? currentUrl.hostname : '',
timestamp: new Date().toISOString(),
scanDuration: duration + 'ms',
localStorage: storageResults.localStorage || {},
sessionStorage: storageResults.sessionStorage || {},
cookies: cookies.map(cookie => ({
name: cookie.name,
value: cookie.value,
domain: cookie.domain,
path: cookie.path,
secure: cookie.secure,
httpOnly: cookie.httpOnly,
expirationDate: cookie.expirationDate,
sameSite: cookie.sameSite
}))
};
scanData = allData;
displayData(allData);
exportBtn.disabled = false;
performanceInfo.textContent = `Scan abgeschlossen in ${duration}ms`;
showStatus(`Scan erfolgreich in ${duration}ms`, 'success');
showResults();
} catch (error) {
console.error('Scan error:', error);
showError();
showStatus('Fehler beim Scannen: ' + error.message, 'error');
}
}
async function clearAllData() {
if (!confirm('Möchten Sie wirklich ALLE LocalStorage, SessionStorage und Cookies für diese Seite löschen?')) {
return;
}
const startTime = performance.now();
showLoading('Lösche Daten...');
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || !tab.url) {
showStatus('Keine aktive Seite gefunden', 'error');
hideLoading();
return;
}
const tabId = tab.id;
// PARALLEL: Storage löschen UND Cookies abrufen
const [clearResult, cookies] = await Promise.all([
chrome.scripting.executeScript({
target: { tabId: tabId },
func: clearStorageData
}).then(results => {
if (results && results[0] && results[0].result) {
return results[0].result;
}
return { success: false };
}).catch(err => {
console.warn('Storage clear error:', err);
return { success: false, error: err.message };
}),
chrome.cookies.getAll({ url: tab.url })
]);
// OPTIMIERT: Alle Cookies parallel löschen
let deletedCookies = 0;
if (cookies.length > 0) {
const deletePromises = cookies.map(cookie => {
const protocol = cookie.secure ? 'https:' : 'http:';
const domain = cookie.domain.startsWith('.') ? cookie.domain.substring(1) : cookie.domain;
const cookieUrl = `${protocol}//${domain}${cookie.path}`;
return chrome.cookies.remove({
url: cookieUrl,
name: cookie.name
}).then(() => {
deletedCookies++;
return true;
}).catch(() => false);
});
await Promise.allSettled(deletePromises);
}
const endTime = performance.now();
const duration = (endTime - startTime).toFixed(0);
const storageCleared = clearResult.success ?
`${clearResult.cleared?.localStorage || 0} LocalStorage, ${clearResult.cleared?.sessionStorage || 0} SessionStorage` :
'Storage-Fehler';
showStatus(`Gelöscht: ${storageCleared}, ${deletedCookies} Cookies (${duration}ms)`, 'success');
clearDisplay();
showResults();
} catch (error) {
console.error('Clear error:', error);
showStatus('Fehler beim Löschen: ' + error.message, 'error');
} finally {
hideLoading();
}
}
function exportData() {
if (!scanData) {
showStatus('Keine Daten zum Exportieren vorhanden', 'error');
return;
}
try {
const dataStr = JSON.stringify(scanData, null, 2);
const blob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const date = new Date().toISOString().split('T')[0];
const domain = scanData.domain || 'unknown';
const filename = `browser-diagnose-${domain}-${date}.json`;
chrome.downloads.download({
url: url,
filename: filename,
saveAs: true
}, (downloadId) => {
if (chrome.runtime.lastError) {
showStatus('Export fehlgeschlagen: ' + chrome.runtime.lastError.message, 'error');
} else {
showStatus('Daten erfolgreich exportiert!', 'success');
}
setTimeout(() => URL.revokeObjectURL(url), 5000);
});
} catch (error) {
console.error('Export error:', error);
showStatus('Export fehlgeschlagen: ' + error.message, 'error');
}
}
function displayData(data) {
requestAnimationFrame(() => {
// Counts aktualisieren
const lsCount = Object.keys(data.localStorage).length;
const ssCount = Object.keys(data.sessionStorage).length;
const ckCount = data.cookies.length;
document.getElementById('localStorage-count').textContent = lsCount;
document.getElementById('sessionStorage-count').textContent = ssCount;
document.getElementById('cookies-count').textContent = ckCount;
document.getElementById('ls-count').textContent = lsCount;
document.getElementById('ss-count').textContent = ssCount;
document.getElementById('ck-count').textContent = ckCount;
// LocalStorage anzeigen
displayStorageSection('localstorage-data', data.localStorage);
// SessionStorage anzeigen
displayStorageSection('sessionstorage-data', data.sessionStorage);
// Cookies anzeigen
displayCookiesSection('cookies-data', data.cookies);
});
}
function displayStorageSection(containerId, storageData) {
const container = document.getElementById(containerId);
container.innerHTML = '';
const entries = Object.entries(storageData);
if (entries.length === 0) {
container.innerHTML = '<div class="empty-state">Keine Daten</div>';
return;
}
const displayLimit = 50;
const fragment = document.createDocumentFragment();
for (let i = 0; i < Math.min(entries.length, displayLimit); i++) {
const [key, value] = entries[i];
const div = document.createElement('div');
div.className = 'data-item';
div.innerHTML = `<span class="key">${escapeHtml(key)}</span>:<span class="value">${truncateValue(value)}</span>`;
fragment.appendChild(div);
}
if (entries.length > displayLimit) {
const info = document.createElement('div');
info.className = 'data-item';
info.style.textAlign = 'center';
info.style.color = '#667eea';
info.textContent = `... und ${entries.length - displayLimit} weitere Einträge (siehe Export)`;
fragment.appendChild(info);
}
container.appendChild(fragment);
}
function displayCookiesSection(containerId, cookies) {
const container = document.getElementById(containerId);
container.innerHTML = '';
if (cookies.length === 0) {
container.innerHTML = '<div class="empty-state">Keine Cookies</div>';
return;
}
const displayLimit = 30;
const fragment = document.createDocumentFragment();
for (let i = 0; i < Math.min(cookies.length, displayLimit); i++) {
const cookie = cookies[i];
const div = document.createElement('div');
div.className = 'data-item';
const flags = [];
if (cookie.secure) flags.push('🔒');
if (cookie.httpOnly) flags.push('🚫JS');
div.innerHTML = `
<span class="key">${escapeHtml(cookie.name)}</span>
<span class="value">${truncateValue(cookie.value, 50)}</span>
${flags.length > 0 ? `<span style="float:right;font-size:10px">${flags.join(' ')}</span>` : ''}
`;
fragment.appendChild(div);
}
if (cookies.length > displayLimit) {
const info = document.createElement('div');
info.className = 'data-item';
info.style.textAlign = 'center';
info.style.color = '#667eea';
info.textContent = `... und ${cookies.length - displayLimit} weitere Cookies (siehe Export)`;
fragment.appendChild(info);
}
container.appendChild(fragment);
}
// === Event Listeners ===
scanBtn.addEventListener('click', scanPage);
clearBtn.addEventListener('click', clearAllData);
exportBtn.addEventListener('click', exportData);
// === Toggle Section (global function for onclick) ===
window.toggleSection = function(sectionId) {
const content = document.getElementById(sectionId + '-data');
if (content) {
content.style.display = content.style.display === 'none' ? 'block' : 'none';
}
};
// === Initialize ===
initialize();
});