-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
230 lines (207 loc) · 9.54 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
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
document.addEventListener('DOMContentLoaded', function() {
const repoInfoElement = document.getElementById('repo-info');
const githubTokenInput = document.getElementById('github-token');
const saveTokenButton = document.getElementById('save-token');
const startFetchButton = document.getElementById('start-fetch');
const fileTableBody = document.getElementById('file-table-body');
const downloadFilesButton = document.getElementById('download-files');
const settingsToggleButton = document.getElementById('settings-toggle');
const settingsPanel = document.getElementById('settings-panel');
const saveRulesButton = document.getElementById('save-rules');
const downloadSection = document.getElementById('download-section');
const estimatedSizeSpan = document.getElementById('estimated-size');
const fileList = document.getElementById('file-list');
const loadingIndicator = document.getElementById('loading');
const mainContent = document.getElementById('main-content');
const mergyignoreInputs = {
includePath: document.getElementById('include-path'),
includeExtension: document.getElementById('include-extension'),
excludePath: document.getElementById('exclude-path'),
excludeExtension: document.getElementById('exclude-extension')
};
let currentRepo = null;
let fetchedFiles = [];
// Load saved token and check if settings should be shown
chrome.storage.sync.get(['githubToken', 'mergyignoreRules'], function(result) {
if (result.githubToken) {
githubTokenInput.value = result.githubToken;
settingsPanel.style.display = 'none';
} else {
settingsPanel.style.display = 'block';
}
if (result.mergyignoreRules) {
const rules = JSON.parse(result.mergyignoreRules);
mergyignoreInputs.includePath.value = rules.includePath.join('\n');
mergyignoreInputs.includeExtension.value = rules.includeExtension.join('\n');
mergyignoreInputs.excludePath.value = rules.excludePath.join('\n');
mergyignoreInputs.excludeExtension.value = rules.excludeExtension.join('\n');
} else {
// Set default exclude rules
mergyignoreInputs.excludePath.value = 'bin\nobj\nnode_modules\nbuild\ndist\n.git';
mergyignoreInputs.excludeExtension.value = '.jpg\n.jpeg\n.png\n.gif\n.bmp\n.tiff\n.ico\n.svg\n.webp\n.mp4\n.avi\n.mov\n.wmv\n.flv\n.ogg\n.mp3\n.wav\n.flac\n.pdf\n.doc\n.docx\n.xls\n.xlsx\n.ppt\n.pptx\n.zip\n.rar\n.tar\n.gz\n.7z\n.exe\n.dll\n.so\n.dylib\n.class\n.pyc\n.pyo\n.o\n.obj\n.lib\n.a\n.jar\n.war\n.ear\n.db\n.sqlite\n.sqlite3\n.mdf\n.ldf\n.bak\n.tmp\n.temp\n.swp\n.DS_Store\n.lock\n.log';
}
});
// Toggle settings panel
settingsToggleButton.addEventListener('click', function() {
if (settingsPanel.style.display === 'none') {
settingsPanel.style.display = 'block';
mainContent.style.display = 'none';
fileList.style.display = 'none';
downloadFilesButton.style.display = 'none';
clearFetchedFiles();
} else {
settingsPanel.style.display = 'none';
mainContent.style.display = 'block';
}
});
function clearFetchedFiles() {
fetchedFiles = [];
fileTableBody.innerHTML = '';
estimatedSizeSpan.textContent = '';
}
// Save token
saveTokenButton.addEventListener('click', function() {
const token = githubTokenInput.value.trim();
if (token) {
chrome.storage.sync.set({githubToken: token}, function() {
chrome.runtime.sendMessage({action: 'setToken', token: token}, function(response) {
if (response && response.success) {
alert('GitHub token saved and set!');
} else {
alert('Error setting token in background script.');
}
});
});
} else {
alert('Please enter a valid GitHub token.');
}
});
// Save MergyIgnore rules
saveRulesButton.addEventListener('click', function() {
const rules = {
includePath: mergyignoreInputs.includePath.value.split('\n').filter(Boolean),
includeExtension: mergyignoreInputs.includeExtension.value.split('\n').filter(Boolean),
excludePath: mergyignoreInputs.excludePath.value.split('\n').filter(Boolean),
excludeExtension: mergyignoreInputs.excludeExtension.value.split('\n').filter(Boolean)
};
chrome.storage.sync.set({mergyignoreRules: JSON.stringify(rules)}, function() {
alert('MergyIgnore rules saved!');
});
});
// Display files in the table
function displayFiles(files) {
fileTableBody.innerHTML = '';
files.forEach(file => {
const row = document.createElement('tr');
row.innerHTML = `
<td><input type="checkbox" checked data-path="${file.path}"></td>
<td>${formatFileSize(file.size)}</td>
<td title="${file.name}">${file.name}</td>
<td title="${file.path}">${file.path}</td>
`;
row.querySelector('input[type="checkbox"]').addEventListener('change', updateEstimatedSize);
fileTableBody.appendChild(row);
});
}
// Update estimated size
function updateEstimatedSize() {
const selectedFiles = Array.from(fileTableBody.querySelectorAll('input[type="checkbox"]:checked'))
.map(checkbox => fetchedFiles.find(file => file.path === checkbox.dataset.path));
const totalSize = selectedFiles.reduce((sum, file) => sum + file.size, 0);
estimatedSizeSpan.textContent = `Estimated size: ${formatFileSize(totalSize)}`;
}
// Download combined files
startFetchButton.addEventListener('click', function() {
if (!currentRepo) {
alert('No repository detected. Please navigate to a GitHub repository page.');
return;
}
showLoading(true);
fileList.style.display = 'none';
downloadFilesButton.style.display = 'none';
clearFetchedFiles();
chrome.storage.sync.get(['githubToken', 'mergyignoreRules'], function(result) {
if (!result.githubToken) {
showLoading(false);
alert('GitHub token not set. Please set your Personal Access Token in the settings.');
return;
}
let rules;
try {
rules = result.mergyignoreRules ? JSON.parse(result.mergyignoreRules) : {};
} catch (e) {
console.error('Error parsing MergyIgnore rules:', e);
rules = {};
}
// Ensure all rule arrays exist
rules.includePath = rules.includePath || [];
rules.includeExtension = rules.includeExtension || [];
rules.excludePath = rules.excludePath || [];
rules.excludeExtension = rules.excludeExtension || [];
chrome.runtime.sendMessage({
action: 'fetchFiles',
repo: currentRepo,
mergyignoreRules: rules,
token: result.githubToken
}, function(response) {
showLoading(false);
if (response.error) {
alert(`Error: ${response.error}`);
} else if (response.files) {
fetchedFiles = response.files;
displayFiles(fetchedFiles);
updateEstimatedSize();
fileList.style.display = 'block';
downloadFilesButton.style.display = 'inline-flex';
}
});
});
});
// Download combined files
downloadFilesButton.addEventListener('click', function() {
showLoading(true);
const selectedFiles = Array.from(fileTableBody.querySelectorAll('input[type="checkbox"]:checked'))
.map(checkbox => checkbox.dataset.path);
chrome.runtime.sendMessage({
action: 'downloadFiles',
repo: currentRepo,
files: selectedFiles
}, function(response) {
showLoading(false);
if (response.error) {
alert(`Error: ${response.error}`);
} else if (response.content) {
const blob = new Blob([response.content], {type: 'text/plain'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${currentRepo.owner}_${currentRepo.name}_combined.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
});
});
function showLoading(show) {
loadingIndicator.style.display = show ? 'block' : 'none';
startFetchButton.disabled = show;
downloadFilesButton.disabled = show;
}
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// Load repository information
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "getRepoInfo"}, function(response) {
if (response && response.repo) {
currentRepo = response.repo;
repoInfoElement.textContent = `Repository: ${currentRepo.owner}/${currentRepo.name}`;
}
});
});
});