feat(core): Update opengrammar/extension/src/content/textExtractor.ts (Commit #12)

Swadhin Biswas committed 7 months ago
e90250e
1 parent d043d6d
commit e90250e
1 file changed
+32 -0
▼opengrammar/extension/src/content/textExtractor.ts
+32-0
4
4
*/
5
5
export function extractText(element: HTMLElement): string {
6
6
if (!element) return '';
7
+
8
+ if (window.location.hostname.includes('docs.google.com')) {
9
+ const docsText = extractGoogleDocsText(element);
10
+ if (docsText) return docsText;
11
+ }
7
12
8
13
if (element.tagName === 'INPUT') {
9
14
return (element as HTMLInputElement).value;
35
40
if (!target) return null;
36
41
37
42
if (target instanceof HTMLElement) {
43
+ if (window.location.hostname.includes('docs.google.com')) {
44
+ const docsTarget = target.closest('[contenteditable="true"], [role="textbox"], .kix-appview-editor') as HTMLElement | null;
45
+ if (docsTarget) return docsTarget;
46
+ }
47
+
38
48
return target;
39
49
}
40
50
41
51
return null;
42
52
}
43
53
54
+function extractGoogleDocsText(element: HTMLElement): string {
55
+ const docsRoot =
56
+ element.closest('[role="textbox"]') ||
57
+ element.closest('.kix-appview-editor') ||
58
+ document.querySelector('[role="textbox"]') ||
59
+ document.querySelector('.kix-appview-editor');
60
+
61
+ if (!docsRoot) return '';
62
+
63
+ const wordNodes = docsRoot.querySelectorAll('.kix-wordhtmlgenerator-word-node, .kix-lineview-text-block');
64
+ if (wordNodes.length > 0) {
65
+ return Array.from(wordNodes)
66
+ .map((node) => (node.textContent || '').trim())
67
+ .filter(Boolean)
68
+ .join(' ')
69
+ .replace(/\s+/g, ' ')
70
+ .trim();
71
+ }
72
+
73
+ return docsRoot.textContent?.replace(/\s+/g, ' ').trim() || '';
74
+}
75
+
44
76
/**
45
77
* Gets the caret position within an editable element
46
78
*/
47
79