inject.tsx

3.6 KB · raw

1import { render } from 'solid-js/web';2import { PasskeyToast } from './PasskeyToast';3// ?inline imports the CSS as a string so we can inject it into the shadow root4// rather than letting Vite/crx inject it into the page document head.5import styles from './passkey-toast.css?inline';6import { ACCENT_COLORS } from '../../shared/types';7import { notifyPasskeyToastClosed } from './lifecycle';89let currentDispose: (() => void) | null = null;10let currentRequestId: string | null = null;11let toastContainer: HTMLElement | null = null;12let shadowRoot: ShadowRoot | null = null;1314function ensureContainer(): ShadowRoot {15  if (!toastContainer) {16    toastContainer = document.createElement('div');17    toastContainer.id = 'peach-passkey-toast-root';18    shadowRoot = toastContainer.attachShadow({ mode: 'closed' });19    document.body.appendChild(toastContainer);2021    const style = document.createElement('style');22    style.textContent = styles;23    shadowRoot.appendChild(style);24  }25  return shadowRoot!;26}2728function applyAccentToShadow(): void {29  if (!shadowRoot) return;30  chrome.runtime.sendMessage({ type: 'GET_SETTINGS' }).then((resp: unknown) => {31    const name = (resp as { ok?: boolean; data?: { accentColor?: string } } | null)?.data?.accentColor;32    if (!name || !shadowRoot) return;33    const color = ACCENT_COLORS.find(c => c.name === name);34    if (!color) return;35    let el = shadowRoot.querySelector<HTMLStyleElement>('#peach-accent-var');36    if (!el) {37      el = document.createElement('style');38      el.id = 'peach-accent-var';39      shadowRoot.appendChild(el);40    }41    el.textContent = `* { --peach-accent: ${color.hex}; }`;42  }).catch(() => {});43}4445export function showPasskeyToast(payload: {46  site: string;47  origin?: string;48  displayName: string;49  username: string;50  requestId: string;51  toastToken: string;52}) {53  // Dismiss any existing toast, actively denying the request it holds.54  if (currentDispose) {55    dismissPasskeyToast(currentRequestId ?? undefined);56  }5758  const root = ensureContainer();59  applyAccentToShadow();60  const wrapper = document.createElement('div');61  root.appendChild(wrapper);6263  let dispose: (() => void) | null = null;6465  dispose = render(() => (66    <PasskeyToast67      {...payload}68      onSignIn={() => {69        chrome.runtime.sendMessage({70          type: 'PASSKEY_AUTH_RESPONSE',71          payload: { requestId: payload.requestId, approved: true },72        });73        cleanup();74      }}75      onDeny={() => {76        chrome.runtime.sendMessage({77          type: 'PASSKEY_AUTH_RESPONSE',78          payload: { requestId: payload.requestId, approved: false },79        });80        cleanup();81      }}82    />83  ), wrapper);8485  currentDispose = dispose;86  currentRequestId = payload.requestId;8788  function cleanup() {89    const ownedDispose = dispose;90    if (ownedDispose) {91      ownedDispose();92      dispose = null;93    }94    wrapper.remove();95    if (currentDispose === ownedDispose) {96      currentDispose = null;97      currentRequestId = null;98    }99    notifyPasskeyToastClosed(payload.toastToken);100  }101}102103export function dismissPasskeyToast(denyRequestId?: string) {104  if (denyRequestId) {105    chrome.runtime.sendMessage({106      type: 'PASSKEY_AUTH_RESPONSE',107      payload: { requestId: denyRequestId, approved: false },108    });109  }110  if (currentDispose) {111    currentDispose();112    currentDispose = null;113    currentRequestId = null;114  }115}116117chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {118  if (msg.type === 'SHOW_PASSKEY_TOAST') {119    showPasskeyToast(msg.payload);120    sendResponse({ ok: true });121  }122  if (msg.type === 'DISMISS_PASSKEY_TOAST') {123    dismissPasskeyToast();124    sendResponse({ ok: true });125  }126});