All files / services actionHint.ts

96.66% Statements 58/60
85.71% Branches 18/21
100% Functions 4/4
96.66% Lines 58/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 611x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 3x 3x 1x 9x 9x 7x 7x 9x 9x 9x 1x 8x 8x 8x 8x 4x 4x 4x 8x 8x 8x 8x 8x     8x  
/**
 * Toolbar "Go" badge: while the panel is asking the user to click the
 * extension icon, the icon itself is marked so the eye goes straight to it.
 *
 * The panel holds a long-lived port and posts the tab that needs the click
 * (or null). The port disconnecting — the panel closed — clears the badge, so
 * it never outlives the prompt that explains it.
 */
export const ACTION_HINT_PORT = "job-panel-action-hint";
 
export interface ActionHintMessage {
  tabId: number | null;
}
 
const HINT_TEXT = "Go";
const HINT_COLOR = "#2563eb";
const HINT_TITLE = "Click to analyze this page";
 
let hintedTabId: number | null = null;
 
export function listenForActionHints(): void {
  chrome.runtime.onConnect.addListener((port) => {
    if (port.name !== ACTION_HINT_PORT) return;
    port.onMessage.addListener((message: ActionHintMessage) => {
      void setActionHint(message?.tabId ?? null);
    });
    port.onDisconnect.addListener(() => {
      void setActionHint(null);
    });
  });
}
 
/** The toolbar click happened: the hint has done its job on that tab. */
export function clearActionHint(tabId: number): void {
  if (tabId === hintedTabId) void setActionHint(null);
}
 
export async function setActionHint(tabId: number | null): Promise<void> {
  if (tabId === hintedTabId) return;
  const previous = hintedTabId;
  hintedTabId = tabId;
  if (previous !== null) await paint(previous, false);
  if (tabId !== null) await paint(tabId, true);
}
 
async function paint(tabId: number, on: boolean): Promise<void> {
  try {
    await chrome.action.setBadgeText({ tabId, text: on ? HINT_TEXT : "" });
    if (on) {
      await chrome.action.setBadgeBackgroundColor({ tabId, color: HINT_COLOR });
      await chrome.action.setBadgeTextColor?.({ tabId, color: "#ffffff" });
    }
    await chrome.action.setTitle({
      tabId,
      title: on ? HINT_TITLE : chrome.runtime.getManifest().name,
    });
  } catch {
    // Tab closed in the meantime — nothing left to paint.
  }
}