3 Commits

Author SHA1 Message Date
semantic-release-bot
d275a6fd0d chore(release): 1.9.3
## [1.9.3](https://github.com/obsqrbtz/goose-highlighter/compare/v1.9.2...v1.9.3) (2025-11-18)

### Bug Fixes

* use CSS Custom Highlight API to avoid dom modifications (fixes [#1](https://github.com/obsqrbtz/goose-highlighter/issues/1)) ([3f2bb60](3f2bb6080b))
2025-11-19 01:26:16 +03:00
3f2bb6080b fix: use CSS Custom Highlight API to avoid dom modifications (fixes #1) 2025-11-19 01:25:55 +03:00
3da28a2ad7 chore: update deps 2025-11-18 20:58:59 +03:00
6 changed files with 988 additions and 1390 deletions

View File

@@ -1,3 +1,10 @@
## [1.9.3](https://github.com/obsqrbtz/goose-highlighter/compare/v1.9.2...v1.9.3) (2025-11-18)
### Bug Fixes
* use CSS Custom Highlight API to avoid dom modifications (fixes [#1](https://github.com/obsqrbtz/goose-highlighter/issues/1)) ([3f2bb60](https://github.com/obsqrbtz/goose-highlighter/commit/3f2bb6080ba3a9ac0599ad6594f0d877c12bb62f))
## [1.9.2](https://github.com/obsqrbtz/goose-highlighter/compare/v1.9.1...v1.9.2) (2025-11-14) ## [1.9.2](https://github.com/obsqrbtz/goose-highlighter/compare/v1.9.1...v1.9.2) (2025-11-14)

View File

@@ -2,7 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "__MSG_extension_name__", "name": "__MSG_extension_name__",
"description": "__MSG_extension_description__", "description": "__MSG_extension_description__",
"version": "1.9.2", "version": "1.9.3",
"default_locale": "en", "default_locale": "en",
"permissions": [ "permissions": [
"scripting", "scripting",

2228
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,7 @@
"@types/chrome": "^0.0.270", "@types/chrome": "^0.0.270",
"eslint": "^9.30.0", "eslint": "^9.30.0",
"globals": "^16.2.0", "globals": "^16.2.0",
"semantic-release": "^24.2.5", "semantic-release": "^24.2.9",
"typescript": "^5.6.0" "typescript": "^5.6.0"
}, },
"scripts": { "scripts": {

View File

@@ -1,9 +1,9 @@
import { HighlightList, ActiveWord } from '../types.js'; import { HighlightList, ActiveWord, CONSTANTS } from '../types.js';
import { DOMUtils } from '../utils/DOMUtils.js'; import { DOMUtils } from '../utils/DOMUtils.js';
export class HighlightEngine { export class HighlightEngine {
private styleSheet: CSSStyleSheet | null = null; private styleSheet: CSSStyleSheet | null = null;
private wordStyleMap = new Map<string, string>(); private highlights = new Map<string, Highlight>();
private observer: MutationObserver; private observer: MutationObserver;
private isHighlighting = false; private isHighlighting = false;
@@ -13,15 +13,10 @@ export class HighlightEngine {
const hasContentChanges = mutations.some((mutation: MutationRecord) => { const hasContentChanges = mutations.some((mutation: MutationRecord) => {
if (mutation.type !== 'childList') return false; if (mutation.type !== 'childList') return false;
if (mutation.target instanceof Element && mutation.target.hasAttribute('data-gh')) {
return false;
}
const allNodes = [...Array.from(mutation.addedNodes), ...Array.from(mutation.removedNodes)]; const allNodes = [...Array.from(mutation.addedNodes), ...Array.from(mutation.removedNodes)];
return allNodes.some(node => { return allNodes.some(node => {
if (node.nodeType === Node.TEXT_NODE) return true; if (node.nodeType === Node.TEXT_NODE) return true;
if (node instanceof Element && !node.hasAttribute('data-gh')) return true; if (node instanceof Element) return true;
return false; return false;
}); });
}); });
@@ -41,28 +36,24 @@ export class HighlightEngine {
} }
} }
private updateWordStyles(activeWords: ActiveWord[], matchCase: boolean): void { private updateHighlightStyles(activeWords: ActiveWord[]): void {
this.initializeStyleSheet(); this.initializeStyleSheet();
while (this.styleSheet!.cssRules.length > 0) { while (this.styleSheet!.cssRules.length > 0) {
this.styleSheet!.deleteRule(0); this.styleSheet!.deleteRule(0);
} }
this.wordStyleMap.clear(); const uniqueStyles = new Map<string, number>();
const uniqueStyles = new Map<string, string>(); let styleIndex = 0;
for (const word of activeWords) { for (const word of activeWords) {
const styleKey = `${word.background}-${word.foreground}`; const styleKey = `${word.background}-${word.foreground}`;
if (!uniqueStyles.has(styleKey)) { if (!uniqueStyles.has(styleKey)) {
const className = `highlighted-word-${uniqueStyles.size}`; uniqueStyles.set(styleKey, styleIndex);
uniqueStyles.set(styleKey, className); const rule = `::highlight(gh-${styleIndex}) { background-color: ${word.background}; color: ${word.foreground}; }`;
const rule = `.${className} { background: ${word.background}; color: ${word.foreground}; padding: 0 2px; }`;
this.styleSheet!.insertRule(rule, this.styleSheet!.cssRules.length); this.styleSheet!.insertRule(rule, this.styleSheet!.cssRules.length);
styleIndex++;
} }
const lookup = matchCase ? word.text : word.text.toLowerCase();
this.wordStyleMap.set(lookup, uniqueStyles.get(styleKey)!);
} }
} }
@@ -78,9 +69,6 @@ export class HighlightEngine {
NodeFilter.SHOW_TEXT, NodeFilter.SHOW_TEXT,
{ {
acceptNode: (node: Text) => { acceptNode: (node: Text) => {
if (node.parentNode && (node.parentNode as Element).hasAttribute('data-gh')) {
return NodeFilter.FILTER_REJECT;
}
if (node.parentNode && ['SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME'].includes(node.parentNode.nodeName)) { if (node.parentNode && ['SCRIPT', 'STYLE', 'NOSCRIPT', 'IFRAME'].includes(node.parentNode.nodeName)) {
return NodeFilter.FILTER_REJECT; return NodeFilter.FILTER_REJECT;
} }
@@ -119,7 +107,6 @@ export class HighlightEngine {
this.isHighlighting = true; this.isHighlighting = true;
this.observer.disconnect(); this.observer.disconnect();
this.clearHighlightsInternal(); this.clearHighlightsInternal();
const activeWords = this.extractActiveWords(lists); const activeWords = this.extractActiveWords(lists);
@@ -129,16 +116,23 @@ export class HighlightEngine {
return; return;
} }
this.updateWordStyles(activeWords, matchCase); this.updateHighlightStyles(activeWords);
const styleMap = new Map<string, number>();
const uniqueStyles = new Map<string, number>();
let styleIndex = 0;
const wordMap = new Map<string, ActiveWord>();
for (const word of activeWords) { for (const word of activeWords) {
const key = matchCase ? word.text : word.text.toLowerCase(); const styleKey = `${word.background}-${word.foreground}`;
wordMap.set(key, word); if (!uniqueStyles.has(styleKey)) {
uniqueStyles.set(styleKey, styleIndex++);
}
const lookup = matchCase ? word.text : word.text.toLowerCase();
styleMap.set(lookup, uniqueStyles.get(styleKey)!);
} }
const flags = matchCase ? 'gu' : 'giu'; const flags = matchCase ? 'gu' : 'giu';
let wordsPattern = Array.from(wordMap.keys()).map(DOMUtils.escapeRegex).join('|'); let wordsPattern = Array.from(styleMap.keys()).map(DOMUtils.escapeRegex).join('|');
if (matchWhole) { if (matchWhole) {
wordsPattern = `(?:(?<!\\p{L})|^)(${wordsPattern})(?:(?!\\p{L})|$)`; wordsPattern = `(?:(?<!\\p{L})|^)(${wordsPattern})(?:(?!\\p{L})|$)`;
@@ -148,37 +142,37 @@ export class HighlightEngine {
const pattern = new RegExp(`(${wordsPattern})`, flags); const pattern = new RegExp(`(${wordsPattern})`, flags);
const textNodes = this.getTextNodes(); const textNodes = this.getTextNodes();
const rangesByStyle = new Map<number, Range[]>();
for (const node of textNodes) { for (const node of textNodes) {
if (!node.nodeValue || !pattern.test(node.nodeValue)) continue; if (!node.nodeValue) continue;
const fragment = document.createDocumentFragment();
const text = node.nodeValue; const text = node.nodeValue;
let lastIndex = 0;
pattern.lastIndex = 0; pattern.lastIndex = 0;
let match; let match;
while ((match = pattern.exec(text)) !== null) { while ((match = pattern.exec(text)) !== null) {
if (match.index > lastIndex) {
fragment.appendChild(document.createTextNode(text.substring(lastIndex, match.index)));
}
const lookup = matchCase ? match[0] : match[0].toLowerCase(); const lookup = matchCase ? match[0] : match[0].toLowerCase();
const className = this.wordStyleMap.get(lookup) || 'highlighted-word-0'; const styleIdx = styleMap.get(lookup);
const highlightSpan = document.createElement('span');
highlightSpan.setAttribute('data-gh', '');
highlightSpan.className = className;
highlightSpan.textContent = match[0];
fragment.appendChild(highlightSpan);
lastIndex = pattern.lastIndex; if (styleIdx !== undefined) {
const range = new Range();
range.setStart(node, match.index);
range.setEnd(node, match.index + match[0].length);
if (!rangesByStyle.has(styleIdx)) {
rangesByStyle.set(styleIdx, []);
}
rangesByStyle.get(styleIdx)!.push(range);
}
} }
}
if (lastIndex < text.length) { for (const [styleIdx, ranges] of rangesByStyle) {
fragment.appendChild(document.createTextNode(text.substring(lastIndex))); const highlight = new Highlight(...ranges);
} const highlightName = `gh-${styleIdx}`;
this.highlights.set(highlightName, highlight);
node.parentNode?.replaceChild(fragment, node); CSS.highlights.set(highlightName, highlight);
} }
} catch (e) { } catch (e) {
console.error('Regex error:', e); console.error('Regex error:', e);
@@ -189,14 +183,10 @@ export class HighlightEngine {
} }
private clearHighlightsInternal(): void { private clearHighlightsInternal(): void {
const highlightedElements = document.querySelectorAll('[data-gh]'); for (const name of this.highlights.keys()) {
highlightedElements.forEach(element => { CSS.highlights.delete(name);
const parent = element.parentNode; }
if (parent) { this.highlights.clear();
parent.replaceChild(document.createTextNode(element.textContent || ''), element);
parent.normalize();
}
});
if (this.styleSheet && this.styleSheet.cssRules.length > 0) { if (this.styleSheet && this.styleSheet.cssRules.length > 0) {
while (this.styleSheet.cssRules.length > 0) { while (this.styleSheet.cssRules.length > 0) {

27
src/types/css-highlights.d.ts vendored Normal file
View File

@@ -0,0 +1,27 @@
// CSS Highlights API type declarations
interface Highlight {
new(...ranges: Range[]): Highlight;
add(range: Range): void;
clear(): void;
delete(range: Range): boolean;
has(range: Range): boolean;
readonly size: number;
}
interface HighlightRegistry {
set(name: string, highlight: Highlight): void;
get(name: string): Highlight | undefined;
delete(name: string): boolean;
clear(): void;
has(name: string): boolean;
readonly size: number;
}
interface CSS {
highlights: HighlightRegistry;
}
declare var Highlight: {
prototype: Highlight;
new(...ranges: Range[]): Highlight;
};