-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
687 lines (606 loc) · 23.2 KB
/
Copy pathbackground.js
File metadata and controls
687 lines (606 loc) · 23.2 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed.");
});
// Include required scripts
importScripts('jszip.min.js', 'getImageType.js', 'adRemover.js');
// Store parsed selectors from EasyList
let adSelectors = null;
// Load EasyList at extension initialization
async function loadEasyList() {
try {
const response = await fetch('easylist.txt');
const text = await response.text();
adSelectors = AdRemover.parseEasyListForSelectors(text);
console.log(`Loaded ${adSelectors.length} ad selectors from EasyList`);
} catch (error) {
console.error('Failed to load EasyList:', error);
// Fallback to basic selectors if EasyList fails to load
adSelectors = [
'[class*="ad-container"]',
'[class*="adsbygoogle"]',
'[id*="div-gpt-ad"]',
'.advertisement',
'iframe[src*="doubleclick.net"]',
'iframe[src*="googlesyndication.com"]'
];
}
}
// Load the selectors immediately
loadEasyList();
// Update the message listener to capture sender
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'PAGE_DATA') {
// Pass sender to processPageData
processPageData(message.data, sender)
.then(() => sendResponse({ success: true }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Indicates async response
}
return false;
});
// Helper to proxy logs to content.js
function proxyConsole(tabId, method, message) {
console[method](message);
chrome.tabs.sendMessage(tabId, { type: 'LOG', level: method, message: message });
}
// Add this function to send file status updates to the content script
function updateFileStatus(tabId, url, status, reason = null) {
chrome.tabs.sendMessage(tabId, {
type: 'FILE_STATUS',
url: url,
status: status, // 'success', 'skipped', or 'failed'
reason: reason // Optional explanation message
});
}
// Update function signature to accept sender
async function processPageData(data, sender) {
const { domain, html, resources, url } = data;
const tabId = sender.tab.id;
const zip = new JSZip();
const domainFolder = zip.folder(domain);
// Get user settings
const settings = await new Promise(resolve => {
chrome.storage.sync.get({
maxResourceSize: 30,
maxTotalSize: 120,
downloadCss: true,
downloadJs: true,
downloadImages: true,
downloadFonts: true,
downloadVideos: true,
removeAds: false
}, resolve);
});
proxyConsole(tabId, 'log', `Settings loaded: Max Resource: ${settings.maxResourceSize}MB, Remove Ads: ${settings.removeAds}`);
// Determine the HTML filename based on the URL
let htmlFilename = "index.html";
try {
const parsedUrl = new URL(url);
const pathname = parsedUrl.pathname;
if (pathname.length > 1 && pathname.endsWith('.html')) {
htmlFilename = pathname.split('/').pop();
}
} catch (error) {
proxyConsole(tabId, 'warn', "Couldn't parse URL, using index.html as default");
}
// Process the HTML content
let modifiedHtml = html;
// Save original HTML for reference
domainFolder.file("original.html", html);
// Apply ad removing if enabled
if (settings.removeAds && adSelectors) {
proxyConsole(tabId, 'log', `Removing ads from HTML using ${adSelectors.length} EasyList selectors...`);
try {
const startTime = performance.now();
// Pass the adSelectors to removeAdsFromHTML
modifiedHtml = AdRemover.removeAdsFromHTML(modifiedHtml, adSelectors);
const endTime = performance.now();
proxyConsole(tabId, 'log', `Ads removed in ${(endTime - startTime).toFixed(2)}ms`);
} catch (error) {
proxyConsole(tabId, 'error', `Error removing ads: ${error.message}`);
}
}
// Apply other HTML modifications
modifiedHtml = modifyHTML(modifiedHtml);
// Save the processed HTML
domainFolder.file(htmlFilename, modifiedHtml);
// Process resources
let processedCount = 0;
const totalResources = resources.length;
chrome.action.setBadgeText({ text: "0%" });
chrome.action.setBadgeBackgroundColor({ color: "#4688F1" });
// Notify content script that download has started
chrome.tabs.sendMessage(tabId, { type: 'DOWNLOAD_STARTED' });
// Load all settings (max sizes and resource type options)
chrome.storage.sync.get({
maxResourceSize: 30,
maxTotalSize: 120,
downloadCss: true,
downloadJs: true,
downloadImages: true,
downloadFonts: true,
downloadVideos: true
}, function (settings) {
const MAX_RESOURCE_SIZE_MB = settings.maxResourceSize;
const MAX_TOTAL_SIZE_MB = settings.maxTotalSize;
proxyConsole(tabId, 'log', `Settings loaded: Max Resource Size: ${MAX_RESOURCE_SIZE_MB}MB, Max Total Size: ${MAX_TOTAL_SIZE_MB}MB`);
(async function processResources() {
let totalSize = 0;
// Notify content script that download has started
chrome.tabs.sendMessage(tabId, { type: 'DOWNLOAD_STARTED' });
for (const res of resources) {
try {
// Define resource types and their handling rules
const resourceTypes = {
css: { folder: "css", extension: ".css", sameDomainOnly: false },
js: { folder: "js", extension: ".js", sameDomainOnly: true },
image: { folder: "images", extension: null, sameDomainOnly: false },
font: { folder: "fonts", extension: null, sameDomainOnly: false },
video: { folder: "videos", extension: null, sameDomainOnly: true }
};
let type = resourceTypes[res.type];
if (!type) continue; // Skip unsupported resource types
// Check if the user disabled this resource type
if (res.type === 'css' && !settings.downloadCss) {
// proxyConsole(tabId, 'warn', `User disabled CSS downloads: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'CSS disabled by user');
continue;
}
if (res.type === 'js' && !settings.downloadJs) {
// proxyConsole(tabId, 'warn', `User disabled JS downloads: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'JS disabled by user');
continue;
}
if (res.type === 'image' && !settings.downloadImages) {
// proxyConsole(tabId, 'warn', `User disabled Images downloads: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'Images disabled by user');
continue;
}
if (res.type === 'font' && !settings.downloadFonts) {
// proxyConsole(tabId, 'warn', `User disabled Fonts downloads: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'Fonts disabled by user');
continue;
}
if (res.type === 'video' && !settings.downloadVideos) {
// proxyConsole(tabId, 'warn', `User disabled Videos downloads: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'Videos disabled by user');
continue;
}
// Check domain restriction for types that require same-domain only
if (type.sameDomainOnly && res.url.startsWith("http")) {
// Normalize domains for comparison by removing www prefix
const resourceHost = new URL(res.url).hostname.replace(/^www\./, '');
const pageHost = domain.replace(/^www\./, '');
// Use the proxyConsole function to make this visible in the content script
proxyConsole(tabId, 'log', `Domain check: ${resourceHost} vs ${pageHost}`);
if (resourceHost !== pageHost) {
// proxyConsole(tabId, 'warn', `Skipping cross-domain resource: ${res.url}`);
updateFileStatus(tabId, res.url, 'skipped', 'Cross-domain resource');
continue;
}
}
// Process filename
let filename = res.filename || res.url.split('/').pop().split('?')[0];
// Create a more unique filename to prevent collisions
if (res.type === 'image') {
// ...existing code for image files...
}
// Don't add folder prefix to JS files
else if (res.type === 'js') {
// Just use the filename as is, without the domain folder prefix
filename = filename.split('?')[0];
}
// Special handling for Next.js images
if (res.url.includes('/_next/image') && res.url.includes('url=')) {
try {
// Extract the encoded URL parameter
const urlMatch = res.url.match(/url=([^&]+)/);
if (urlMatch && urlMatch[1]) {
// Decode the URL
const decodedUrl = decodeURIComponent(urlMatch[1]);
// Extract filename from the decoded URL
const nextjsFilename = decodedUrl.split('/').pop().split('?')[0];
if (nextjsFilename) {
filename = nextjsFilename;
}
}
} catch (e) {
proxyConsole(tabId, 'warn', `Error extracting Next.js image name: ${e.message}`);
}
}
// More robust fetch with timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000);
try {
const response = await fetch(res.url, {
signal: controller.signal,
credentials: 'omit',
cache: 'force-cache'
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP error ${response.status}: ${response.statusText}`);
}
const blob = await response.blob();
// Get content type from response
const contentType = response.headers.get('content-type');
// Determine proper file extension based on content type and actual file content
if (!filename.includes('.')) {
// No extension in filename, need to add one
if (res.type === 'image') {
// For images, try to determine from content type first
if (contentType) {
const extensionMap = {
'image/jpeg': '.jpg',
'image/png': '.png',
'image/gif': '.gif',
'image/svg+xml': '.svg',
'image/webp': '.webp',
'image/avif': '.avif',
'image/bmp': '.bmp',
'image/x-icon': '.ico',
'image/vnd.microsoft.icon': '.ico'
};
const extension = extensionMap[contentType.split(';')[0]];
if (extension) {
filename += extension;
} else {
// If content type doesn't help, try to examine the file header
try {
const fileType = await getImageType(blob);
if (fileType === 'image/jpeg') filename += '.jpg';
else if (fileType === 'image/png') filename += '.png';
else if (fileType === 'image/gif') filename += '.gif';
else if (fileType === 'image/bmp') filename += '.bmp';
else filename += '.jpg'; // Default if all else fails
} catch (e) {
filename += '.jpg'; // Default if examination fails
}
}
} else {
// Try to examine the file header if no content type
try {
const fileType = await getImageType(blob);
if (fileType === 'image/jpeg') filename += '.jpg';
else if (fileType === 'image/png') filename += '.png';
else if (fileType === 'image/gif') filename += '.gif';
else if (fileType === 'image/bmp') filename += '.bmp';
else filename += '.jpg'; // Default if all else fails
} catch (e) {
filename += '.jpg'; // Default if examination fails
}
}
} else if (res.type === 'font') {
// For fonts, add proper extension based on content type or URL pattern
if (res.url.includes('.woff2')) {
filename += '.woff2';
} else if (res.url.includes('.woff')) {
filename += '.woff';
} else if (res.url.includes('.ttf')) {
filename += '.ttf';
} else if (res.url.includes('.otf')) {
filename += '.otf';
} else if (res.url.includes('.eot')) {
filename += '.eot';
} else {
// Try content type as a fallback
if (contentType && contentType.includes('font/woff2')) {
filename += '.woff2';
} else if (contentType && contentType.includes('font/woff')) {
filename += '.woff';
} else if (contentType && contentType.includes('font/ttf')) {
filename += '.ttf';
} else {
filename += '.woff2'; // Default font extension
}
}
} else if (res.type === 'css') {
filename += '.css';
} else if (res.type === 'js') {
filename += '.js';
} else if (res.type === 'video') {
// For videos, try to determine from content type
if (contentType) {
if (contentType.includes('video/mp4')) {
filename += '.mp4';
} else if (contentType.includes('video/webm')) {
filename += '.webm';
} else {
filename += '.mp4'; // Default video extension
}
} else {
filename += '.mp4'; // Default if content type is missing
}
}
}
// Handle fonts specifically with proper file detection
if (contentType && contentType.includes('font/')) {
// Override the resource type to font regardless of original detection
type = resourceTypes["font"];
// Fix extension if needed
if (!filename.match(/\.(woff2?|ttf|otf|eot)$/i)) {
if (contentType.includes('font/woff2')) {
filename = filename.replace(/\.[^.]+$/, '') + '.woff2';
} else if (contentType.includes('font/woff')) {
filename = filename.replace(/\.[^.]+$/, '') + '.woff';
} else if (contentType.includes('font/ttf')) {
filename = filename.replace(/\.[^.]+$/, '') + '.ttf';
} else {
filename = filename.replace(/\.[^.]+$/, '') + '.woff2';
}
}
}
proxyConsole(tabId, 'log', `File processed with correct extension: ${filename}`);
// Check individual resource size
if (blob.size > MAX_RESOURCE_SIZE_MB * 1024 * 1024) {
proxyConsole(tabId, 'warn', `Skipping ${res.url}: exceeds maximum resource size`);
updateFileStatus(tabId, res.url, 'skipped', `Exceeds max size limit (${MAX_RESOURCE_SIZE_MB}MB)`);
continue;
}
// Track total size
totalSize += blob.size;
if (totalSize > MAX_TOTAL_SIZE_MB * 1024 * 1024) {
proxyConsole(tabId, 'warn', `Total size exceeds limit of ${MAX_TOTAL_SIZE_MB}MB`);
}
const subFolder = domainFolder.folder(type.folder);
subFolder.file(filename, blob);
proxyConsole(tabId, 'log', `Successfully added to ZIP: ${filename}`);
updateFileStatus(tabId, res.url, 'success');
} catch (fetchError) {
clearTimeout(timeoutId);
proxyConsole(tabId, 'error', `Failed to fetch ${res.url}: ${fetchError.message}`);
updateFileStatus(tabId, res.url, 'failed', fetchError.message);
continue;
}
// Update progress after each resource
processedCount++;
const percentage = Math.round((processedCount / totalResources) * 100);
chrome.action.setBadgeText({ text: `${percentage}%` });
chrome.tabs.sendMessage(tabId, {
type: 'DOWNLOAD_PROGRESS',
percentage: percentage
});
} catch (err) {
proxyConsole(tabId, 'error', `Error processing resource ${res.url}: ${err.message}`);
}
}
// Generate a descriptive filename for the ZIP
const zipFilename = getZipFilename(domain, url);
try {
const content = await zip.generateAsync({ type: "blob" });
const dataUrl = await blobToDataURL(content);
downloadURL(zipFilename, dataUrl);
chrome.action.setBadgeText({ text: "" });
chrome.tabs.sendMessage(tabId, {
type: 'DOWNLOAD_COMPLETE',
filename: zipFilename
});
} catch (err) {
proxyConsole(tabId, 'error', `Error generating ZIP file: ${err.message}`);
}
})();
});
}
// Convert blob to data URL using FileReader
function blobToDataURL(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
// Use chrome.downloads.download API to trigger the download
function downloadURL(filename, url) {
chrome.downloads.download({
url: url,
filename: filename,
saveAs: false
}, (downloadId) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
}
// Helper function to generate a descriptive ZIP filename
function getZipFilename(domain, url) {
try {
const parsedUrl = new URL(url);
const pathname = parsedUrl.pathname;
// If it's the homepage or just a slash
if (pathname === "/" || pathname === "") {
return `${domain}.zip`;
}
// Remove extensions and clean up the pathname
let pageName = pathname.split('/').pop().split('.')[0];
// If we have a page name, add it to the filename
if (pageName && pageName.length > 0) {
return `${domain}-${pageName}.zip`;
} else {
return `${domain}.zip`;
}
} catch (error) {
console.warn("Couldn't parse URL for ZIP filename, using domain only", error);
return `${domain}.zip`;
}
}
// Modifies the HTML string to update local references and strip analytics scripts
function modifyHTML(html) {
// Replace external CSS references with local paths
html = html.replace(/<link[^>]*rel=["']stylesheet["'][^>]*href=["']([^"']+)["'][^>]*>/gi, function (match, url) {
// Skip data URLs or empty URLs
if (url.startsWith('data:') || !url) return match;
// Extract filename from URL
const filename = url.split('/').pop().split('?')[0];
// Handle absolute URLs
if (url.startsWith('http')) {
return match.replace(url, `css/${filename}`);
}
// Handle relative URLs starting with /
else if (url.startsWith('/')) {
return match.replace(url, `css/${filename}`);
}
// Handle relative URLs without leading slash
else if (!url.startsWith('css/')) {
return match.replace(url, `css/${url}`);
}
return match;
});
// Replace external JS references with local paths (self-closing script tags)
html = html.replace(/<script[^>]*src=["']([^"']+)["'][^>]*><\/script>/gi, function (match, url) {
// Skip analytics scripts but don't remove the script tag
if (url.includes('googletagmanager.com') || url.includes('clarity.ms')) {
return match;
}
if (!url) return match;
// Extract filename from URL
const filename = url.split('/').pop().split('?')[0];
// Handle absolute URLs
if (url.startsWith('http')) {
return match.replace(url, `js/${filename}`);
}
// Handle relative URLs starting with /
else if (url.startsWith('/')) {
return match.replace(url, `js/${filename}`);
}
// Handle relative URLs without leading slash
else if (!url.startsWith('js/')) {
return match.replace(url, `js/${url}`);
}
return match;
});
// Handle standalone script tags with src attribute directly
html = html.replace(/<script[^>]*src=["']([^"']+)["'][^>]*>[\s\S]*?<\/script>/gi, function (match, url) {
// Skip analytics scripts but don't remove the script tag
if (url.includes('googletagmanager.com') || url.includes('clarity.ms')) {
return match;
}
if (!url) return match;
// Extract filename from URL
const filename = url.split('/').pop().split('?')[0];
// Handle absolute URLs
if (url.startsWith('http')) {
return match.replace(url, `js/${filename}`);
}
// Handle relative URLs starting with /
else if (url.startsWith('/')) {
return match.replace(url, `js/${filename}`);
}
// Handle relative URLs without leading slash
else if (!url.startsWith('js/')) {
return match.replace(url, `js/${url}`);
}
return match;
});
// Instead of removing inline scripts that contain certain patterns, just keep them
// We only want to modify the resource references, not alter functionality
// Update favicon links
html = html.replace(/<link[^>]*rel=["'](icon|shortcut icon|apple-touch-icon|apple-touch-icon-precomposed)["'][^>]*href=["']([^"']+)["'][^>]*>/gi, function (match, rel, url) {
if (!url) return match;
// Extract filename
const filename = url.split('/').pop().split('?')[0];
// Handle absolute URLs
if (url.startsWith('http')) {
return match.replace(url, `images/${filename}`);
}
// Handle relative URLs starting with /
else if (url.startsWith('/')) {
return match.replace(url, `images/${filename}`);
}
// Handle relative URLs without leading slash
else if (!url.startsWith('images/')) {
return match.replace(url, `images/${url}`);
}
return match;
});
// Replace image src paths with the new naming convention
html = html.replace(/<img[^>]*src=["']([^"']+)["'][^>]*>/gi, function (match, url) {
// Skip data URLs
if (url.startsWith('data:')) return match;
if (!url) return match;
try {
// Extract just the filename without parent directory prefixing
const filename = url.split('/').pop().split('?')[0];
// For assets that shouldn't be renamed with prefixes
return match.replace(url, `images/${filename}`);
} catch (e) {
// If URL parsing fails, use a simple approach
const filename = url.split('/').pop().split('?')[0];
return match.replace(url, `images/${filename}`);
}
});
// Update audio sources
html = html.replace(/<source[^>]*src=["']([^"']+)["'][^>]*>/gi, function (match, url) {
if (!url) return match;
// Extract filename
const filename = url.split('/').pop().split('?')[0];
// Handle different types of media
if (url.includes('.mp3') || url.includes('.wav') || url.includes('.ogg')) {
// Audio files
if (url.startsWith('http')) {
return match.replace(url, `audio/${filename}`);
} else if (url.startsWith('/')) {
return match.replace(url, `audio/${filename}`);
} else if (!url.startsWith('audio/')) {
return match.replace(url, `audio/${url}`);
}
} else if (url.includes('.mp4') || url.includes('.webm') || url.includes('.ogv')) {
// Video files
if (url.startsWith('http')) {
return match.replace(url, `videos/${filename}`);
} else if (url.startsWith('/')) {
return match.replace(url, `videos/${filename}`);
} else if (!url.startsWith('videos/')) {
return match.replace(url, `videos/${url}`);
}
}
return match;
});
// Handle video elements with src attribute directly
html = html.replace(/<video[^>]*src=["']([^"']+)["'][^>]*>/gi, function (match, url) {
if (!url) return match;
// Extract filename
const filename = url.split('/').pop().split('?')[0];
// Video files
if (url.startsWith('http')) {
return match.replace(url, `videos/${filename}`);
} else if (url.startsWith('/')) {
return match.replace(url, `videos/${filename}`);
} else if (!url.startsWith('videos/')) {
return match.replace(url, `videos/${url}`);
}
return match;
});
// Update background image paths in inline styles to point to the local images folder
html = html.replace(/url\(["']?([^"')]+)["']?\)/g, function (match, p1) {
if (p1.startsWith("data:")) return match;
if (!p1) return match;
try {
// Extract just the filename without parent directory prefixing
const filename = p1.split('/').pop().split('?')[0];
return `url('images/${filename}')`;
} catch (e) {
// If URL parsing fails, use the original approach
const filename = p1.split("/").pop().split("?")[0];
return `url('images/${filename}')`;
}
});
// Handle any other link tags with href that might be for resources
html = html.replace(/<link[^>]*href=["']([^"']+\.(woff2?|ttf|otf|eot|png|jpg|jpeg|gif|svg|webp))["'][^>]*>/gi, function (match, url) {
if (!url) return match;
// Extract filename
const filename = url.split('/').pop().split('?')[0];
// Determine appropriate folder based on file extension
let folder = "images"; // Default
if (/\.(woff2?|ttf|otf|eot)$/i.test(url)) {
folder = "fonts";
}
if (url.startsWith('http') || url.startsWith('/')) {
return match.replace(url, `${folder}/${filename}`);
} else if (!url.startsWith(`${folder}/`)) {
return match.replace(url, `${folder}/${filename}`);
}
return match;
});
return html;
}