172 lines
4.5 KiB
JavaScript
172 lines
4.5 KiB
JavaScript
// Création de la boîte d'information
|
|
const infoBox = document.createElement('div');
|
|
infoBox.id = 'info-box';
|
|
infoBox.innerHTML = `
|
|
<div id="info-toggle">
|
|
<label>
|
|
<input type="checkbox" id="toggle-display"> <span id="toggle_hide_text">Masquer</span>
|
|
</label>
|
|
</div>
|
|
<div id="info-content">
|
|
<div><strong>Parent :</strong> <span id="element-parent"></span></div>
|
|
<div><strong>Enfant :</strong> <span id="element-info"></span></div>
|
|
</div>
|
|
`;
|
|
|
|
// Ajout des styles
|
|
const style = document.createElement('style');
|
|
style.textContent = `
|
|
#info-box {
|
|
position: fixed;
|
|
top: 20px;
|
|
right: 20px;
|
|
background: #333;
|
|
color: white;
|
|
padding: 15px 20px;
|
|
border-radius: 8px;
|
|
min-width: 250px;
|
|
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
|
|
font-size: 14px;
|
|
display: none;
|
|
z-index: 9999;
|
|
}
|
|
|
|
#info-box.visible {
|
|
display: block;
|
|
}
|
|
|
|
#info-box div {
|
|
margin: 5px 0;
|
|
}
|
|
|
|
#info-box strong {
|
|
color: #4CAF50;
|
|
}
|
|
|
|
#info-toggle {
|
|
margin-bottom: 10px;
|
|
padding-bottom: 10px;
|
|
border-bottom: 1px solid #555;
|
|
}
|
|
|
|
#info-box.no_infos {
|
|
max-width: 200px;
|
|
display: block;
|
|
overflow: hidden;
|
|
min-width: auto;
|
|
}
|
|
|
|
#info-box.no_infos {
|
|
#info-toggle {
|
|
border-bottom: 0px solid #555;
|
|
padding: 0;
|
|
margin: 0;
|
|
}
|
|
#toggle_hide_text {
|
|
display: none;
|
|
}
|
|
}
|
|
#info-toggle label {
|
|
cursor: pointer;
|
|
user-select: none;
|
|
}
|
|
|
|
#info-content.hidden {
|
|
display: none;
|
|
}
|
|
`;
|
|
|
|
// Injection dans le DOM
|
|
document.head.appendChild(style);
|
|
document.body.appendChild(infoBox);
|
|
|
|
// Récupération des éléments d'affichage
|
|
const elementInfo = document.getElementById('element-info');
|
|
const elementParent = document.getElementById('element-parent');
|
|
const toggleCheckbox = document.getElementById('toggle-display');
|
|
const infoContent = document.getElementById('info-content');
|
|
|
|
// Clé pour le localStorage
|
|
const STORAGE_KEY = 'info-box-hidden-state';
|
|
|
|
// Fonction pour appliquer l'état
|
|
function applyToggleState(isHidden) {
|
|
if (isHidden) {
|
|
infoContent.classList.add('hidden');
|
|
infoBox.classList.add('no_infos');
|
|
toggleCheckbox.checked = true;
|
|
} else {
|
|
infoContent.classList.remove('hidden');
|
|
infoBox.classList.remove('no_infos');
|
|
toggleCheckbox.checked = false;
|
|
}
|
|
}
|
|
|
|
// Charger l'état depuis localStorage au démarrage
|
|
const savedState = localStorage.getItem(STORAGE_KEY);
|
|
if (savedState !== null) {
|
|
applyToggleState(savedState === 'true');
|
|
}
|
|
|
|
// Gestion du toggle
|
|
toggleCheckbox.addEventListener('change', function() {
|
|
const isHidden = this.checked;
|
|
applyToggleState(isHidden);
|
|
// Sauvegarder l'état dans localStorage
|
|
localStorage.setItem(STORAGE_KEY, isHidden);
|
|
});
|
|
|
|
// Écouter l'événement mouseover sur tout le document
|
|
document.addEventListener('mouseover', function(e) {
|
|
const el = e.target;
|
|
|
|
// Ignorer la boîte d'info elle-même
|
|
if (el.closest('#info-box')) {
|
|
return;
|
|
}
|
|
|
|
// Récupérer les informations
|
|
const tag = el.tagName.toLowerCase();
|
|
const id = el.id;
|
|
const classes = el.className;
|
|
|
|
// Construire la chaîne au format element#id.class
|
|
let infoStr = tag;
|
|
if (id) {
|
|
infoStr += '#' + id;
|
|
}
|
|
if (classes) {
|
|
// Remplacer les espaces par des points pour les classes multiples
|
|
infoStr += '.' + classes.trim().replace(/\s+/g, '.');
|
|
}
|
|
|
|
// Construire la chaîne parent au format element#id.class
|
|
let parentStr = '(aucun)';
|
|
if (el.parentElement) {
|
|
const parentTag = el.parentElement.tagName.toLowerCase();
|
|
const parentId = el.parentElement.id;
|
|
const parentClasses = el.parentElement.className;
|
|
|
|
parentStr = parentTag;
|
|
if (parentId) {
|
|
parentStr += '#' + parentId;
|
|
}
|
|
if (parentClasses) {
|
|
parentStr += '.' + parentClasses.trim().replace(/\s+/g, '.');
|
|
}
|
|
}
|
|
|
|
// Afficher les informations
|
|
elementInfo.textContent = infoStr;
|
|
elementParent.textContent = parentStr;
|
|
|
|
// Rendre la boîte visible
|
|
infoBox.classList.add('visible');
|
|
});
|
|
|
|
// Optionnel : cacher la boîte quand on quitte la page
|
|
document.addEventListener('mouseout', function(e) {
|
|
if (e.relatedTarget === null) {
|
|
infoBox.classList.remove('visible');
|
|
}
|
|
}); |