Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ interface ContentSettings {

interface ContentSiteSettings {
highlightAllWords?: boolean;
disableCopyOnHover?: boolean;
}

class HoverTranslator {
private isEnabled = true;

private translations: ContentTranslationData = {};
private targetUrls: string[] = [];
private delay: number = 300;
private hoverTimeout: number | null = null;
private currentHoveredElement: HTMLElement | null = null;
private tooltip: HTMLElement | null = null;
Expand Down Expand Up @@ -95,6 +97,7 @@ class HoverTranslator {
this.translations = translations;
this.targetUrls = result.targetUrls || [];
this.isEnabled = result.isEnabled !== false;
this.delay = result.delay ?? 300;
this.siteSettings = result.siteSettings || {};
this.currentDomain = window.location.hostname;
} catch (error) {
Expand Down Expand Up @@ -186,13 +189,22 @@ class HoverTranslator {
const allTranslations = this.findAllTranslations(text);

if (allTranslations.length > 0) {
// Retirer l'ancienne bordure et ajouter la nouvelle
// Annuler le timeout précédent s'il existe
if (this.hoverTimeout) {
clearTimeout(this.hoverTimeout);
this.hoverTimeout = null;
}

// Retirer l'ancienne bordure immédiatement
this.removeTranslationBorder();
this.addMultipleTranslationBorders(target, text, allTranslations);

// Afficher le tooltip avec toutes les traductions
const tooltipText = this.formatMultipleTranslations(allTranslations);
this.showTooltip(tooltipText, event);
// Utiliser un timeout pour afficher les bordures, surlignages et tooltip après le délai
this.hoverTimeout = window.setTimeout(() => {
this.addMultipleTranslationBorders(target, text, allTranslations);

const tooltipText = this.formatMultipleTranslations(allTranslations);
this.showTooltipImmediate(tooltipText, event);
}, this.delay);
}
}

Expand Down Expand Up @@ -241,6 +253,12 @@ class HoverTranslator {
target.hasAttribute("data-auto-highlight") ||
target.hasAttribute("data-hover-translator-highlight")
) {
// Vérifier si le copier-coller est désactivé pour ce site
const currentSiteSettings = this.siteSettings[this.currentDomain];
if (currentSiteSettings?.disableCopyOnHover) {
return;
}

event.preventDefault();
event.stopPropagation();

Expand Down Expand Up @@ -498,18 +516,12 @@ class HoverTranslator {
});
}

private showTooltip(translation: string, event: MouseEvent): void {
if (this.hoverTimeout) {
clearTimeout(this.hoverTimeout);
private showTooltipImmediate(translation: string, event: MouseEvent): void {
if (this.tooltip) {
this.tooltip.textContent = translation;
this.tooltip.style.opacity = "1";
this.positionTooltip(event);
}

this.hoverTimeout = window.setTimeout(() => {
if (this.tooltip) {
this.tooltip.textContent = translation;
this.tooltip.style.opacity = "1";
this.positionTooltip(event);
}
}, 300);
}

private hideTooltip(): void {
Expand Down
5 changes: 5 additions & 0 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ <h1>🔤 Traducteur personnalisé au survol</h1>
<span class="checkbox-custom"></span>
<span class="checkbox-text">Surligner tous les mots connus</span>
</label>
<label class="checkbox-label">
<input type="checkbox" id="disableCopyOnHover" class="checkbox-input">
<span class="checkbox-custom"></span>
<span class="checkbox-text">Désactiver le copier-coller au hover</span>
</label>
</div>
</div>

Expand Down
30 changes: 25 additions & 5 deletions src/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface PopupSettings {

interface SiteSettings {
highlightAllWords?: boolean;
disableCopyOnHover?: boolean;
}

interface PopupElements {
Expand All @@ -23,6 +24,7 @@ interface PopupElements {
removeCurrentSite: HTMLButtonElement;
currentSiteUrl: HTMLInputElement;
highlightAllWords: HTMLInputElement;
disableCopyOnHover: HTMLInputElement;
infoSection: HTMLDivElement;
siteOptionsSection: HTMLDivElement;
}
Expand Down Expand Up @@ -60,6 +62,9 @@ class PopupManager {
highlightAllWords: document.getElementById(
"highlightAllWords"
) as HTMLInputElement,
disableCopyOnHover: document.getElementById(
"disableCopyOnHover"
) as HTMLInputElement,
infoSection: document.getElementById("infoSection") as HTMLDivElement,
siteOptionsSection: document.getElementById(
"siteOptionsSection"
Expand All @@ -84,6 +89,10 @@ class PopupManager {
this.elements.highlightAllWords.addEventListener("change", () => {
this.updateSiteSettings();
});

this.elements.disableCopyOnHover.addEventListener("change", () => {
this.updateSiteSettings();
});
} catch (error) {
console.error(
"❌ Erreur lors de la liaison des événements popup:",
Expand Down Expand Up @@ -142,6 +151,8 @@ class PopupManager {
const currentSiteSettings = siteSettings[currentDomain] || {};
this.elements.highlightAllWords.checked =
currentSiteSettings.highlightAllWords || false;
this.elements.disableCopyOnHover.checked =
currentSiteSettings.disableCopyOnHover || false;

// Afficher le statut spécifique au site actuel
if (isActiveOnCurrentSite) {
Expand Down Expand Up @@ -366,6 +377,8 @@ class PopupManager {
siteSettings[domain] ??= {};
siteSettings[domain].highlightAllWords =
this.elements.highlightAllWords.checked;
siteSettings[domain].disableCopyOnHover =
this.elements.disableCopyOnHover.checked;

// Sauvegarder
await chrome.storage.sync.set({ siteSettings });
Expand All @@ -380,11 +393,18 @@ class PopupManager {
await chrome.tabs.reload(activeTab.id);
}

this.showNotification(
this.elements.highlightAllWords.checked
? "Surlignage automatique activé"
: "Surlignage automatique désactivé"
);
const messages: string[] = [];
if (this.elements.highlightAllWords.checked) {
messages.push("Surlignage automatique activé");
} else {
messages.push("Surlignage automatique désactivé");
}
if (this.elements.disableCopyOnHover.checked) {
messages.push("Copier-coller au hover désactivé");
} else {
messages.push("Copier-coller au hover activé");
}
this.showNotification(messages.join(" - "));
} catch (error) {
console.error("Erreur lors de la mise à jour des paramètres:", error);
this.showNotification("Erreur lors de la mise à jour");
Expand Down