jobbler commited on
Commit
19d388b
·
1 Parent(s): 2ecb595

feat: Auto-recompute text in extension when saving settings

Browse files
firefox-extension/content.js CHANGED
@@ -70,6 +70,9 @@ browser.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
70
 
71
  const container = document.createElement('span');
72
  container.className = 'flowread-container';
 
 
 
73
  container.innerHTML = htmlString;
74
  range.insertNode(container);
75
 
@@ -81,6 +84,8 @@ browser.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
81
  }
82
  } else if (request.action === "flowread_page") {
83
  await processEntirePage();
 
 
84
  }
85
  });
86
 
@@ -174,6 +179,9 @@ async function processEntirePage() {
174
 
175
  const container = document.createElement('span');
176
  container.className = 'flowread-container';
 
 
 
177
  container.innerHTML = htmlString;
178
 
179
  if (node.parentNode) {
@@ -191,6 +199,71 @@ async function processEntirePage() {
191
  showToast(`Done! Analyzed ${processedCount} blocks.`, 2000);
192
  }
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  // Grouping logic extracted from your frontend code
195
  function generateFlowReadHTML(currentTokens, threshold, useGradient) {
196
  let html = "";
 
70
 
71
  const container = document.createElement('span');
72
  container.className = 'flowread-container';
73
+ container.dataset.tokens = JSON.stringify(currentTokens);
74
+ container.dataset.preprompt = preprompt;
75
+ container.dataset.originalText = selectedText;
76
  container.innerHTML = htmlString;
77
  range.insertNode(container);
78
 
 
84
  }
85
  } else if (request.action === "flowread_page") {
86
  await processEntirePage();
87
+ } else if (request.action === "settings_updated") {
88
+ await updateExisting(request.settings);
89
  }
90
  });
91
 
 
179
 
180
  const container = document.createElement('span');
181
  container.className = 'flowread-container';
182
+ container.dataset.tokens = JSON.stringify(data.words);
183
+ container.dataset.preprompt = preprompt;
184
+ container.dataset.originalText = text;
185
  container.innerHTML = htmlString;
186
 
187
  if (node.parentNode) {
 
199
  showToast(`Done! Analyzed ${processedCount} blocks.`, 2000);
200
  }
201
 
202
+ async function updateExisting(newSettings) {
203
+ const threshold = newSettings.threshold !== undefined ? newSettings.threshold : 0.35;
204
+ const useGradient = newSettings.gradientMode || false;
205
+ const preprompt = newSettings.preprompt || "";
206
+ const apiUrl = newSettings.apiUrl || "http://127.0.0.1:8000";
207
+ const checkedLayers = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
208
+
209
+ const containers = document.querySelectorAll('.flowread-container');
210
+ if (containers.length === 0) return;
211
+
212
+ let reFetchCount = 0;
213
+ let rerenderCount = 0;
214
+
215
+ for (const container of containers) {
216
+ const oldPreprompt = container.dataset.preprompt || "";
217
+ const text = container.dataset.originalText;
218
+ if (!text) continue;
219
+
220
+ if (oldPreprompt !== preprompt) {
221
+ if (reFetchCount === 0) {
222
+ showToast("Updating FlowRead elements with new intent...", 0);
223
+ }
224
+ try {
225
+ const response = await fetch(`${apiUrl}/analyze`, {
226
+ method: 'POST',
227
+ headers: { 'Content-Type': 'application/json' },
228
+ body: JSON.stringify({
229
+ text: text,
230
+ preprompt: preprompt,
231
+ layers: checkedLayers
232
+ })
233
+ });
234
+
235
+ if (!response.ok) continue;
236
+ const data = await response.json();
237
+ if (!data.words) continue;
238
+
239
+ container.dataset.tokens = JSON.stringify(data.words);
240
+ container.dataset.preprompt = preprompt;
241
+ const htmlString = generateFlowReadHTML(data.words, threshold, useGradient);
242
+ container.innerHTML = htmlString;
243
+ reFetchCount++;
244
+ } catch (err) {
245
+ console.error("Update error:", err);
246
+ }
247
+ } else {
248
+ // Just re-render visuals locally (super fast)
249
+ try {
250
+ const tokens = JSON.parse(container.dataset.tokens);
251
+ const htmlString = generateFlowReadHTML(tokens, threshold, useGradient);
252
+ container.innerHTML = htmlString;
253
+ rerenderCount++;
254
+ } catch (e) {
255
+ console.error("Error parsing tokens", e);
256
+ }
257
+ }
258
+ }
259
+
260
+ if (reFetchCount > 0) {
261
+ showToast(`Updated ${reFetchCount} blocks with new AI intent!`, 2000);
262
+ } else if (rerenderCount > 0) {
263
+ showToast(`Updated visuals for ${rerenderCount} blocks!`, 1500);
264
+ }
265
+ }
266
+
267
  // Grouping logic extracted from your frontend code
268
  function generateFlowReadHTML(currentTokens, threshold, useGradient) {
269
  let html = "";
firefox-extension/popup.js CHANGED
@@ -39,6 +39,13 @@ document.addEventListener('DOMContentLoaded', () => {
39
  browser.storage.local.set(settings).then(() => {
40
  saveBtn.textContent = 'Saved!';
41
  setTimeout(() => saveBtn.textContent = 'Save Settings', 1500);
 
 
 
 
 
 
 
42
  });
43
  });
44
 
 
39
  browser.storage.local.set(settings).then(() => {
40
  saveBtn.textContent = 'Saved!';
41
  setTimeout(() => saveBtn.textContent = 'Save Settings', 1500);
42
+
43
+ // Notify the active tab to automatically recompute text
44
+ browser.tabs.query({active: true, currentWindow: true}, (tabs) => {
45
+ if (tabs[0]) {
46
+ browser.tabs.sendMessage(tabs[0].id, { action: "settings_updated", settings: settings });
47
+ }
48
+ });
49
  });
50
  });
51
 
flowread-extension.zip CHANGED
Binary files a/flowread-extension.zip and b/flowread-extension.zip differ