Kraft102 commited on
Commit
e523b2e
·
1 Parent(s): a0c9ee9

fix(extension): convert content.js to javascript and update api port

Browse files
Files changed (1) hide show
  1. browser-extension/content.js +24 -37
browser-extension/content.js CHANGED
@@ -3,27 +3,14 @@
3
  * Captures page content and enables AI assistance
4
  */
5
 
6
- interface PageContent {
7
- url: string;
8
- title: string;
9
- text: string;
10
- metadata: {
11
- author?: string;
12
- publishedDate?: string;
13
- description?: string;
14
- };
15
- selectedText?: string;
16
- }
17
-
18
  class WidgeTDCAssistant {
19
- private apiUrl: string = 'http://localhost:3000/api';
20
- private sidebar: HTMLElement | null = null;
21
-
22
  constructor() {
 
 
23
  this.init();
24
  }
25
 
26
- private async init() {
27
  // Listen for messages from background script
28
  chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
29
  this.handleMessage(message, sendResponse);
@@ -37,7 +24,7 @@ class WidgeTDCAssistant {
37
  /**
38
  * Handle messages from background script
39
  */
40
- private async handleMessage(message: any, sendResponse: (response?: any) => void) {
41
  switch (message.action) {
42
  case 'captureContent':
43
  const content = this.capturePageContent();
@@ -68,9 +55,9 @@ class WidgeTDCAssistant {
68
  /**
69
  * Capture page content
70
  */
71
- private capturePageContent(): PageContent {
72
  // Extract main content (remove scripts, styles, etc.)
73
- const clone = document.cloneNode(true) as Document;
74
  const scripts = clone.querySelectorAll('script, style, noscript');
75
  scripts.forEach(el => el.remove());
76
 
@@ -78,7 +65,7 @@ class WidgeTDCAssistant {
78
  const text = clone.body.innerText.trim();
79
 
80
  // Extract metadata
81
- const metadata: PageContent['metadata'] = {
82
  author: this.getMetaContent('author'),
83
  publishedDate: this.getMetaContent('article:published_time'),
84
  description: this.getMetaContent('description'),
@@ -95,17 +82,17 @@ class WidgeTDCAssistant {
95
  /**
96
  * Get meta tag content
97
  */
98
- private getMetaContent(name: string): string | undefined {
99
  const meta = document.querySelector(`meta[name="${name}"], meta[property="${name}"]`);
100
- return meta?.getAttribute('content') || undefined;
101
  }
102
 
103
  /**
104
  * Handle text selection
105
  */
106
- private handleTextSelection(event: MouseEvent) {
107
  const selection = window.getSelection();
108
- const selectedText = selection?.toString().trim();
109
 
110
  if (selectedText && selectedText.length > 10) {
111
  // Show floating action button
@@ -116,7 +103,7 @@ class WidgeTDCAssistant {
116
  /**
117
  * Show floating action button
118
  */
119
- private showFloatingButton(x: number, y: number, text: string) {
120
  // Remove existing button
121
  const existing = document.getElementById('widgetdc-floating-btn');
122
  if (existing) existing.remove();
@@ -138,8 +125,8 @@ class WidgeTDCAssistant {
138
  // Add event listeners
139
  button.querySelectorAll('.widgetdc-btn').forEach(btn => {
140
  btn.addEventListener('click', async (e) => {
141
- const action = (e.target as HTMLElement).getAttribute('data-action');
142
- await this.handleAction(action!, text);
143
  button.remove();
144
  });
145
  });
@@ -155,7 +142,7 @@ class WidgeTDCAssistant {
155
  /**
156
  * Handle floating button action
157
  */
158
- private async handleAction(action: string, text: string) {
159
  switch (action) {
160
  case 'save':
161
  await this.saveText(text);
@@ -177,7 +164,7 @@ class WidgeTDCAssistant {
177
  /**
178
  * Save text to backend
179
  */
180
- private async saveText(text: string) {
181
  await this.sendToBackend('/memory/ingest', {
182
  content: text,
183
  source: 'browser_selection',
@@ -189,7 +176,7 @@ class WidgeTDCAssistant {
189
  /**
190
  * Search for similar content
191
  */
192
- private async searchSimilar(query: string): Promise<any[]> {
193
  const response = await this.sendToBackend('/search', { query, limit: 5 });
194
  return response.results || [];
195
  }
@@ -197,7 +184,7 @@ class WidgeTDCAssistant {
197
  /**
198
  * Ask AI a question
199
  */
200
- private async askQuestion(question: string): Promise<string> {
201
  const response = await this.sendToBackend('/query', { question });
202
  return response.answer || 'No answer available';
203
  }
@@ -205,7 +192,7 @@ class WidgeTDCAssistant {
205
  /**
206
  * Send data to backend
207
  */
208
- private async sendToBackend(endpoint: string, data: any): Promise<any> {
209
  try {
210
  const response = await fetch(`${this.apiUrl}${endpoint}`, {
211
  method: 'POST',
@@ -229,7 +216,7 @@ class WidgeTDCAssistant {
229
  /**
230
  * Toggle sidebar
231
  */
232
- private toggleSidebar() {
233
  if (this.sidebar) {
234
  this.sidebar.remove();
235
  this.sidebar = null;
@@ -241,7 +228,7 @@ class WidgeTDCAssistant {
241
  /**
242
  * Create sidebar
243
  */
244
- private createSidebar() {
245
  const sidebar = document.createElement('div');
246
  sidebar.id = 'widgetdc-sidebar';
247
  sidebar.className = 'widgetdc-sidebar';
@@ -266,7 +253,7 @@ class WidgeTDCAssistant {
266
  /**
267
  * Show sidebar with search results
268
  */
269
- private showSidebarWithResults(results: any[]) {
270
  this.createSidebar();
271
  const content = this.sidebar?.querySelector('.widgetdc-sidebar-content');
272
  if (content) {
@@ -286,7 +273,7 @@ class WidgeTDCAssistant {
286
  /**
287
  * Show sidebar with AI answer
288
  */
289
- private showSidebarWithAnswer(answer: string) {
290
  this.createSidebar();
291
  const content = this.sidebar?.querySelector('.widgetdc-sidebar-content');
292
  if (content) {
@@ -300,7 +287,7 @@ class WidgeTDCAssistant {
300
  /**
301
  * Show notification
302
  */
303
- private showNotification(message: string) {
304
  const notification = document.createElement('div');
305
  notification.className = 'widgetdc-notification';
306
  notification.textContent = message;
 
3
  * Captures page content and enables AI assistance
4
  */
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  class WidgeTDCAssistant {
 
 
 
7
  constructor() {
8
+ this.apiUrl = 'http://localhost:3001/api';
9
+ this.sidebar = null;
10
  this.init();
11
  }
12
 
13
+ async init() {
14
  // Listen for messages from background script
15
  chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
16
  this.handleMessage(message, sendResponse);
 
24
  /**
25
  * Handle messages from background script
26
  */
27
+ async handleMessage(message, sendResponse) {
28
  switch (message.action) {
29
  case 'captureContent':
30
  const content = this.capturePageContent();
 
55
  /**
56
  * Capture page content
57
  */
58
+ capturePageContent() {
59
  // Extract main content (remove scripts, styles, etc.)
60
+ const clone = document.cloneNode(true);
61
  const scripts = clone.querySelectorAll('script, style, noscript');
62
  scripts.forEach(el => el.remove());
63
 
 
65
  const text = clone.body.innerText.trim();
66
 
67
  // Extract metadata
68
+ const metadata = {
69
  author: this.getMetaContent('author'),
70
  publishedDate: this.getMetaContent('article:published_time'),
71
  description: this.getMetaContent('description'),
 
82
  /**
83
  * Get meta tag content
84
  */
85
+ getMetaContent(name) {
86
  const meta = document.querySelector(`meta[name="${name}"], meta[property="${name}"]`);
87
+ return meta ? meta.getAttribute('content') : undefined;
88
  }
89
 
90
  /**
91
  * Handle text selection
92
  */
93
+ handleTextSelection(event) {
94
  const selection = window.getSelection();
95
+ const selectedText = selection ? selection.toString().trim() : '';
96
 
97
  if (selectedText && selectedText.length > 10) {
98
  // Show floating action button
 
103
  /**
104
  * Show floating action button
105
  */
106
+ showFloatingButton(x, y, text) {
107
  // Remove existing button
108
  const existing = document.getElementById('widgetdc-floating-btn');
109
  if (existing) existing.remove();
 
125
  // Add event listeners
126
  button.querySelectorAll('.widgetdc-btn').forEach(btn => {
127
  btn.addEventListener('click', async (e) => {
128
+ const action = e.target.getAttribute('data-action');
129
+ await this.handleAction(action, text);
130
  button.remove();
131
  });
132
  });
 
142
  /**
143
  * Handle floating button action
144
  */
145
+ async handleAction(action, text) {
146
  switch (action) {
147
  case 'save':
148
  await this.saveText(text);
 
164
  /**
165
  * Save text to backend
166
  */
167
+ async saveText(text) {
168
  await this.sendToBackend('/memory/ingest', {
169
  content: text,
170
  source: 'browser_selection',
 
176
  /**
177
  * Search for similar content
178
  */
179
+ async searchSimilar(query) {
180
  const response = await this.sendToBackend('/search', { query, limit: 5 });
181
  return response.results || [];
182
  }
 
184
  /**
185
  * Ask AI a question
186
  */
187
+ async askQuestion(question) {
188
  const response = await this.sendToBackend('/query', { question });
189
  return response.answer || 'No answer available';
190
  }
 
192
  /**
193
  * Send data to backend
194
  */
195
+ async sendToBackend(endpoint, data) {
196
  try {
197
  const response = await fetch(`${this.apiUrl}${endpoint}`, {
198
  method: 'POST',
 
216
  /**
217
  * Toggle sidebar
218
  */
219
+ toggleSidebar() {
220
  if (this.sidebar) {
221
  this.sidebar.remove();
222
  this.sidebar = null;
 
228
  /**
229
  * Create sidebar
230
  */
231
+ createSidebar() {
232
  const sidebar = document.createElement('div');
233
  sidebar.id = 'widgetdc-sidebar';
234
  sidebar.className = 'widgetdc-sidebar';
 
253
  /**
254
  * Show sidebar with search results
255
  */
256
+ showSidebarWithResults(results) {
257
  this.createSidebar();
258
  const content = this.sidebar?.querySelector('.widgetdc-sidebar-content');
259
  if (content) {
 
273
  /**
274
  * Show sidebar with AI answer
275
  */
276
+ showSidebarWithAnswer(answer) {
277
  this.createSidebar();
278
  const content = this.sidebar?.querySelector('.widgetdc-sidebar-content');
279
  if (content) {
 
287
  /**
288
  * Show notification
289
  */
290
+ showNotification(message) {
291
  const notification = document.createElement('div');
292
  notification.className = 'widgetdc-notification';
293
  notification.textContent = message;