aboutsummaryrefslogtreecommitdiff
path: root/extension/src/webext-bridge/internal/endpoint.ts
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src/webext-bridge/internal/endpoint.ts')
-rw-r--r--extension/src/webext-bridge/internal/endpoint.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/extension/src/webext-bridge/internal/endpoint.ts b/extension/src/webext-bridge/internal/endpoint.ts
new file mode 100644
index 0000000..0c271f2
--- /dev/null
+++ b/extension/src/webext-bridge/internal/endpoint.ts
@@ -0,0 +1,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}` : ''}`
+}