aboutsummaryrefslogtreecommitdiff
path: root/extension/src/webext-bridge/internal/endpoint.ts
blob: 0c271f2ca6380579f17ec56d575db854b5ef5c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { Endpoint, RuntimeContext } from '../types'

const ENDPOINT_RE = /^((?:background$)|devtools|popup|options|content-script|window)(?:@(\d+)(?:\.(\d+))?)?$/

export const parseEndpoint = (endpoint: string): Endpoint => {
  const [, context, tabId, frameId] = endpoint.match(ENDPOINT_RE) || []

  return {
    context: context as RuntimeContext,
    tabId: +tabId,
    frameId: frameId ? +frameId : undefined,
  }
}

export const formatEndpoint = ({ context, tabId, frameId }: Endpoint): string => {
  if (['background', 'popup', 'options'].includes(context))
    return context

  return `${context}@${tabId}${frameId ? `.${frameId}` : ''}`
}