1. async function loadResourcesFromHeaders(url) {
  2. try {
  3. const response = await fetch(url);
  4. if (!response.ok) {
  5. throw new Error(`HTTP error! status: ${response.status}`);
  6. }
  7. const headers = response.headers;
  8. // Example: Load a CSS file
  9. if (headers.get('Content-Type')?.startsWith('text/css')) {
  10. const cssUrl = headers.get('url') || '';
  11. if (cssUrl) {
  12. const cssResponse = await fetch(cssUrl);
  13. if (cssResponse.ok) {
  14. const cssText = await cssResponse.text();
  15. // Inject CSS into the document (example)
  16. const style = document.createElement('style');
  17. style.textContent = cssText;
  18. document.head.appendChild(style);
  19. } else {
  20. console.error('Failed to load CSS:', cssUrl);
  21. }
  22. }
  23. }
  24. // Example: Load a JavaScript file
  25. if (headers.get('Content-Type')?.startsWith('application/javascript')) {
  26. const jsUrl = headers.get('url') || '';
  27. if (jsUrl) {
  28. const jsResponse = await fetch(jsUrl);
  29. if (jsResponse.ok) {
  30. const jsCode = await jsResponse.text();
  31. // Execute JavaScript (example)
  32. eval(jsCode);
  33. } else {
  34. console.error('Failed to load JS:', jsUrl);
  35. }
  36. }
  37. }
  38. // Add more resource loading logic based on Content-Type and header keys
  39. } catch (error) {
  40. console.error('Error loading resources:', error);
  41. }
  42. }

Add your comment