ChemPal Documentation - v1.18.0
    Preparing search index...
    • Answers the background worker when it asks whether this page controls the badge.

      Parameters

      • isOwner: () => boolean

        Whether it does right now

      Returns () => void

      Stops answering

      const release = claimBadge(() => document.visibilityState === 'visible');
      
      export function claimBadge(isOwner: () => boolean): () => void {
      const handleMessage = (
      message: unknown,
      _sender: chrome.runtime.MessageSender,
      sendResponse: (response: boolean) => void,
      ): void => {
      if (typeof message !== 'object' || message === null) return;
      // Only the owner answers, so it is heard when several pages are open.
      if (Reflect.get(message, 'type') === MESSAGE_TYPE.BADGE_OWNER && isOwner()) sendResponse(true);
      };
      chrome.runtime?.onMessage?.addListener(handleMessage);
      return () => chrome.runtime?.onMessage?.removeListener(handleMessage);
      }