Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
277 lines (237 loc) · 9.46 KB
/
Copy pathscript.js
File metadata and controls
277 lines (237 loc) · 9.46 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
/*
* This program is licensed under the GNU GPL 3.0.
* You may redistribute and modify it under the terms of the license.
* See <https://www.gnu.org/licenses/gpl-3.0.html> for details.
*/
let isInvalid = false;
const inputField = document.getElementById('extensionInput');
// Handle form submission to download the extension
document.getElementById('extensionForm').addEventListener('submit', function (event) {
event.preventDefault();
downloadExtension();
});
// Update download options and focus on the input when browser selection changes
document.querySelectorAll('.browser-input').forEach(input => {
input.addEventListener('change', function () {
updateDownloadOptions();
inputField.focus();
});
});
// Keyboard shortcut '/' to focus on the input
document.addEventListener('keydown', function (event) {
if (event.key === '/' && event.target.tagName !== 'INPUT') {
event.preventDefault();
inputField.focus();
}
});
// Update download options based on the selected browser
function updateDownloadOptions() {
const browser = document.querySelector('input[name="browser"]:checked').value;
const downloadType = document.getElementById('downloadType');
const options = {
chrome: ['crx'],
firefox: ['xpi', 'zip'],
opera: ['crx'],
edge: ['crx']
};
downloadType.innerHTML = "";
options[browser].forEach(format => {
const option = document.createElement("option");
option.value = format;
option.textContent = `Download as ${format.toUpperCase()}`;
downloadType.appendChild(option);
});
}
updateDownloadOptions();
// Detect browser from input URL and update the browser selection
inputField.addEventListener("input", function () {
detectBrowserFromUrl(this.value.trim());
});
function detectBrowserFromUrl(input) {
const urlPatterns = {
chrome: [
/chrome\.google\.com\/webstore\/detail\//,
/chromewebstore\.google\.com\/detail\//
],
edge: [/microsoftedge\.microsoft\.com\/addons\/detail\//],
opera: [/addons\.opera\.com\/.*extensions\/details\//],
firefox: [
/addons\.mozilla\.org\/.*addon\//,
/addons\.allizom\.org\/.*addon\//,
/addons-dev\.allizom\.org\/.*addon\//,
/addons\.thunderbird\.net\/.*addon\//
]
};
for (const [browser, patterns] of Object.entries(urlPatterns)) {
if (patterns.some(pattern => pattern.test(input))) {
document.getElementById(browser).checked = true;
updateDownloadOptions();
break;
}
}
}
// Reset the border color in case of invalid input
inputField.addEventListener('input', function () {
if (isInvalid) {
inputField.style.borderColor = '';
isInvalid = false;
}
});
// Handle extension download logic
function downloadExtension() {
const input = inputField.value.trim();
const browserElement = document.querySelector('input[name="browser"]:checked');
const browser = browserElement.value;
const downloadType = document.getElementById('downloadType').value;
let extensionId = extractExtensionId(input, browser);
const downloadUrl = getDownloadUrl(browser, extensionId);
if (!input) return;
if (!extensionId) {
inputField.style.borderColor = '#b44dff';
alert("Please enter a valid Extension ID or URL.");
isInvalid = true;
return;
}
if (downloadType === "zip" && browser === "firefox") {
convertXpiToZip(downloadUrl, extensionId);
return;
}
window.location.href = downloadUrl;
}
// Extract extension ID from the URL based on the browser type
function extractExtensionId(input, browser) {
const patterns = {
chrome: /([a-z]{32})/,
edge: /([a-z]{32})/,
firefox: /addons(?:-dev)?\.(?:mozilla\.org|allizom\.org|thunderbird\.net)\/[^\/]+\/(?:firefox|thunderbird)\/addon\/([^\/?]+)/,
opera: /(?:addons.opera.com\/.*?\/extensions\/details\/)([a-zA-Z0-9_-]+)(?=\/|$)/
};
// If input is already an extension ID, return it directly
if ((browser === "firefox" || browser === "opera") && /^[a-zA-Z0-9_-]+$/.test(input)) {
return input;
}
const match = patterns[browser]?.exec(input);
return match ? match[1] : null;
}
// Get the base download URL for each browser
function getDownloadUrl(browser, extensionId) {
const baseUrls = {
chrome: `https://clients2.google.com/service/update2/crx?response=redirect&os=win&arch=x86-64&os_arch=x86-64&nacl_arch=x86-64&prod=chromiumcrx&prodchannel=unknown&prodversion=132.0.0.0&acceptformat=crx2,crx3&x=id%3D${extensionId}%26uc`,
edge: `https://edge.microsoft.com/extensionwebstorebase/v1/crx?response=redirect&x=id%3D${extensionId}%26installsource%3Dondemand%26uc`,
opera: `https://addons.opera.com/extensions/download/${extensionId}`,
};
const inputUrl = inputField.value.trim();
if (browser === "firefox") {
if (inputUrl.includes("addons.allizom.org")) {
return `https://addons.allizom.org/firefox/downloads/latest/${extensionId}/platform:5/${extensionId}.xpi`;
}
if (inputUrl.includes("addons-dev.allizom.org")) {
return `https://addons-dev.allizom.org/firefox/downloads/latest/${extensionId}/platform:5/${extensionId}.xpi`;
}
if (inputUrl.includes("addons.thunderbird.net")) {
return `https://addons.thunderbird.net/firefox/downloads/latest/${extensionId}/platform:5/${extensionId}.xpi`;
}
else {
return `https://addons.mozilla.org/firefox/downloads/latest/${extensionId}/platform:5/${extensionId}.xpi`;
}
}
return baseUrls[browser];
}
// XPI to ZIP
function convertXpiToZip(url, extensionId) {
if (url.includes("addons.thunderbird.net")) {
alert("Please download the XPI file first, then upload it here to convert it into a ZIP.");
return;
}
fetch(url)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
var blob = new Blob([arrayBuffer], { type: "application/zip" });
var zipUrl = URL.createObjectURL(blob);
var link = document.createElement("a");
link.href = zipUrl;
link.download = extensionId + ".zip";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch((error) => {
console.error("Error converting XPI to ZIP:", error);
alert(`Please ensure that the Extension ID or URL and browser are correct.`);
});
}
// Handle file uploads for CRX/XPI conversion
const dropZone = document.getElementById("dropZone");
const fileInput = document.getElementById("fileInput");
const dropText = document.getElementById("dropText");
const convertButton = document.getElementById("convertButton");
// Initially disable the convert button
convertButton.disabled = true;
// Click to open file selection
dropZone.addEventListener("click", () => fileInput.click());
// Handle file selection
fileInput.addEventListener("change", handleFileSelect);
dropZone.addEventListener("dragover", (event) => {
event.preventDefault();
dropZone.style.background = "rgba(255, 255, 255, 0.2)";
});
dropZone.addEventListener("dragleave", () => {
dropZone.style.background = "transparent";
});
dropZone.addEventListener("drop", (event) => {
event.preventDefault();
fileInput.files = event.dataTransfer.files;
handleFileSelect();
});
function handleFileSelect() {
if (fileInput.files.length > 0) {
const file = fileInput.files[0];
dropText.textContent = `Selected: ${file.name}`;
convertButton.disabled = false;
} else {
dropText.textContent = "Drop your .crx or .xpi file here or click to browse";
convertButton.disabled = true;
}
}
// Handle Convert Button Click
convertButton.addEventListener("click", function () {
if (fileInput.files.length === 0) return;
const file = fileInput.files[0];
convertFileToZip(file);
});
// Convert CRX/XPI to ZIP
function convertFileToZip(file) {
const reader = new FileReader();
reader.onload = function (event) {
const arrayBuffer = event.target.result;
let zipBlob;
if (file.name.endsWith(".crx")) {
zipBlob = ArrayBufferToBlob(arrayBuffer);
} else {
zipBlob = new Blob([arrayBuffer], { type: "application/zip" });
}
downloadZip(zipBlob, file.name.replace(/\.[^.]+$/, ".zip"));
};
reader.readAsArrayBuffer(file);
}
// Convert CRX to ZIP (Strips CRX Header)
function ArrayBufferToBlob(arraybuffer) {
var buf = new Uint8Array(arraybuffer);
var publicKeyLength, signatureLength, zipStartOffset;
if (buf[4] === 2) { // CRX v2 format
publicKeyLength = (buf[8] + (buf[9] << 8) + (buf[10] << 16) + (buf[11] << 24)) >>> 0;
signatureLength = (buf[12] + (buf[13] << 8) + (buf[14] << 16) + (buf[15] << 24)) >>> 0;
zipStartOffset = 16 + publicKeyLength + signatureLength;
} else { // CRX v3 format
publicKeyLength = (buf[8] + (buf[9] << 8) + (buf[10] << 16) + (buf[11] << 24)) >>> 0;
zipStartOffset = 12 + publicKeyLength;
}
return new Blob([new Uint8Array(arraybuffer, zipStartOffset)], { type: "application/zip" });
}
// Download the converted ZIP file
function downloadZip(blob, fileName) {
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
}