|
1 | 1 | import './style.css'
|
| 2 | +import { logger } from '../../lib/logger' |
| 3 | +import type { |
| 4 | + GetOpenSpotsMessage, |
| 5 | + GetOpenSpotsResponse, |
| 6 | + SwitchToTabMessage, |
| 7 | +} from '../../lib/messages' |
| 8 | +import { EnhancerRegistry } from '../../lib/registries' |
| 9 | +import type { CommentState } from '../background' |
2 | 10 |
|
3 |
| -document.addEventListener('DOMContentLoaded', async () => { |
4 |
| - const statusDiv = document.getElementById('scan-results') as HTMLElement |
| 11 | +// Test basic DOM access |
| 12 | +try { |
| 13 | + const app = document.getElementById('app')! |
| 14 | + logger.debug('Found app element:', app) |
| 15 | + app.innerHTML = '<div>Script is running...</div>' |
| 16 | +} catch (error) { |
| 17 | + logger.error('Error accessing DOM:', error) |
| 18 | +} |
5 | 19 |
|
| 20 | +const enhancers = new EnhancerRegistry() |
| 21 | + |
| 22 | +async function getOpenSpots(): Promise<CommentState[]> { |
| 23 | + logger.debug('Sending message to background script...') |
6 | 24 | try {
|
7 |
| - // get current active tab |
8 |
| - const tabs = await browser.tabs.query({ active: true, currentWindow: true }) |
9 |
| - const tab = tabs[0] |
10 |
| - |
11 |
| - if (!tab?.id) { |
12 |
| - statusDiv.textContent = 'Cannot access current tab' |
13 |
| - return |
14 |
| - } |
15 |
| - |
16 |
| - // send message to content script to get scan results |
17 |
| - const results = await browser.tabs.sendMessage(tab.id, { |
18 |
| - action: 'getScanResults', |
19 |
| - }) |
20 |
| - if (results) { |
21 |
| - // TODO: statusDiv.textContent = {{show drafts}} |
22 |
| - } |
| 25 | + const message: GetOpenSpotsMessage = { type: 'GET_OPEN_SPOTS' } |
| 26 | + const response = (await browser.runtime.sendMessage(message)) as GetOpenSpotsResponse |
| 27 | + logger.debug('Received response:', response) |
| 28 | + return response.spots || [] |
23 | 29 | } catch (error) {
|
24 |
| - console.error('Popup error:', error) |
25 |
| - statusDiv.textContent = 'Unable to load saved drafts.' |
| 30 | + logger.error('Error sending message to background:', error) |
| 31 | + return [] |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function switchToTab(tabId: number, windowId: number): void { |
| 36 | + // Send message to background script to handle tab switching |
| 37 | + // This avoids the popup context being destroyed before completion |
| 38 | + const message: SwitchToTabMessage = { |
| 39 | + tabId, |
| 40 | + type: 'SWITCH_TO_TAB', |
| 41 | + windowId, |
| 42 | + } |
| 43 | + browser.runtime.sendMessage(message) |
| 44 | + window.close() |
| 45 | +} |
| 46 | + |
| 47 | +function createSpotElement(commentState: CommentState): HTMLElement { |
| 48 | + const item = document.createElement('div') |
| 49 | + item.className = 'spot-item' |
| 50 | + |
| 51 | + logger.debug('Creating spot element for:', commentState.spot) |
| 52 | + const enhancer = enhancers.enhancerFor(commentState.spot) |
| 53 | + if (!enhancer) { |
| 54 | + logger.error('No enhancer found for:', commentState.spot) |
| 55 | + logger.error('Only have enhancers for:', enhancers.byType) |
26 | 56 | }
|
| 57 | + |
| 58 | + const title = document.createElement('div') |
| 59 | + title.className = 'spot-title' |
| 60 | + title.textContent = enhancer.tableTitle(commentState.spot) |
| 61 | + item.appendChild(title) |
| 62 | + item.addEventListener('click', () => { |
| 63 | + switchToTab(commentState.tab.tabId, commentState.tab.windowId) |
| 64 | + }) |
| 65 | + return item |
| 66 | +} |
| 67 | + |
| 68 | +async function renderOpenSpots(): Promise<void> { |
| 69 | + logger.debug('renderOpenSpots called') |
| 70 | + const app = document.getElementById('app')! |
| 71 | + const spots = await getOpenSpots() |
| 72 | + logger.debug('Got spots:', spots) |
| 73 | + |
| 74 | + if (spots.length === 0) { |
| 75 | + app.innerHTML = '<div class="no-spots">No open comment spots</div>' |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + const header = document.createElement('h2') |
| 80 | + header.textContent = 'Open Comment Spots' |
| 81 | + app.appendChild(header) |
| 82 | + |
| 83 | + const list = document.createElement('div') |
| 84 | + list.className = 'spots-list' |
| 85 | + |
| 86 | + spots.forEach((spot) => { |
| 87 | + list.appendChild(createSpotElement(spot)) |
| 88 | + }) |
| 89 | + |
| 90 | + app.appendChild(list) |
| 91 | +} |
| 92 | + |
| 93 | +renderOpenSpots().catch((error) => { |
| 94 | + logger.error('Error in renderOpenSpots:', error) |
| 95 | + const app = document.getElementById('app')! |
| 96 | + app.innerHTML = `<div class="no-spots">Error loading spots: ${error.message}</div>` |
27 | 97 | })
|
0 commit comments