1. (function() {
  2. // Function to synchronize resources (e.g., CSS, JS) across HTML documents
  3. function syncResources() {
  4. const documents = document.getElementsByTagName('html'); // Get all HTML documents
  5. if (documents.length < 2) {
  6. console.warn("Less than 2 HTML documents found. No synchronization needed.");
  7. return;
  8. }
  9. for (let i = 0; i < documents.length; i++) {
  10. for (let j = i + 1; j < documents.length; j++) {
  11. const doc1 = documents[i];
  12. const doc2 = documents[j];
  13. // Get all links to external resources in doc1
  14. const links1 = Array.from(doc1.getElementsByTagName('link'));
  15. const scripts1 = Array.from(doc1.getElementsByTagName('script'));
  16. // Get all links to external resources in doc2
  17. const links2 = Array.from(doc2.getElementsByTagName('link'));
  18. const scripts2 = Array.from(doc2.getElementsByTagName('script'));
  19. // Synchronize links
  20. for (const link of links1) {
  21. const href = link.href;
  22. const found = links2.some(l => l.href === href);
  23. if (!found) {
  24. // Add the link to doc2 if it doesn't exist
  25. const newLink = document.createElement('link');
  26. newLink.href = href;
  27. newLink.rel = link.rel;
  28. newLink.type = link.type;
  29. doc2.head.appendChild(newLink);
  30. console.log(`Linked resource ${href} added to document ${j + 1}`);
  31. }
  32. }
  33. // Synchronize scripts
  34. for (const script of scripts1) {
  35. const src = script.src;
  36. const found = scripts2.some(s => s.src === src);
  37. if (!found) {
  38. // Add the script to doc2 if it doesn't exist
  39. const newScript = document.createElement('script');
  40. newScript.src = src;
  41. newScript.async = script.async;
  42. newScript.defer = script.defer;
  43. doc2.body.appendChild(newScript);
  44. console.log(`Script ${src} added to document ${j + 1}`);
  45. }
  46. }
  47. }
  48. }
  49. }
  50. // Run the synchronization function
  51. syncResources();
  52. })();

Add your comment