Spaces:
Paused
Paused
File size: 3,464 Bytes
5a81b95 038a1a3 5a81b95 5ec082b 5a81b95 5ec082b 5a81b95 | 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 | // WidgeTDC Harvester Background Script
let API_BASE = 'http://localhost:3001';
// Load config on startup
chrome.storage.sync.get({ widgetdc_api_url: 'http://localhost:3001' }, (items) => {
API_BASE = items.widgetdc_api_url;
});
// Listen for config changes
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'sync' && changes.widgetdc_api_url) {
API_BASE = changes.widgetdc_api_url.newValue;
}
});
// Listen for messages from popup or content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'ingest_page') {
handleIngestion(request.data).then(sendResponse);
return true; // Keep channel open for async response
}
if (request.action === 'crawl_site') {
handleCrawl(request.data).then(sendResponse);
return true;
}
if (request.action === 'check_connection') {
checkBackendHealth().then(sendResponse);
return true;
}
});
async function checkBackendHealth() {
try {
const response = await fetch(`${API_BASE}/health`);
return { success: response.ok };
} catch (error) {
return { success: false, error: error.message };
}
}
async function handleIngestion(data) {
const { url, title, html } = data;
console.log('Ingesting page:', url);
try {
// Option 1: Send raw HTML (heavy)
// Option 2: Send URL and let backend crawl (cleaner) -> We use this via ingestion.crawl
const response = await fetch(`${API_BASE}/api/mcp/route`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tool: 'ingestion.crawl',
params: {
url: url,
depth: 0 // Single page
}
})
});
const result = await response.json();
return { success: true, data: result };
} catch (error) {
console.error('Ingestion failed:', error);
return { success: false, error: error.message };
}
}
async function handleCrawl(data) {
const { url, depth } = data;
console.log(`Crawling site: ${url} (Depth: ${depth})`);
try {
const response = await fetch(`${API_BASE}/api/mcp/route`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tool: 'ingestion.crawl',
params: {
url: url,
depth: depth || 1,
mode: data.mode || 'single',
keywords: data.keywords || [],
user: data.user
}
})
});
const result = await response.json();
return { success: true, data: result };
} catch (error) {
console.error('Crawl failed:', error);
return { success: false, error: error.message };
}
}
// Context menu for quick actions
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "widgetdc-ingest",
title: "Ingest to WidgeTDC",
contexts: ["page", "selection", "link"]
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "widgetdc-ingest") {
const targetUrl = info.linkUrl || info.pageUrl;
handleIngestion({ url: targetUrl });
}
});
|