Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
200 lines (176 loc) · 6.39 KB
/
Copy pathpopup.js
File metadata and controls
200 lines (176 loc) · 6.39 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
document.addEventListener('DOMContentLoaded', function() {
// Initialize accordion functionality
const acc = document.getElementsByClassName('accordion');
for (let i = 0; i < acc.length; i++) {
acc[i].addEventListener('click', function() {
this.classList.toggle('active');
const panel = this.nextElementSibling;
panel.classList.toggle('open');
// Only load data when opening the panel
if (panel.classList.contains('open')) {
handlePanelOpen(this, panel);
}
});
}
// Initialize toggle switches
const toggles = ["generics", "specifics", "aws", "checkEnv", "checkGit",
"alerts", "notifications", "uniqueByHostname"];
const toggleDefaults = {
"generics": true,
"specifics": true,
"aws": true,
"checkEnv": false,
"checkGit": false,
"alerts": true,
"notifications": true,
"uniqueByHostname": false
};
// Initialize toggle switches with storage values
toggles.forEach(toggle => {
chrome.storage.sync.get([toggle], result => {
const checkbox = document.getElementById(toggle);
if (result[toggle] === undefined) {
checkbox.checked = toggleDefaults[toggle];
chrome.storage.sync.set({ [toggle]: toggleDefaults[toggle] });
} else {
checkbox.checked = result[toggle];
}
});
document.getElementById(toggle).addEventListener('change', function() {
chrome.storage.sync.set({ [toggle]: this.checked });
});
});
// Initialize other UI components
initDenyList();
initFindingsButtons();
initTabOpener();
});
function handlePanelOpen(button, panel) {
if (button.textContent.includes('Deny List')) {
loadDenyList();
} else if (button.textContent.includes('Findings')) {
loadFindings();
}
}
function initDenyList() {
const denyListElement = document.getElementById('denyList');
const updateDenyList = () => {
const denyList = denyListElement.value.split(',')
.map(item => item.trim())
.filter(item => item);
chrome.storage.sync.set({ originDenyList: denyList });
};
denyListElement.addEventListener('input', updateDenyList);
denyListElement.addEventListener('paste', updateDenyList);
}
function initFindingsButtons() {
// Clear current origin findings
document.getElementById('clearOriginFindings').addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }).then(tabs => {
const url = tabs[0]?.url;
if (!url) return;
try {
const origin = new URL(url).origin;
chrome.storage.sync.get('leakedKeys', ({ leakedKeys = {} }) => {
delete leakedKeys[origin];
chrome.storage.sync.set({ leakedKeys });
chrome.action.setBadgeText({ text: '' });
document.getElementById('findingList').innerHTML = '';
});
} catch (error) {
console.error('Invalid URL:', error);
}
});
});
// Clear all findings
document.getElementById('clearAllFindings').addEventListener('click', () => {
chrome.storage.sync.set({ leakedKeys: {} });
chrome.action.setBadgeText({ text: '' });
document.getElementById('findingList').innerHTML = '';
});
// Download findings
document.getElementById('downloadAllFindings').addEventListener('click', downloadCSV);
}
function initTabOpener() {
document.getElementById('openTabs').addEventListener('click', () => {
const rawTabList = document.getElementById('tabList').value;
const tabList = rawTabList.split(',')
.map(item => item.trim())
.filter(item => item);
tabList.forEach(url => {
if (isValidUrl(url)) {
chrome.tabs.create({ url });
}
});
});
}
function loadDenyList() {
const denyListElement = document.getElementById('denyList');
chrome.storage.sync.get('originDenyList', ({ originDenyList = [] }) => {
denyListElement.value = originDenyList.join(', ');
});
}
function loadFindings() {
chrome.tabs.query({ active: true, currentWindow: true }).then(tabs => {
const url = tabs[0]?.url;
if (!url) return;
try {
const origin = new URL(url).origin;
chrome.storage.sync.get('leakedKeys', ({ leakedKeys = {} }) => {
const findings = leakedKeys[origin] || [];
const listElement = document.getElementById('findingList');
listElement.innerHTML = findings.map(f => `
<li>
<strong>${htmlEntities(f.key)}</strong><br>
${htmlEntities(f.match.substring(0, 50))}...<br>
<small>Found in: ${htmlEntities(f.src)}</small>
</li>
`).join('');
});
} catch (error) {
console.error('Error loading findings:', error);
}
});
}
function downloadCSV() {
chrome.storage.sync.get('leakedKeys', ({ leakedKeys = {} }) => {
const csvContent = Object.entries(leakedKeys)
.flatMap(([origin, findings]) =>
findings.map(f => [
`"${origin}"`,
`"${f.src}"`,
`"${f.parentUrl}"`,
`"${f.key}"`,
`"${f.match}"`,
`"${f.encoded}"`
].join(','))
).join('\n');
if (!csvContent) {
alert('No findings to download!');
return;
}
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
chrome.downloads.download({
url: url,
filename: 'ShadowHound_findings.csv',
conflictAction: 'uniquify'
});
});
}
// Helper functions
function htmlEntities(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}