mirror of
https://github.com/obsqrbtz/goose-highlighter.git
synced 2026-04-09 04:29:09 +03:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d275a6fd0d | ||
| 3f2bb6080b | |||
| 3da28a2ad7 | |||
|
|
4f575d9534 | ||
| affddd3dbc | |||
|
|
c8334f9e68 | ||
| 172aa7583b | |||
|
|
6d7d9ac151 | ||
| 5ef380e544 | |||
|
|
c634f6bc8b | ||
| 67577c89cf | |||
|
|
326e585021 | ||
| 8be53f3240 | |||
| f07617fa55 | |||
| e79874922a | |||
| 71216cbcd9 |
3
.github/workflows/publish-extension.yml
vendored
3
.github/workflows/publish-extension.yml
vendored
@@ -33,6 +33,9 @@ jobs:
|
|||||||
id: version
|
id: version
|
||||||
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
|
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Update manifest version
|
||||||
|
run: node scripts/update-manifest-version.js ${{ steps.version.outputs.VERSION }}
|
||||||
|
|
||||||
- name: Create zip package
|
- name: Create zip package
|
||||||
run: |
|
run: |
|
||||||
zip -r goose-highlighter.zip . -x '*.git*' 'node_modules/*' 'src/*' 'scripts/*' 'versioning.md' '.releaserc.json' 'package.json' 'package-lock.json' 'README.md' 'tsconfig.json' 'eslint.config.mjs'
|
zip -r goose-highlighter.zip . -x '*.git*' 'node_modules/*' 'src/*' 'scripts/*' 'versioning.md' '.releaserc.json' 'package.json' 'package-lock.json' 'README.md' 'tsconfig.json' 'eslint.config.mjs'
|
||||||
|
|||||||
42
CHANGELOG.md
42
CHANGELOG.md
@@ -1,3 +1,45 @@
|
|||||||
|
## [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)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **highlight:** prevent creating extra <span>'s ([#1](https://github.com/obsqrbtz/goose-highlighter/issues/1)) ([affddd3](https://github.com/obsqrbtz/goose-highlighter/commit/affddd3dbc7de30100ca134ec65f4dc090275ca5))
|
||||||
|
|
||||||
|
## [1.9.1](https://github.com/obsqrbtz/goose-highlighter/compare/v1.9.0...v1.9.1) (2025-11-05)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* remove halowen styling ([172aa75](https://github.com/obsqrbtz/goose-highlighter/commit/172aa7583b325761af43c780db4ac61dc4bda99b))
|
||||||
|
|
||||||
|
# [1.9.0](https://github.com/obsqrbtz/goose-highlighter/compare/v1.8.5...v1.9.0) (2025-10-31)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* haloween styling ([5ef380e](https://github.com/obsqrbtz/goose-highlighter/commit/5ef380e54447f45f7360dd4b7b84456aae55bfee))
|
||||||
|
|
||||||
|
## [1.8.5](https://github.com/obsqrbtz/goose-highlighter/compare/v1.8.4...v1.8.5) (2025-10-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* highlight colors when multiple list have different configurations ([67577c8](https://github.com/obsqrbtz/goose-highlighter/commit/67577c89cffca1ab6d40a8913e51b7c3c6f91c85))
|
||||||
|
|
||||||
|
## [1.8.4](https://github.com/obsqrbtz/goose-highlighter/compare/v1.8.3...v1.8.4) (2025-10-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* do not re-highlight when already processing highlights ([8be53f3](https://github.com/obsqrbtz/goose-highlighter/commit/8be53f32402c2f0f228ca003ef3805c5ff0b6e88))
|
||||||
|
|
||||||
## [1.8.3](https://github.com/obsqrbtz/goose-highlighter/compare/v1.8.2...v1.8.3) (2025-10-08)
|
## [1.8.3](https://github.com/obsqrbtz/goose-highlighter/compare/v1.8.2...v1.8.3) (2025-10-08)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Goose Highlighter
|
# <img src="img/logo.png" alt="Goose Highlighter Logo" width="32" style="vertical-align: middle;"> Goose Highlighter
|
||||||
|
|
||||||
Goose Highlighter is a browser extension that allows you to highlight words on any webpage.
|
Goose Highlighter is a browser extension that allows you to highlight words on any webpage.
|
||||||
|
|
||||||
|
|||||||
BIN
img/logo.png
Normal file
BIN
img/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 MiB |
@@ -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.8.3",
|
"version": "1.9.3",
|
||||||
"default_locale": "en",
|
"default_locale": "en",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"scripting",
|
"scripting",
|
||||||
|
|||||||
2228
package-lock.json
generated
2228
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -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": {
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export class ContentScript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setupScrollHandler(): void {
|
private setupScrollHandler(): void {
|
||||||
const debouncedProcess = DOMUtils.debounce(() => this.processHighlights(), 300);
|
const debouncedProcess = DOMUtils.debounce(() => this.processHighlights(), CONSTANTS.DEBOUNCE_DELAY);
|
||||||
window.addEventListener('scroll', debouncedProcess);
|
window.addEventListener('scroll', debouncedProcess);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,30 @@
|
|||||||
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;
|
||||||
|
|
||||||
constructor(private onUpdate: () => void) {
|
constructor(private onUpdate: () => void) {
|
||||||
this.observer = new MutationObserver(DOMUtils.debounce((mutations: MutationRecord[]) => {
|
this.observer = new MutationObserver(DOMUtils.debounce((mutations: MutationRecord[]) => {
|
||||||
const hasRelevantChanges = mutations.some((mutation: MutationRecord) => {
|
if (this.isHighlighting) return;
|
||||||
if (mutation.target instanceof Element && mutation.target.hasAttribute('data-gh')) {
|
|
||||||
|
const hasContentChanges = mutations.some((mutation: MutationRecord) => {
|
||||||
|
if (mutation.type !== 'childList') return false;
|
||||||
|
const allNodes = [...Array.from(mutation.addedNodes), ...Array.from(mutation.removedNodes)];
|
||||||
|
return allNodes.some(node => {
|
||||||
|
if (node.nodeType === Node.TEXT_NODE) return true;
|
||||||
|
if (node instanceof Element) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
});
|
||||||
const addedNodes = Array.from(mutation.addedNodes);
|
|
||||||
const removedNodes = Array.from(mutation.removedNodes);
|
|
||||||
const isOurChange = [...addedNodes, ...removedNodes].some(node =>
|
|
||||||
node instanceof Element && (node.hasAttribute('data-gh') || node.querySelector('[data-gh]'))
|
|
||||||
);
|
|
||||||
return !isOurChange;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hasRelevantChanges) {
|
if (hasContentChanges) {
|
||||||
onUpdate();
|
this.onUpdate();
|
||||||
}
|
}
|
||||||
}, 300));
|
}, CONSTANTS.DEBOUNCE_DELAY));
|
||||||
}
|
}
|
||||||
|
|
||||||
private initializeStyleSheet(): void {
|
private initializeStyleSheet(): void {
|
||||||
@@ -35,28 +36,24 @@ export class HighlightEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updateWordStyles(activeWords: ActiveWord[]): 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 = word.text;
|
|
||||||
this.wordStyleMap.set(lookup, uniqueStyles.get(styleKey)!);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,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;
|
||||||
}
|
}
|
||||||
@@ -109,26 +103,36 @@ export class HighlightEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
highlight(lists: HighlightList[], matchCase: boolean, matchWhole: boolean): void {
|
highlight(lists: HighlightList[], matchCase: boolean, matchWhole: boolean): void {
|
||||||
this.observer.disconnect();
|
if (this.isHighlighting) return;
|
||||||
|
this.isHighlighting = true;
|
||||||
|
|
||||||
|
this.observer.disconnect();
|
||||||
this.clearHighlightsInternal();
|
this.clearHighlightsInternal();
|
||||||
|
|
||||||
const activeWords = this.extractActiveWords(lists);
|
const activeWords = this.extractActiveWords(lists);
|
||||||
if (activeWords.length === 0) {
|
if (activeWords.length === 0) {
|
||||||
this.startObserving();
|
this.startObserving();
|
||||||
|
this.isHighlighting = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.updateWordStyles(activeWords);
|
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})|$)`;
|
||||||
@@ -138,34 +142,51 @@ 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 span = document.createElement('span');
|
const text = node.nodeValue;
|
||||||
span.innerHTML = node.nodeValue.replace(pattern, (match) => {
|
pattern.lastIndex = 0;
|
||||||
const lookup = matchCase ? match : match.toLowerCase();
|
let match;
|
||||||
const className = this.wordStyleMap.get(lookup) || 'highlighted-word-0';
|
|
||||||
return `<span data-gh class="${className}">${match}</span>`;
|
|
||||||
});
|
|
||||||
|
|
||||||
node.parentNode?.replaceChild(span, node);
|
while ((match = pattern.exec(text)) !== null) {
|
||||||
|
const lookup = matchCase ? match[0] : match[0].toLowerCase();
|
||||||
|
const styleIdx = styleMap.get(lookup);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [styleIdx, ranges] of rangesByStyle) {
|
||||||
|
const highlight = new Highlight(...ranges);
|
||||||
|
const highlightName = `gh-${styleIdx}`;
|
||||||
|
this.highlights.set(highlightName, highlight);
|
||||||
|
CSS.highlights.set(highlightName, highlight);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Regex error:', e);
|
console.error('Regex error:', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.startObserving();
|
this.startObserving();
|
||||||
|
this.isHighlighting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@@ -182,8 +203,6 @@ export class HighlightEngine {
|
|||||||
this.observer.observe(document.body, {
|
this.observer.observe(document.body, {
|
||||||
childList: true,
|
childList: true,
|
||||||
subtree: true,
|
subtree: true,
|
||||||
characterData: true,
|
|
||||||
// Don't observe attribute changes to avoid triggering on our own style changes
|
|
||||||
attributes: false
|
attributes: false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/types/css-highlights.d.ts
vendored
Normal file
27
src/types/css-highlights.d.ts
vendored
Normal 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;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user