Files
gh-christianlouis-docuelevate/browser-extension/scripts/background.js
T
copilot-swe-agent[bot] 817dc31e12 feat(browser): implement browser extension for sending files to DocuElevate
- Add complete browser extension with popup UI and background workers
- Support Chrome, Firefox, Edge, and other Chromium-based browsers
- Include context menu integration for quick file sending
- Add comprehensive documentation for users and administrators
- Update main README and API docs to include browser extension
- Implement secure configuration storage in browser extension storage
- Add SSRF-protected URL upload endpoint integration

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
2026-02-12 03:04:36 +00:00

114 lines
3.5 KiB
JavaScript

// Background service worker for DocuElevate browser extension
// Listen for installation
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
console.log('DocuElevate extension installed');
// Open options page on first install
chrome.tabs.create({
url: chrome.runtime.getURL('popup/popup.html')
});
} else if (details.reason === 'update') {
console.log('DocuElevate extension updated');
}
});
// Listen for messages from content script or popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'SEND_URL') {
handleSendUrl(message.data)
.then(result => sendResponse({ success: true, data: result }))
.catch(error => sendResponse({ success: false, error: error.message }));
return true; // Keep channel open for async response
}
});
// Handle sending URL to DocuElevate
async function handleSendUrl(data) {
const { url, filename, serverUrl, sessionCookie } = data;
if (!url || !serverUrl) {
throw new Error('URL and server URL are required');
}
const headers = {
'Content-Type': 'application/json'
};
if (sessionCookie) {
headers['Cookie'] = sessionCookie;
}
const payload = {
url: url,
filename: filename || null
};
const response = await fetch(`${serverUrl}/api/process-url`, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload),
credentials: 'include'
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: 'Unknown error' }));
throw new Error(errorData.detail || `HTTP ${response.status}`);
}
return await response.json();
}
// Add context menu item for sending URLs
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'send-to-docuelevate',
title: 'Send to DocuElevate',
contexts: ['link', 'page']
});
});
// Handle context menu clicks
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'send-to-docuelevate') {
// Get the URL to send (link URL or page URL)
const targetUrl = info.linkUrl || info.pageUrl;
// Load configuration
const config = await new Promise((resolve) => {
chrome.storage.sync.get(['serverUrl', 'sessionCookie'], resolve);
});
if (!config.serverUrl) {
// Open popup to configure
chrome.action.openPopup();
return;
}
// Send the URL
try {
const result = await handleSendUrl({
url: targetUrl,
serverUrl: config.serverUrl,
sessionCookie: config.sessionCookie
});
// Show success notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon48.png',
title: 'DocuElevate',
message: `File sent successfully! Task ID: ${result.task_id}`
});
} catch (error) {
// Show error notification
chrome.notifications.create({
type: 'basic',
iconUrl: 'icons/icon48.png',
title: 'DocuElevate Error',
message: `Failed to send file: ${error.message}`
});
}
}
});