-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
224 lines (177 loc) · 7.48 KB
/
contentScript.js
File metadata and controls
224 lines (177 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// ─── State ────────────────────────────────────────────────────────────────────
let chunks = [];
let chunksStale = true; // invalidated by MutationObserver
let currentHighlightEl = null;
let currentHighlightStyles = null; // saved originals for non-destructive restore
let currentHighlightId = null;
// ─── DOM change watcher (SPA support) ─────────────────────────────────────────
const domObserver = new MutationObserver(() => {
chunksStale = true;
});
domObserver.observe(document.body, {
childList: true,
subtree: true,
});
// ─── Visibility ───────────────────────────────────────────────────────────────
function isVisible(el) {
const style = window.getComputedStyle(el);
if (style.visibility === "hidden" || style.display === "none") return false;
const rect = el.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
}
// ─── Chunk collection ─────────────────────────────────────────────────────────
// Tags that, when present as children, mark a parent as a "container" rather
// than a leaf content block. We never want to index the container — only its
// children — because that would produce near-duplicate entries.
const BLOCK_CHILD_TAGS = new Set([
"P", "LI", "BLOCKQUOTE", "PRE", "CODE",
"H1", "H2", "H3", "H4", "H5", "H6",
]);
function hasBlockChildren(el) {
for (const child of el.children) {
if (BLOCK_CHILD_TAGS.has(child.tagName)) return true;
}
return false;
}
function collectTextChunks() {
// Leaf-first selectors: precise tags first, then containers only when they
// have no block-level children (i.e., they ARE the leaf for their text).
const candidates = Array.from(
document.querySelectorAll(
"p, li, blockquote, pre, code, h1, h2, h3, h4, h5, h6, div, section, article"
)
);
const seen = new Set();
const list = [];
let id = 0;
for (const el of candidates) {
if (!isVisible(el)) continue;
const tag = el.tagName.toLowerCase();
const isContainer = tag === "div" || tag === "section" || tag === "article";
// Skip containers whose block-level children will be (or were) indexed
// independently — avoids duplicate / superset entries.
if (isContainer && hasBlockChildren(el)) continue;
const text = el.innerText?.trim();
if (!text || text.length < 10) continue; // skip trivially short nodes
// Deduplicate identical text (e.g. visually hidden duplicates)
if (seen.has(text)) continue;
seen.add(text);
let type = "text";
if (tag === "pre" || tag === "code") type = "code";
else if (/^h[1-6]$/.test(tag)) type = "headings";
list.push({ id: `chunk-${id++}`, el, text, type });
}
chunks = list;
chunksStale = false;
}
function ensureChunks() {
if (chunksStale || !chunks.length) collectTextChunks();
}
// ─── Scoring ──────────────────────────────────────────────────────────────────
const STOPWORDS = new Set([
"the", "a", "an", "and", "or", "but", "if", "then", "so",
"of", "in", "on", "for", "to", "is", "are", "was", "were",
"it", "this", "that", "with", "as", "by", "at", "be", "from",
]);
function tokenize(str) {
return str
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter((t) => t.length > 1 && !STOPWORDS.has(t));
}
function similarityScore(query, text) {
const qTokens = tokenize(query);
const tTokens = tokenize(text);
if (!qTokens.length || !tTokens.length) return 0;
// Token overlap with partial (prefix) matching for better recall.
// "component" will match "components", "computing" will not.
let match = 0;
for (const qt of qTokens) {
// Exact match scores 1, prefix match (stem) scores 0.6
if (tTokens.includes(qt)) {
match += 1;
} else if (tTokens.some((tt) => tt.startsWith(qt) || qt.startsWith(tt))) {
match += 0.6;
}
}
const base = match / qTokens.length;
// Boost chunks that contain the full query as a substring.
const exactBoost = text.toLowerCase().includes(query.toLowerCase()) ? 0.3 : 0;
// Length penalty: prefer focused chunks over huge containers that happen to
// share a word.
const lengthPenalty = Math.max(0, (text.length - 800) / 8000); // 0 up to 800 chars
return Math.min(1, base + exactBoost - lengthPenalty);
}
// ─── Highlight (non-destructive) ─────────────────────────────────────────────
const HIGHLIGHT_STYLE = {
backgroundColor: "#fff176",
color: "#000",
outline: "2px solid #f9a825",
borderRadius: "3px",
};
function clearHighlight() {
if (!currentHighlightEl) return;
// Restore exact original inline styles
for (const prop of Object.keys(HIGHLIGHT_STYLE)) {
currentHighlightEl.style[prop] = currentHighlightStyles[prop] ?? "";
}
currentHighlightEl = null;
currentHighlightStyles = null;
currentHighlightId = null;
}
function highlightChunk(chunkId) {
if (currentHighlightId === chunkId) return;
clearHighlight();
const chunk = chunks.find((c) => c.id === chunkId);
if (!chunk) return;
const el = chunk.el;
// Save originals before touching anything
currentHighlightStyles = {};
for (const prop of Object.keys(HIGHLIGHT_STYLE)) {
currentHighlightStyles[prop] = el.style[prop];
}
Object.assign(el.style, HIGHLIGHT_STYLE);
currentHighlightEl = el;
currentHighlightId = chunkId;
el.scrollIntoView({ behavior: "smooth", block: "center" });
}
// ─── Message handler ──────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.type === "SEMANTIC_SEARCH") {
ensureChunks();
const { query, mode = "all", semantic = true } = message;
let filtered = chunks;
if (mode === "text") filtered = chunks.filter((c) => c.type === "text");
else if (mode === "code") filtered = chunks.filter((c) => c.type === "code");
else if (mode === "headings") filtered = chunks.filter((c) => c.type === "headings");
let results;
if (semantic) {
results = filtered
.map((c) => ({
id: c.id,
preview: c.text.slice(0, 160),
score: similarityScore(query, c.text),
}))
.filter((r) => r.score > 0.05) // drop near-zero noise
.sort((a, b) => b.score - a.score)
.slice(0, 50);
} else {
const qLower = query.toLowerCase();
results = filtered
.filter((c) => c.text.toLowerCase().includes(qLower))
.map((c) => ({ id: c.id, preview: c.text.slice(0, 160), score: 1 }))
.slice(0, 50);
}
sendResponse({ results });
return true; // keep message channel open for async response
}
if (message.type === "SEMANTIC_JUMP_TO") {
highlightChunk(message.id);
}
if (message.type === "SEMANTIC_REINDEX") {
chunksStale = true;
ensureChunks();
sendResponse({ count: chunks.length });
return true;
}
});