mirror of
https://github.com/obsqrbtz/goose-highlighter.git
synced 2026-04-08 20:19:06 +03:00
chore: refactor
This commit is contained in:
106
src/content/ContentScript.ts
Normal file
106
src/content/ContentScript.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { HighlightList, MessageData } from '../types.js';
|
||||
import { StorageService } from '../services/StorageService.js';
|
||||
import { MessageService } from '../services/MessageService.js';
|
||||
import { HighlightEngine } from './HighlightEngine.js';
|
||||
import { DOMUtils } from '../utils/DOMUtils.js';
|
||||
|
||||
export class ContentScript {
|
||||
private lists: HighlightList[] = [];
|
||||
private isGlobalHighlightEnabled = true;
|
||||
private exceptionsList: string[] = [];
|
||||
private isCurrentSiteException = false;
|
||||
private matchCase = false;
|
||||
private matchWhole = false;
|
||||
private highlightEngine: HighlightEngine;
|
||||
|
||||
constructor() {
|
||||
this.highlightEngine = new HighlightEngine(() => this.processHighlights());
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
private async initialize(): Promise<void> {
|
||||
await this.loadSettings();
|
||||
this.setupMessageListener();
|
||||
this.setupScrollHandler();
|
||||
this.processHighlights();
|
||||
}
|
||||
|
||||
private async loadSettings(): Promise<void> {
|
||||
const data = await StorageService.get([
|
||||
'lists',
|
||||
'globalHighlightEnabled',
|
||||
'matchCaseEnabled',
|
||||
'matchWholeEnabled',
|
||||
'exceptionsList'
|
||||
]);
|
||||
|
||||
this.lists = data.lists || [];
|
||||
this.isGlobalHighlightEnabled = data.globalHighlightEnabled ?? true;
|
||||
this.matchCase = data.matchCaseEnabled ?? false;
|
||||
this.matchWhole = data.matchWholeEnabled ?? false;
|
||||
this.exceptionsList = data.exceptionsList || [];
|
||||
this.isCurrentSiteException = this.checkCurrentSiteException();
|
||||
}
|
||||
|
||||
private checkCurrentSiteException(): boolean {
|
||||
const currentHostname = window.location.hostname;
|
||||
return this.exceptionsList.includes(currentHostname);
|
||||
}
|
||||
|
||||
private setupMessageListener(): void {
|
||||
MessageService.onMessage((message: MessageData) => {
|
||||
switch (message.type) {
|
||||
case 'WORD_LIST_UPDATED':
|
||||
this.handleWordListUpdate();
|
||||
break;
|
||||
case 'GLOBAL_TOGGLE_UPDATED':
|
||||
this.handleGlobalToggleUpdate(message.enabled!);
|
||||
break;
|
||||
case 'MATCH_OPTIONS_UPDATED':
|
||||
this.handleMatchOptionsUpdate(message.matchCase!, message.matchWhole!);
|
||||
break;
|
||||
case 'EXCEPTIONS_LIST_UPDATED':
|
||||
this.handleExceptionsUpdate();
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private setupScrollHandler(): void {
|
||||
const debouncedProcess = DOMUtils.debounce(() => this.processHighlights(), 300);
|
||||
window.addEventListener('scroll', debouncedProcess);
|
||||
}
|
||||
|
||||
private async handleWordListUpdate(): Promise<void> {
|
||||
const data = await StorageService.get(['lists']);
|
||||
this.lists = data.lists || [];
|
||||
this.processHighlights();
|
||||
}
|
||||
|
||||
private handleGlobalToggleUpdate(enabled: boolean): void {
|
||||
this.isGlobalHighlightEnabled = enabled;
|
||||
this.processHighlights();
|
||||
}
|
||||
|
||||
private handleMatchOptionsUpdate(matchCase: boolean, matchWhole: boolean): void {
|
||||
this.matchCase = matchCase;
|
||||
this.matchWhole = matchWhole;
|
||||
this.processHighlights();
|
||||
}
|
||||
|
||||
private async handleExceptionsUpdate(): Promise<void> {
|
||||
const data = await StorageService.get(['exceptionsList']);
|
||||
this.exceptionsList = data.exceptionsList || [];
|
||||
this.isCurrentSiteException = this.checkCurrentSiteException();
|
||||
this.processHighlights();
|
||||
}
|
||||
|
||||
private processHighlights(): void {
|
||||
if (!this.isGlobalHighlightEnabled || this.isCurrentSiteException) {
|
||||
this.highlightEngine.clearHighlights();
|
||||
return;
|
||||
}
|
||||
|
||||
this.highlightEngine.highlight(this.lists, this.matchCase, this.matchWhole);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user