Skip to content

Commit

Permalink
Merge pull request #40 from jeffnawroth/development
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffnawroth authored Nov 17, 2024
2 parents eebb93d + 6b2b539 commit 20c5379
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ async function updateContextMenuState() {
console.warn('Failed to update context menu item:', error)
}
})

// Update "check-bibliography" context menu item
const bibliographyTitle = translations.checkSelectedText.message
// eslint-disable-next-line no-console
console.log('Setting menu title for check-bibliography:', bibliographyTitle)
browser.contextMenus.update('check-bibliography', {
title: bibliographyTitle,
}).catch((error) => {
if (error.message.includes('Cannot find menu item with id check-bibliography')) {
console.warn('Context menu item "check-bibliography" not found. It might not be created yet.')
}
else {
console.warn('Failed to update context menu item:', error)
}
})
}

// Global error-catcher for unhandled Promise rejections
Expand All @@ -77,10 +92,11 @@ self.addEventListener('unhandledrejection', (event) => {
// Function to open Sidepanel and update state
// @ts-expect-error missing types
function attemptSidePanelOpen(windowId: number | null, selectedText?: string, tab?: chrome.tabs.Tab) {
if (windowId !== null && windowId !== -1) {
if (windowId && windowId !== -1) {
// eslint-disable-next-line no-console
console.log(`Performing sidepanel open with validated windowId: ${windowId}`)
// @ts-expect-error missing types

browser.sidePanel.open({ windowId }).then(() => {
isSidePanelOpen = true
updateContextMenuState()
Expand All @@ -93,6 +109,19 @@ function attemptSidePanelOpen(windowId: number | null, selectedText?: string, ta
console.error('Failed to open sidepanel:', error)
})
}
else {
console.warn('Invalid `windowId`, attempting fallback to last focused window...')
chrome.windows.getLastFocused({ populate: true }, (lastFocusedWindow: { id: number | null }) => {
if (lastFocusedWindow?.id && lastFocusedWindow.id !== -1) {
// eslint-disable-next-line no-console
console.log(`Fallback windowId obtained: ${lastFocusedWindow.id}`)
attemptSidePanelOpen(lastFocusedWindow.id, selectedText, tab)
}
else {
console.error('No valid windowId available. Sidepanel cannot be opened.')
}
})
}
}

// Handle context menu clicks
Expand Down

0 comments on commit 20c5379

Please sign in to comment.