"ignore" and "once" attributes, improved performance

This commit is contained in:
Arsen Mirzaev Tatyano-Muradovich 2023-10-06 04:50:20 +07:00
parent f4d37ebf3a
commit 6074da9a52

View File

@ -1,181 +1,244 @@
"use strict"; "use strict";
if (typeof window.reinitializer !== "function") { if (typeof window.reinitializer !== "function") {
// Not initialized // Not initialized
// Initialize of the class in global namespace // Initialize of the class in global namespace
window.reinitializer = class reinitializer { window.reinitializer = class reinitializer {
/** /**
* Parent element for location <link> elements * Parent element for location <link> elements
*/ */
css = document.head; css = document.head;
/** /**
* Parent element for location <script> elements * Parent element for location <script> elements
*/ */
js = document.body; js = document.body;
/** /**
* Target element for searching new <link> and <script> elements * Target element for searching new <link> and <script> elements
*/ */
root = document.body.getElementsByTagName("main")[0]; root = document.body.getElementsByTagName("main")[0];
/** /**
* Instance of the observer * Instance of the observer
*/ */
observer = new MutationObserver(() => this.handle(this.root)); observer = new MutationObserver(() => this.handle(this.root));
/** /**
* Construct * Construct
* *
* @param {object} root Entry point * @param {object} root Entry point
*/ */
constructor(root) { constructor(root) {
// Initialize of the root element // Initialize of the root element
this.root = root ?? this.root; this.root = root ?? this.root;
} }
/** /**
* Reinitialize <link> and <script> elements * Reinitialize <link> and <script> elements
* *
* @return {bool} Processing status * @return {bool} Processing status
*/ */
handle() { handle() {
// Check for a dublicate execute launch // Check for a dublicate execute launch
if (this.started) return false; if (this.started) return false;
// Initialization an observation status // Initialization an observation status
this.started = true; this.started = true;
for ( for (
let links; let links;
(links = this.root.getElementsByTagName("link")).length > 0; (links = this.root.getElementsByTagName("link")).length > 0;
) { ) {
// Enumeration <link> elements // Enumeration <link> elements
// Initialization of the <link> element // Initialization of the <link> element
const link = links[0]; const link = links[0];
// Initialization link of the <link> element if (link.getAttribute("data-reinitializer-ignore") === "true") {
const href = link.getAttribute("href"); // Marked as ignored
// Stop listening // Move element
this.stop(); this.css.appendChild(link);
// Delete outdated <link> element from the document continue;
link.remove(); }
// Start listening // Initialization link of the <link> element
this.start(); const href = link.getAttribute("href");
// Deleting outdated elements if (
for ( link.getAttribute("data-reinitializer-once") === "true" &&
const element of this.css.querySelectorAll( this.css.querySelector(`:scope > link[href="${href}"]`)
`script[href="${href}"]`, ) {
) // Marked as executing once and already executed
) element.remove();
// Initialization of new <link> element // Stop listening
const element = document.createElement("link"); this.stop();
element.setAttribute("href", href);
element.setAttribute("rel", "stylesheet");
// Write new element // Delete outdated <link> element from the document
this.css.appendChild(element); link.remove();
}
for ( // Start listening
let scripts; this.start();
(scripts = this.root.getElementsByTagName("script")).length > 0;
) {
// Enumeration of <script> elements
// Initialization of the <script> element continue;
const script = scripts[0]; }
// Initialization link of the <script> element // Initialization outerHTML of the <script> element
const src = script.getAttribute("src"); const html = script.outerHTML;
// Initialization text of the <script> element // Stop listening
const text = script.textContent; this.stop();
// Stop listening // Delete outdated <link> element from the document
this.stop(); link.remove();
// Delete outdated <script> element from the document // Start listening
script.remove(); this.start();
// Start listening // Deleting outdated elements
this.start(); for (const element of this.css.querySelectorAll(
`:scope > link[href="${href}"]`
)) element.remove();
// Initialization of new <script> element // Initialization of new <link> element
const element = document.createElement("script"); const element = document.createElement("link");
element.setAttribute("href", href);
element.setAttribute("rel", "stylesheet");
if (typeof src === "string") { // Write new element
// File this.css.appendChild(element);
}
// Deleting outdated elements for (
for ( let scripts;
const element of this.js.querySelectorAll( (scripts = this.root.getElementsByTagName("script")).length > 0;
`script[src="${src}"]`, ) {
) // Enumeration of <script> elements
) element.remove();
// Copy link from outdated <script> element // Initialization of the <script> element
element.setAttribute("src", src); const script = scripts[0];
} else {
// Script
// Deleting outdated elements if (script.getAttribute("data-reinitializer-ignore") === "true") {
for ( // Marked as ignored
const element of Array.from(
this.js.getElementsByTagName("script"),
)
.filter((e) => e.textContent === text)
) {
element.remove();
}
// Copy text from outdated <script> element // Move element
element.textContent = text; this.js.appendChild(script);
}
// Write new <script> element to end of <body> element continue;
this.js.appendChild(element); }
}
// Initialize of observation status // Initialization link of the <script> element
this.started = false; const src = script.getAttribute("src");
// Return (success) // Initialization text of the <script> element
return true; const text = script.textContent;
}
/** if (
* Start observation script.getAttribute("data-reinitializer-once") === "true" &&
* (this.js.querySelector(`:scope > script[src="${src}"]`) ||
* @return {void} Array.from(this.js.querySelectorAll(`:scope > script`)).filter(
*/ (e) => e.textContent === text
start() { ).length > 0)
this.observer.observe(this.root, { ) {
childList: true, // Marked as executing once and already executed
});
}
/** // Stop listening
* Stop observation this.stop();
*
* @return {void} // Delete outdated <script> element from the document
*/ script.remove();
stop() {
this.observer.disconnect(); // Start listening
} this.start();
};
continue;
}
// Initialization outerHTML of the <script> element
const html = script.outerHTML;
// Stop listening
this.stop();
// Delete outdated <script> element from the document
script.remove();
// Start listening
this.start();
// Initialization of new <script> element
const element = document.createElement("script");
if (typeof src === "string") {
// File
// Deleting outdated elements
for (const element of this.js.querySelectorAll(
`:scope > script[src="${src}"]`
))
element.remove();
// Copy link from outdated <script> element
element.setAttribute("src", src);
// Write a type of <script> element
element.setAttribute('type', 'text/javascript');
} else {
// Script
// Deleting outdated elements
for (const element of Array.from(
this.js.querySelectorAll(`:scope > script`)
).filter((e) => e.textContent === text)) {
element.remove();
}
// Copy text from outdated <script> element
element.textContent = text;
}
// Write the new <script> element to end of <body> element
this.js.appendChild(element);
// Write content to the new <script> element
element.outerHTML = html;
}
// Initialize of observation status
this.started = false;
// Return (success)
return true;
}
/**
* Start observation
*
* @return {void}
*/
start() {
this.observer.observe(this.root, {
childList: true,
});
}
/**
* Stop observation
*
* @return {void}
*/
stop() {
this.observer.disconnect();
}
};
} }
// Вызов события: "инициализировано" // Вызов события: "инициализировано"
document.dispatchEvent( document.dispatchEvent(
new CustomEvent("reinitializer.initialized", { new CustomEvent("reinitializer.initialized", {
detail: { reinitializer: window.reinitializer }, detail: { reinitializer: window.reinitializer },
}), })
); );