async function loadResourcesFromHeaders(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const headers = response.headers;
// Example: Load a CSS file
if (headers.get('Content-Type')?.startsWith('text/css')) {
const cssUrl = headers.get('url') || '';
if (cssUrl) {
const cssResponse = await fetch(cssUrl);
if (cssResponse.ok) {
const cssText = await cssResponse.text();
// Inject CSS into the document (example)
const style = document.createElement('style');
style.textContent = cssText;
document.head.appendChild(style);
} else {
console.error('Failed to load CSS:', cssUrl);
}
}
}
// Example: Load a JavaScript file
if (headers.get('Content-Type')?.startsWith('application/javascript')) {
const jsUrl = headers.get('url') || '';
if (jsUrl) {
const jsResponse = await fetch(jsUrl);
if (jsResponse.ok) {
const jsCode = await jsResponse.text();
// Execute JavaScript (example)
eval(jsCode);
} else {
console.error('Failed to load JS:', jsUrl);
}
}
}
// Add more resource loading logic based on Content-Type and header keys
} catch (error) {
console.error('Error loading resources:', error);
}
}
Add your comment