(function() {
// Function to synchronize resources (e.g., CSS, JS) across HTML documents
function syncResources() {
const documents = document.getElementsByTagName('html'); // Get all HTML documents
if (documents.length < 2) {
console.warn("Less than 2 HTML documents found. No synchronization needed.");
return;
}
for (let i = 0; i < documents.length; i++) {
for (let j = i + 1; j < documents.length; j++) {
const doc1 = documents[i];
const doc2 = documents[j];
// Get all links to external resources in doc1
const links1 = Array.from(doc1.getElementsByTagName('link'));
const scripts1 = Array.from(doc1.getElementsByTagName('script'));
// Get all links to external resources in doc2
const links2 = Array.from(doc2.getElementsByTagName('link'));
const scripts2 = Array.from(doc2.getElementsByTagName('script'));
// Synchronize links
for (const link of links1) {
const href = link.href;
const found = links2.some(l => l.href === href);
if (!found) {
// Add the link to doc2 if it doesn't exist
const newLink = document.createElement('link');
newLink.href = href;
newLink.rel = link.rel;
newLink.type = link.type;
doc2.head.appendChild(newLink);
console.log(`Linked resource ${href} added to document ${j + 1}`);
}
}
// Synchronize scripts
for (const script of scripts1) {
const src = script.src;
const found = scripts2.some(s => s.src === src);
if (!found) {
// Add the script to doc2 if it doesn't exist
const newScript = document.createElement('script');
newScript.src = src;
newScript.async = script.async;
newScript.defer = script.defer;
doc2.body.appendChild(newScript);
console.log(`Script ${src} added to document ${j + 1}`);
}
}
}
}
}
// Run the synchronization function
syncResources();
})();
Add your comment