fix(browser): improve error handling and code quality
- Fix response.json() called before checking response.ok in popup.js - Consolidate duplicate onInstalled listeners in background.js - Remove unnecessary return true from content.js message handler - Add better error handling for non-JSON responses - Improve user experience by not auto-opening popup on install Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -109,18 +109,22 @@ sendFileBtn.addEventListener('click', async () => {
|
|||||||
credentials: 'include'
|
credentials: 'include'
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await response.json();
|
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
const result = await response.json();
|
||||||
showStatus(
|
showStatus(
|
||||||
`✓ File sent successfully! Task ID: ${result.task_id}\nFilename: ${result.filename}`,
|
`✓ File sent successfully! Task ID: ${result.task_id}\nFilename: ${result.filename}`,
|
||||||
'success'
|
'success'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
showStatus(
|
// Try to parse JSON error, fall back to status text
|
||||||
`Error: ${result.detail || 'Failed to send file'}`,
|
let errorMessage = 'Failed to send file';
|
||||||
'error'
|
try {
|
||||||
);
|
const result = await response.json();
|
||||||
|
errorMessage = result.detail || errorMessage;
|
||||||
|
} catch (e) {
|
||||||
|
errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
||||||
|
}
|
||||||
|
showStatus(`Error: ${errorMessage}`, 'error');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showStatus(
|
showStatus(
|
||||||
|
|||||||
@@ -4,13 +4,18 @@
|
|||||||
chrome.runtime.onInstalled.addListener((details) => {
|
chrome.runtime.onInstalled.addListener((details) => {
|
||||||
if (details.reason === 'install') {
|
if (details.reason === 'install') {
|
||||||
console.log('DocuElevate extension installed');
|
console.log('DocuElevate extension installed');
|
||||||
// Open options page on first install
|
// Note: We don't open the popup automatically to avoid poor UX
|
||||||
chrome.tabs.create({
|
// User can click the extension icon to configure
|
||||||
url: chrome.runtime.getURL('popup/popup.html')
|
|
||||||
});
|
|
||||||
} else if (details.reason === 'update') {
|
} else if (details.reason === 'update') {
|
||||||
console.log('DocuElevate extension updated');
|
console.log('DocuElevate extension updated');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create context menu item
|
||||||
|
chrome.contextMenus.create({
|
||||||
|
id: 'send-to-docuelevate',
|
||||||
|
title: 'Send to DocuElevate',
|
||||||
|
contexts: ['link', 'page']
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Listen for messages from content script or popup
|
// Listen for messages from content script or popup
|
||||||
@@ -59,15 +64,6 @@ async function handleSendUrl(data) {
|
|||||||
return await response.json();
|
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
|
// Handle context menu clicks
|
||||||
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||||
if (info.menuItemId === 'send-to-docuelevate') {
|
if (info.menuItemId === 'send-to-docuelevate') {
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|||||||
title: document.title
|
title: document.title
|
||||||
};
|
};
|
||||||
sendResponse(pageInfo);
|
sendResponse(pageInfo);
|
||||||
return true; // Indicates response will be sent asynchronously
|
|
||||||
}
|
}
|
||||||
return false; // No async response
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Detect if current page is a direct file link
|
// Detect if current page is a direct file link
|
||||||
|
|||||||
Reference in New Issue
Block a user