-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
180 lines (145 loc) · 5.97 KB
/
popup.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
// Define global variables
let extractedData = {
ipAddresses: [],
urlsAndUris: [],
hashes: [],
cves: []
};
let extractedMatches = [];
const patterns = {
ipAddresses: /\b(?:\d{1,3}(?:\.\d{1,3}|\[\.\]){3}\d{1,3}|\d{1,3}(?:\[\.\]\d{1,3}){3}|\d{1,3}(?:\.\d{1,3}){3})\b/g,
urlsAndUris: /\b(?:https?:\/\/[^\s/$.?#].[^\s]*|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}|[a-zA-Z0-9-]+\[[.]\][a-zA-Z]{2,}|[a-zA-Z]:\\[^\\]+\\[^\\]+\\[^\\]+\\[^\\]+\.[a-zA-Z0-9]+|\/[^\s/]+(?:\/[^\s/]+)*|\\\\[^\\]+\\[^\\]+(?:\\[^\\]+)+\\[^\s/]+\.[a-zA-Z0-9]+)\b/g,
hashes: /\b[0-9a-fA-F]{32}\b|\b[0-9a-fA-F]{40}\b|\b[0-9a-fA-F]{64}\b/g,
cves: /\bCVE-\d{4}-\d{4,7}\b/g
};
// Union pattern for IP addresses, URLs/URIs, and hashes
const unionPattern = new RegExp(
patterns.ipAddresses.source + '|' +
patterns.urlsAndUris.source + '|' +
patterns.hashes.source + '|' +
patterns.cves.source,
'g'
);
// Function to extract data from the active tab
function extractDataFromTab(tabId) {
chrome.scripting.executeScript({
target: { tabId: tabId },
function: extractText
}, function(results) {
const extractedText = results[0].result;
const extracted = extractIpAddressAndUrlAndHashes(extractedText);
// Extracted data is added in the order it's found
extractedData.ipAddresses.push(...extracted.ipAddresses);
extractedData.urlsAndUris.push(...extracted.urlsAndUris);
extractedData.hashes.push(...extracted.hashes);
extractedData.cves.push(...extracted.cves)
console.log("Extracted Data:", extractedData);
extractedMatches = [];
// Extract matches from the extracted text
extractedMatches.push(...(extractedText.match(unionPattern) || []));
// Display the extracted data
console.log("Extracted Matches:", extractedMatches);
displayMatches();
});
}
// Function to display matches
function displayMatches() {
const extractedDataContainer = document.getElementById('extractedData');
extractedDataContainer.innerHTML = '';
// Filter extractedMatches to unique values
const uniqueMatches = [...new Set(extractedMatches)];
// Iterate through unique matches and create divs
uniqueMatches.forEach(match => {
const div = createItemDiv(match, " ");
extractedDataContainer.appendChild(div);
});
}
// Function to create a div for a match
function createItemDiv(value, label) {
const div = document.createElement('div');
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function() {
removeItem(value);
});
const text = document.createElement('span');
text.textContent = label + value;
div.appendChild(removeButton);
div.appendChild(text);
return div;
}
// Function to remove an item from the displayed and extracted matches
function removeItem(value) {
extractedMatches = extractedMatches.filter(match => match !== value);
removeFromExtractedData(value);
displayMatches();
}
// Function to remove an item from extractedData
function removeFromExtractedData(value) {
extractedData.ipAddresses = extractedData.ipAddresses.filter(ip => ip !== value);
extractedData.urlsAndUris = extractedData.urlsAndUris.filter(url => url !== value);
extractedData.hashes = extractedData.hashes.filter(hash => hash !== value)
extractedData.cves = extractedData.cves.filter(cves => cves !== value)
}
// Function to extract text from the active tab
function extractText() {
const allText = document.body.innerText;
console.log(allText)
return allText;
}
// Function to extract IP addresses, URLs, and Hashes from text
function extractIpAddressAndUrlAndHashes(text) {
const extractedData = {};
for (const key in patterns) {
const pattern = patterns[key];
const matches = text.match(pattern);
if (matches) {
extractedData[key] = [...new Set(matches)];
} else {
extractedData[key] = [];
}
}
return extractedData;
}
// Event listener when popup is opened
document.addEventListener('DOMContentLoaded', function() {
const downloadButton = document.getElementById('downloadButton');
let activeTab; // Define activeTab here
// Get the active tab and extract data
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
activeTab = tabs[0]; // Update the value of activeTab
extractDataFromTab(activeTab.id);
});
downloadButton.addEventListener('click', function() {
// Combine all the data into a single array
const allData = extractedMatches.slice();
// Filter out duplicates and maintain order
const uniqueData = [];
const uniqueSet = new Set();
for (const item of allData) {
if (!uniqueSet.has(item)) {
uniqueSet.add(item);
uniqueData.push(item);
}
}
// Create a text file with the correct content
const dataText = uniqueData.join('\n');
// Generate a filename using the TLD and current epoch time
const tld = new URL(activeTab.url).hostname.split('.').slice(-2).join('.');
const filename = `${tld}_${Date.now()}.txt`;
// Create a blob with the text data
const blob = new Blob([dataText], { type: 'text/plain' });
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create an invisible anchor element to trigger the download
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = filename;
document.body.appendChild(a);
// Trigger the download
a.click();
// Clean up the URL object
URL.revokeObjectURL(url);
});
});