From 6badceaf46b8d83087685bf4e2ea7affd9547bfb Mon Sep 17 00:00:00 2001 From: Tilen Komel Date: Mon, 12 Oct 2026 20:45:51 +0200 Subject: [PATCH] Enhance visit filtering and batching for remote data submission --- extension/service-worker.js | 40 ++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/extension/service-worker.js b/extension/service-worker.js index ce15084..45d3474 100644 --- a/extension/service-worker.js +++ b/extension/service-worker.js @@ -24,16 +24,38 @@ async function archiveLocally(visits) { async function sendToCollector(visits) { // Empty settings deliberately disable sending; the local SQLite archive still works. if (!CONFIG.collectorUrl || !CONFIG.apiToken) return true; + const remoteVisits = visits + .filter((visit) => { + if (typeof visit?.url !== "string") return false; + try { + const protocol = new URL(visit.url).protocol; + return protocol === "http:" || protocol === "https:"; + } catch { + return false; + } + }) + .map((visit) => ({ + ...visit, + title: String(visit.title ?? "").slice(0, 1000) + })); try { - const response = await fetch(CONFIG.collectorUrl, { - method: "POST", - headers: { - "Content-Type": "application/json", - "Authorization": `Bearer ${CONFIG.apiToken}` - }, - body: JSON.stringify({ device_id: CONFIG.deviceId, visits }) - }); - if (!response.ok) throw new Error(`HTTP ${response.status}`); + for (let offset = 0; offset < remoteVisits.length; offset += 100) { + const response = await fetch(CONFIG.collectorUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${CONFIG.apiToken}` + }, + body: JSON.stringify({ + device_id: CONFIG.deviceId, + visits: remoteVisits.slice(offset, offset + 100) + }) + }); + if (!response.ok) { + const detail = (await response.text()).trim().slice(0, 1000); + throw new Error(`HTTP ${response.status}${detail ? `: ${detail}` : ""}`); + } + } return true; } catch (error) { console.warn("Collector unavailable", error.message);