-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattentionHelp.js
More file actions
314 lines (268 loc) · 11.2 KB
/
attentionHelp.js
File metadata and controls
314 lines (268 loc) · 11.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(function () {
'use strict';
console.log('Sentence navigation script loaded');
let shiftHeld = false;
let sentenceSpans = [];
let currentIndex = 0;
let active = false;
let lastPageText = '';
const observer = new MutationObserver(() => {
const popup = document.querySelector('div[id^="cdk-overlay-"].gb-popup');
if (popup) popup.remove();
});
observer.observe(document.body, { childList: true, subtree: true });
document.addEventListener('keydown', (e) => {
if (e.key === 'Shift') {
shiftHeld = true;
if (sentenceSpans[currentIndex]) {
selectText(sentenceSpans[currentIndex]);
}
}
});
document.addEventListener('keyup', (e) => {
if (e.key === 'Shift') {
shiftHeld = false;
window.getSelection().removeAllRanges();
}
});
const style = document.createElement('style');
style.textContent = `
.focused-sentence {
background-color: #ffff99;
border-radius: 4px;
display: inline;
box-decoration-break: clone;
}
.hide-cursor {
cursor: none !important;
}
#fake-cursor {
position: fixed;
width: 8px;
height: 8px;
background: transparent;
border-radius: 50%;
pointer-events: none;
z-index: 9999;
}
`;
document.head.appendChild(style);
function autoUpdateSentences() {
setInterval(() => {
const bodyText = document.body.innerText.slice(0, 1000);
if (bodyText !== lastPageText) {
lastPageText = bodyText;
document.querySelectorAll('.sentence-nav-span').forEach(el => {
const textNode = document.createTextNode(el.textContent);
el.replaceWith(textNode);
});
sentenceSpans = [];
splitAndWrapSentences();
}
}, 1500);
}
function selectText(span) {
const range = document.createRange();
range.selectNodeContents(span);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
function splitSentencesWithIntl(text) {
const segmenter = new Intl.Segmenter('en', { granularity: 'sentence' });
return Array.from(segmenter.segment(text))
.map(segment => segment.segment.trim())
.filter(Boolean);
}
function getMergedTextNodes() {
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
const mergedNodes = [];
let currentGroup = [];
while (walker.nextNode()) {
const node = walker.currentNode;
if (node.nodeValue.trim().length > 0 && node.parentNode.nodeName !== "SCRIPT") {
const isInline = window.getComputedStyle(node.parentNode).display === "inline" ||
node.parentNode.tagName === "SPAN" || node.parentNode.tagName === "A";
if (isInline) {
currentGroup.push(node);
} else {
if (currentGroup.length > 0) {
mergedNodes.push([...currentGroup]);
currentGroup = [];
}
mergedNodes.push([node]);
}
}
}
if (currentGroup.length > 0) mergedNodes.push([...currentGroup]);
return mergedNodes;
}
function splitAndWrapSentences() {
const nodeGroups = getMergedTextNodes();
sentenceSpans = [];
for (let group of nodeGroups) {
const parent = group[0].parentNode;
const allSameParent = group.every(n => n.parentNode === parent);
if (!allSameParent) continue;
const text = group.map(n => n.nodeValue).join(" ");
const rawSentences = splitSentencesWithIntl(text);
if (!rawSentences || rawSentences.length === 0) continue;
const fragment = document.createDocumentFragment();
rawSentences.forEach((sentence) => {
const span = document.createElement('span');
span.textContent = sentence + ' ';
span.classList.add('sentence-nav-span');
span.addEventListener('click', () => {
currentIndex = sentenceSpans.indexOf(span);
highlightSentence(currentIndex);
});
fragment.appendChild(span);
sentenceSpans.push(span);
});
parent.insertBefore(fragment, group[group.length - 1].nextSibling);
group.forEach(n => {
if (n.parentNode === parent) parent.removeChild(n);
});
}
highlightSentence(0);
active = true;
}
function observeChanges() {
const targetNode = document.body;
const observer = new MutationObserver(() => {
const spansExist = document.querySelector('.sentence-nav-span');
if (!spansExist) {
sentenceSpans = [];
splitAndWrapSentences();
}
});
observer.observe(targetNode, { childList: true, subtree: true });
}
function highlightSentence(i) {
sentenceSpans.forEach(span => span.classList.remove('focused-sentence'));
if (sentenceSpans[i]) {
const span = sentenceSpans[i];
span.classList.add('focused-sentence');
currentIndex = i;
if (shiftHeld) selectText(span);
else window.getSelection().removeAllRanges();
}
}
function hideTooltip() {
// Simulate moving mouse away from text (offscreen)
const offX = window.innerWidth - 10;
const offY = window.innerHeight - 10;
const el = document.elementFromPoint(offX, offY);
if (el) {
el.dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX: offX, clientY: offY }));
el.dispatchEvent(new MouseEvent('mouseout', { bubbles: true, clientX: offX, clientY: offY }));
el.dispatchEvent(new MouseEvent('mouseleave', { bubbles: true, clientX: offX, clientY: offY }));
}
}
function simulateHover(x, y) {
const el = document.elementFromPoint(x, y);
if (el) {
el.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, clientX: x, clientY: y }));
el.dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX: x, clientY: y }));
el.dispatchEvent(new MouseEvent('mouseenter', { bubbles: true, clientX: x, clientY: y }));
}
}
function simulateFakeCursorHover(span) {
const range = document.createRange();
range.selectNodeContents(span);
const rects = range.getClientRects();
if (rects.length === 0) return;
const rect = rects[0];
const x = rect.left + 10;
const y = rect.top + 5;
// Hide real cursor
document.body.classList.add('hide-cursor');
let fakeCursor = document.getElementById('fake-cursor');
if (!fakeCursor) {
fakeCursor = document.createElement('div');
fakeCursor.id = 'fake-cursor';
document.body.appendChild(fakeCursor);
}
fakeCursor.style.left = `${x}px`;
fakeCursor.style.top = `${y}px`;
simulateHover(x, y);
setTimeout(() => {
fakeCursor.remove();
document.body.classList.remove('hide-cursor');
}, 1500);
}
// Key handler
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.altKey && e.key.toLowerCase() === 'r') {
splitAndWrapSentences();
autoUpdateSentences();
observeChanges();
return;
}
if (!active) return;
if (e.key === 's') {
if (currentIndex < sentenceSpans.length - 1) {
hideTooltip();
currentIndex++;
highlightSentence(currentIndex);
}
e.preventDefault();
} else if (e.key === 'w') {
if (currentIndex > 0) {
hideTooltip();
currentIndex--;
highlightSentence(currentIndex);
}
e.preventDefault();
} else if (e.key === 'd') {
const span = sentenceSpans[currentIndex];
if (!span) return;
const range = document.createRange();
range.selectNodeContents(span);
const rects = range.getClientRects();
if (rects.length === 0) return;
const rect = rects[0];
const x = rect.left + 10;
const y = rect.top + 5;
// Create or reuse fake cursor
let fakeCursor = document.getElementById('fake-cursor');
if (!fakeCursor) {
fakeCursor = document.createElement('div');
fakeCursor.id = 'fake-cursor';
fakeCursor.style.position = 'fixed';
fakeCursor.style.width = '6px';
fakeCursor.style.height = '6px';
fakeCursor.style.borderRadius = '50%';
fakeCursor.style.background = 'red'; // make visible to debug
fakeCursor.style.zIndex = '9999';
fakeCursor.style.pointerEvents = 'none';
document.body.appendChild(fakeCursor);
}
fakeCursor.style.left = `${x}px`;
fakeCursor.style.top = `${y}px`;
// Optional: slight scroll nudge to center sentence
// span.scrollIntoView({ block: 'center', behavior: 'instant' });
const target = document.elementFromPoint(x, y);
if (target) {
// Hover events first
target.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, clientX: x, clientY: y }));
target.dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX: x, clientY: y }));
// Give hover a little time before click
setTimeout(() => {
// Then click
target.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: x, clientY: y }));
target.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, clientX: x, clientY: y }));
target.dispatchEvent(new MouseEvent('click', { bubbles: true, clientX: x, clientY: y }));
// Now hide real cursor
document.body.classList.add('hide-cursor');
// Remove fake cursor and restore real cursor
setTimeout(() => {
if (fakeCursor) fakeCursor.remove();
document.body.classList.remove('hide-cursor');
}, 1500); // keep it invisible briefly to stabilize tooltip
}, 150); // short delay to allow hover before click
}
e.preventDefault();
}
});
})();