adnbn 0.0.39 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/dist/browser/index.cjs +122 -76
  2. package/dist/browser/index.cjs.map +1 -1
  3. package/dist/browser/index.d.mts +6 -1
  4. package/dist/browser/index.d.ts +6 -1
  5. package/dist/browser/index.js +120 -77
  6. package/dist/browser/index.js.map +1 -1
  7. package/dist/cli/index.cjs +39 -32
  8. package/dist/cli/index.cjs.map +1 -1
  9. package/dist/entry/command/index.cjs +24 -4
  10. package/dist/entry/command/index.cjs.map +1 -1
  11. package/dist/entry/command/index.js +24 -4
  12. package/dist/entry/command/index.js.map +1 -1
  13. package/dist/entry/service/index.cjs +77 -40
  14. package/dist/entry/service/index.cjs.map +1 -1
  15. package/dist/entry/service/index.js +77 -40
  16. package/dist/entry/service/index.js.map +1 -1
  17. package/dist/index.cjs +76 -39
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.js +76 -39
  20. package/dist/index.js.map +1 -1
  21. package/dist/message/index.cjs +75 -37
  22. package/dist/message/index.cjs.map +1 -1
  23. package/dist/message/index.d.mts +12 -10
  24. package/dist/message/index.d.ts +12 -10
  25. package/dist/message/index.js +75 -38
  26. package/dist/message/index.js.map +1 -1
  27. package/dist/message/react.cjs +74 -37
  28. package/dist/message/react.cjs.map +1 -1
  29. package/dist/message/react.d.mts +2 -2
  30. package/dist/message/react.d.ts +2 -2
  31. package/dist/message/react.js +74 -37
  32. package/dist/message/react.js.map +1 -1
  33. package/dist/message-CXzpmlXD.d.mts +28 -0
  34. package/dist/message-CXzpmlXD.d.ts +28 -0
  35. package/dist/relay/index.cjs +73 -68
  36. package/dist/relay/index.cjs.map +1 -1
  37. package/dist/relay/index.d.mts +34 -20
  38. package/dist/relay/index.d.ts +34 -20
  39. package/dist/relay/index.js +72 -66
  40. package/dist/relay/index.js.map +1 -1
  41. package/dist/service/index.cjs +77 -40
  42. package/dist/service/index.cjs.map +1 -1
  43. package/dist/service/index.d.mts +2 -2
  44. package/dist/service/index.d.ts +2 -2
  45. package/dist/service/index.js +77 -40
  46. package/dist/service/index.js.map +1 -1
  47. package/dist/storage/index.cjs.map +1 -1
  48. package/dist/storage/index.js.map +1 -1
  49. package/dist/storage/react.cjs.map +1 -1
  50. package/dist/storage/react.js.map +1 -1
  51. package/package.json +6 -5
  52. package/dist/message-DO3GF4T9.d.mts +0 -27
  53. package/dist/message-DO3GF4T9.d.ts +0 -27
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/browser/browser.ts","../../src/browser/runtime.ts","../../src/browser/scripting.ts","../../src/types/relay.ts","../../src/relay/ProxyRelay.ts","../../src/relay/RelayManager.ts","../../src/relay/BaseRelay.ts","../../src/relay/RegisterRelay.ts","../../src/relay/index.ts"],"names":["path","args"],"mappings":";;;AAAO,IAAM,UAAU,MAAqB;AACxC,EAAO,OAAA,MAAA;AACX,CAAA;;;ACMA,IAAM,OAAA,GAAU,MAAM,OAAA,EAAU,CAAA,OAAA;AA0DzB,IAAM,oBAAoB,MAAY;AACzC,EAAM,MAAA,KAAA,GAAQ,SAAU,CAAA,SAAA;AAExB,EAAA,IAAI,KAAO,EAAA;AACP,IAAM,MAAA,IAAI,KAAM,CAAA,KAAA,CAAM,OAAO,CAAA;AAAA;AAErC,CAAA;;;AC9DO,IAAM,SAAA,GAAY,MAAM,OAAA,EAAU,CAAA,SAAA;AAElC,IAAM,oBAAuB,GAAA,MAAe,CAAC,CAAC,SAAU,EAAA;AAExD,IAAM,gBAAgB,CAAU,SAAA,KAAuC,IAAI,OAAuC,CAAA,CAAC,SAAS,MAAW,KAAA;AAC1I,EAAA,SAAA,EAAY,CAAA,aAAA,CAAc,SAAW,EAAA,CAAC,MAAW,KAAA;AAC7C,IAAI,IAAA;AACA,MAAkB,iBAAA,EAAA;AAElB,MAAA,OAAA,CAAQ,MAAM,CAAA;AAAA,aACT,CAAG,EAAA;AACR,MAAA,MAAA,CAAO,C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AAI,KAAK,IAAI,CAAA;AAAA;AAEvD,CAAA;;;ACVqB,IAAA,aAAA,GAArB,cAA0F,SAAa,CAAA;AAAA,EAEnG,WAAA,CAAY,MAAiC,IAA6B,EAAA;AACtE,IAAA,KAAA,CAAM,IAAI,CAAA;AAD+B,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AAAA;AAE7C,EAEO,YAAY,IAAa,EAAA;AAC5B,IAAA,IAAI,aAAa,WAAY,EAAA,CAAE,GAAI,CAAA,IAAA,CAAK,IAAI,CAAG,EAAA;AAC3C,MAAA,MAAM,IAAI,KAAA,CAAM,CAA0B,uBAAA,EAAA,IAAA,CAAK,IAAI,CAAkD,gDAAA,CAAA,CAAA;AAAA;AAEzG,IAAa,YAAA,CAAA,WAAA,GAAc,GAAI,CAAA,IAAA,CAAK,MAAM,IAAK,CAAA,IAAA,CAAK,GAAG,IAAI,CAAC,CAAA;AAAA;AAEpE;;;ACNa,IAAA,QAAA,GAAW,CAAsB,IAAA,EAAc,OAAsC,KAAA;AAC9F,EAAA,OAAO,IAAI,UAAA,CAAc,IAAI,CAAA,CAAE,IAAI,OAAO,CAAA;AAC9C;AAEa,IAAA,kBAAA,GAAqB,CAAsB,IAAiB,KAAA;AACrE,EAAA,OAAO,IAAI,SAAA,CAAa,IAAI,CAAA,CAAE,GAAI,EAAA;AACtC","file":"index.js","sourcesContent":["export const browser = (): typeof chrome => {\n return chrome;\n}","import {browser} from \"./browser\";\nimport {ManifestVersion} from \"@typing/manifest\";\n\ntype Manifest = chrome.runtime.Manifest;\ntype PlatformInfo = chrome.runtime.PlatformInfo;\ntype ContextFilter = chrome.runtime.ContextFilter;\ntype ExtensionContext = chrome.runtime.ExtensionContext;\n\nconst runtime = () => browser().runtime;\n\nconst backgroundPaths = [\n '/_generated_background_page.view',\n];\n\nexport const getId = (): string => runtime().id;\n\nexport const getUrl = (path: string) => runtime().getURL(path);\n\nexport const getManifest = (): Manifest => runtime().getManifest();\n\nexport const getManifestVersion = (): ManifestVersion => getManifest().manifest_version;\n\nexport const getRuntimeContexts = (filter: ContextFilter) => new Promise<ExtensionContext[]>((resolve, reject) => {\n runtime().getContexts(filter, contexts => {\n try {\n throwRuntimeError();\n\n resolve(contexts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getPlatformInfo = (): Promise<PlatformInfo> => new Promise<PlatformInfo>((resolve, reject) => {\n runtime().getPlatformInfo((platformInfo) => {\n try {\n throwRuntimeError();\n\n resolve(platformInfo);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const isManifestVersion3 = (): boolean => getManifestVersion() === 3;\n\nexport const isBackground = (): boolean => {\n if (!getId()) {\n return false;\n }\n\n const manifest = getManifest();\n\n if (!manifest.background) {\n return false;\n }\n\n if (manifest.manifest_version === 3) {\n return typeof window === \"undefined\";\n }\n\n return window !== undefined && backgroundPaths.includes(location.pathname);\n}\n\nexport const throwRuntimeError = (): void => {\n const error = runtime().lastError;\n\n if (error) {\n throw new Error(error.message);\n }\n}\n\nexport const onRuntimeInstalled = (callback: Parameters<typeof chrome.runtime.onInstalled.addListener>[0]): () => void => {\n runtime().onInstalled.addListener(callback);\n\n return () => runtime().onInstalled.removeListener(callback);\n}\n","import {browser} from \"./browser\";\nimport {throwRuntimeError} from \"./runtime\";\n\ntype Awaited<T> = chrome.scripting.Awaited<T>;\ntype CSSInjection = chrome.scripting.CSSInjection;\ntype ScriptInjection<Args extends any[], Result> = chrome.scripting.ScriptInjection<Args, Result>;\ntype InjectionResult<T> = chrome.scripting.InjectionResult<T>;\ntype ContentScriptFilter = chrome.scripting.ContentScriptFilter;\ntype RegisteredContentScript = chrome.scripting.RegisteredContentScript;\n\nexport const scripting = () => browser().scripting;\n\nexport const isAvailableScripting = (): boolean => !!scripting()\n\nexport const executeScript = <T = any>(injection: ScriptInjection<any, T>) => new Promise<InjectionResult<Awaited<T>>[]>((resolve, reject) => {\n scripting().executeScript(injection, (result) => {\n try {\n throwRuntimeError();\n\n resolve(result);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const registerContentScripts = (scripts: RegisteredContentScript[]) => new Promise<void>((resolve, reject) => {\n scripting().registerContentScripts(scripts, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const unregisterContentScripts = (filter?: ContentScriptFilter) => new Promise<void>((resolve, reject) => {\n scripting().unregisterContentScripts(filter || {}, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const updateContentScripts = (scripts: RegisteredContentScript[]) => new Promise<void>((resolve, reject) => {\n scripting().updateContentScripts(scripts, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getRegisteredContentScripts = (filter?: ContentScriptFilter) => new Promise<RegisteredContentScript[]>((resolve, reject) => {\n scripting().getRegisteredContentScripts(filter || {}, (scripts) => {\n try {\n throwRuntimeError();\n\n resolve(scripts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const insertScriptingCSS = (injection: CSSInjection) => new Promise<void>((resolve, reject) => {\n scripting().insertCSS(injection, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const removeScriptingCSS = (injection: CSSInjection) => new Promise<void>((resolve, reject) => {\n scripting().removeCSS(injection, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n","export const RelayWindowKey = '__relay';\n\nexport type RelayType = ((...args: any[]) => Promise<any>) | { [key: string]: any | RelayType };\n","import {executeScript, isAvailableScripting} from \"@browser/scripting\";\nimport {DeepAsyncProxy} from \"@typing/helpers\";\nimport {RelayType, RelayWindowKey} from \"@typing/relay\";\n\ntype InjectionTarget = chrome.scripting.InjectionTarget;\n\nexport default class ProxyRelay<T extends RelayType> {\n\n constructor(protected readonly name: string) {\n }\n\n private createProxy(options: number | InjectionTarget, path?: string): DeepAsyncProxy<T> {\n const wrapped = () => {\n }\n\n const target = typeof options === 'number' ? {tabId: options} : options;\n\n const proxy = new Proxy(wrapped, {\n apply: async (_target, _thisArg, args) => {\n const result = await executeScript({\n target,\n\n func: async (name: string, path: string, args: any[], key: string) => {\n try {\n return await window[key].property({name, path, args})\n } catch (error) {\n console.error('ProxyRelay.createProxy()', error)\n throw error\n }\n },\n\n args: [this.name, path!, args, RelayWindowKey],\n });\n\n return result?.[0]?.result;\n },\n\n get: (_target, propertyName, receiver) => {\n if (propertyName === '__proxy' || typeof propertyName !== 'string') {\n return Reflect.get(wrapped, propertyName, receiver);\n }\n const newPath = path == null ? propertyName : `${path}.${propertyName}`;\n return this.createProxy(options, newPath);\n },\n });\n\n proxy['__proxy'] = true;\n\n return proxy as unknown as DeepAsyncProxy<T>;\n }\n\n public get(options: number | InjectionTarget): DeepAsyncProxy<T> {\n if (!isAvailableScripting()) {\n throw new Error(`You are trying to get proxy relay ${this.name} from script content. You can get original relay instead`);\n }\n\n return this.createProxy(options) as DeepAsyncProxy<T>;\n }\n}\n","import get from 'get-value'\nimport {RelayWindowKey} from \"@typing/relay\";\n\ntype PropertyType = {\n name: string,\n path?: string,\n args?: any[],\n options?: get.Options,\n}\n\nexport default class RelayManager {\n private instances = new Map<string, any>();\n\n public static getInstance(): RelayManager {\n if (window[RelayWindowKey] === undefined) {\n window[RelayWindowKey] = new RelayManager();\n }\n return window[RelayWindowKey];\n }\n\n public async property({name, path, args, options}: PropertyType): Promise<any> {\n const relay = this.get(name)\n const property = path == null ? relay : get(relay, path, options)\n\n if (property === undefined) {\n throw new Error(`Property not found at path \"${path}\" in relay \"${name}\"`)\n }\n\n if (typeof property === 'function') {\n return await property.apply(relay, args);\n }\n\n return property\n }\n\n public add(name: string, instance: any) {\n this.instances.set(name, instance);\n }\n\n public get(name: string): any | undefined {\n return this.instances.get(name);\n }\n\n public has(name: string): boolean {\n return this.instances.has(name);\n }\n\n public remove(name: string): any | undefined {\n const instance = this.get(name);\n this.instances.delete(name);\n return instance;\n }\n\n public clear(): void {\n this.instances.clear();\n }\n}\n","import {isAvailableScripting} from \"@browser/scripting\";\nimport RelayManager from \"./RelayManager\";\n\nexport default class BaseRelay<T> {\n\n constructor(protected readonly name: string){\n }\n\n public get(): T {\n if(isAvailableScripting()){\n throw new Error(`Relay \"${this.name}\" can be getting only from content script`);\n }\n return RelayManager.getInstance().get(this.name);\n }\n}\n","import {RelayType} from \"@typing/relay\";\nimport RelayManager from \"./RelayManager\";\nimport BaseRelay from \"./BaseRelay\";\n\nexport default class RegisterRelay<T extends RelayType, TArgs extends any[] = []> extends BaseRelay<T> {\n\n constructor(name: string, protected readonly init: (...args: TArgs) => T) {\n super(name)\n }\n\n public register(...args: TArgs) {\n if (RelayManager.getInstance().has(this.name)) {\n throw new Error(`A relay with the name \"${this.name}\" already exists. The relay name must be unique.`);\n }\n RelayManager.getInstance().add(this.name, this.init(...args))\n }\n}\n","import ProxyRelay from \"./ProxyRelay\";\nimport BaseRelay from \"./BaseRelay\";\n\nimport {RelayType} from \"@typing/relay\";\n\ntype InjectionTarget = chrome.scripting.InjectionTarget;\n\nexport {default as ProxyRelay} from './ProxyRelay'\nexport {default as RegisterRelay} from \"./RegisterRelay\";\n\nexport const getRelay = <T extends RelayType>(name: string, options: number | InjectionTarget) => {\n return new ProxyRelay<T>(name).get(options);\n}\n\nexport const getRegisteredRelay = <T extends RelayType>(name: string) => {\n return new BaseRelay<T>(name).get();\n}\n"]}
1
+ {"version":3,"sources":["../../src/browser/browser.ts","../../src/browser/runtime.ts","../../src/browser/scripting.ts","../../src/types/relay.ts","../../src/relay/RelayManager.ts","../../src/relay/providers/BaseRelay.ts","../../src/relay/providers/ProxyRelay.ts","../../src/relay/providers/RegisterRelay.ts","../../src/relay/index.ts"],"names":["path","args"],"mappings":";;;AAAO,IAAM,UAAU,MAAqB;AACxC,EAAO,OAAA,MAAA;AACX,CAAA;;;ACSA,IAAM,OAAA,GAAU,MAAM,OAAA,EAAU,CAAA,OAAA;AA0DzB,IAAM,oBAA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AI,CAAkD,gDAAA,CAAA,CAAA;AAAA;AAGzG,IAAK,IAAA,CAAA,OAAA,CAAQ,IAAI,IAAK,CAAA,IAAA,EAAM,KAAK,IAAK,CAAA,GAAG,IAAI,CAAC,CAAA;AAAA;AAEtD;;;ACda,IAAA,QAAA,GAAW,CAAsB,IAAgC,KAAA;AAC1E,EAAA,OAAO,IAAI,iBAAA,CAAa,IAAI,CAAA,CAAE,GAAI,EAAA;AACtC","file":"index.js","sourcesContent":["export const browser = (): typeof chrome => {\n return chrome;\n}","import {browser} from \"./browser\";\nimport {handleListener} from \"./utils\";\n\nimport {ManifestVersion} from \"@typing/manifest\";\nimport {MessageBody, MessageDictionary, MessageResponse, MessageType} from \"@typing/message\";\n\ntype Manifest = chrome.runtime.Manifest;\ntype PlatformInfo = chrome.runtime.PlatformInfo;\ntype ContextFilter = chrome.runtime.ContextFilter;\ntype ExtensionContext = chrome.runtime.ExtensionContext;\n\nconst runtime = () => browser().runtime;\n\nconst backgroundPaths = [\n '/_generated_background_page.view',\n];\n\nexport const getId = (): string => runtime().id;\n\nexport const getUrl = (path: string) => runtime().getURL(path);\n\nexport const getManifest = (): Manifest => runtime().getManifest();\n\nexport const getManifestVersion = (): ManifestVersion => getManifest().manifest_version;\n\nexport const getRuntimeContexts = (filter: ContextFilter) => new Promise<ExtensionContext[]>((resolve, reject) => {\n runtime().getContexts(filter, contexts => {\n try {\n throwRuntimeError();\n\n resolve(contexts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getPlatformInfo = (): Promise<PlatformInfo> => new Promise<PlatformInfo>((resolve, reject) => {\n runtime().getPlatformInfo((platformInfo) => {\n try {\n throwRuntimeError();\n\n resolve(platformInfo);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const isManifestVersion3 = (): boolean => getManifestVersion() === 3;\n\nexport const isBackground = (): boolean => {\n if (!getId()) {\n return false;\n }\n\n const manifest = getManifest();\n\n if (!manifest.background) {\n return false;\n }\n\n if (manifest.manifest_version === 3) {\n return typeof window === \"undefined\";\n }\n\n return window !== undefined && backgroundPaths.includes(location.pathname);\n}\n\nexport const throwRuntimeError = (): void => {\n const error = runtime().lastError;\n\n if (error) {\n throw new Error(error.message);\n }\n}\n\nexport const sendMessage = <\n T extends MessageDictionary,\n K extends MessageType<T>\n>(\n message: MessageBody<T, K>\n): Promise<MessageResponse<T, K>> => new Promise<MessageResponse<T, K>>((resolve, reject) => {\n runtime().sendMessage(message, (response) => {\n try {\n throwRuntimeError();\n\n resolve(response);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const onRuntimeInstalled = (callback: Parameters<typeof chrome.runtime.onInstalled.addListener>[0]): () => void => {\n return handleListener(runtime().onInstalled, callback)\n}\n\nexport const onMessage = (callback: Parameters<typeof chrome.runtime.onMessage.addListener>[0]): () => void => {\n return handleListener(runtime().onMessage, callback)\n}\n","import {browser} from \"./browser\";\nimport {throwRuntimeError} from \"./runtime\";\n\ntype Awaited<T> = chrome.scripting.Awaited<T>;\ntype CSSInjection = chrome.scripting.CSSInjection;\ntype ScriptInjection<Args extends any[], Result> = chrome.scripting.ScriptInjection<Args, Result>;\ntype InjectionResult<T> = chrome.scripting.InjectionResult<T>;\ntype ContentScriptFilter = chrome.scripting.ContentScriptFilter;\ntype RegisteredContentScript = chrome.scripting.RegisteredContentScript;\n\nexport const scripting = () => browser().scripting;\n\nexport const isAvailableScripting = (): boolean => !!scripting()\n\nexport const executeScript = <T = any>(injection: ScriptInjection<any, T>) => new Promise<InjectionResult<Awaited<T>>[]>((resolve, reject) => {\n scripting().executeScript(injection, (result) => {\n try {\n throwRuntimeError();\n\n resolve(result);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const registerContentScripts = (scripts: RegisteredContentScript[]) => new Promise<void>((resolve, reject) => {\n scripting().registerContentScripts(scripts, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const unregisterContentScripts = (filter?: ContentScriptFilter) => new Promise<void>((resolve, reject) => {\n scripting().unregisterContentScripts(filter || {}, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const updateContentScripts = (scripts: RegisteredContentScript[]) => new Promise<void>((resolve, reject) => {\n scripting().updateContentScripts(scripts, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getRegisteredContentScripts = (filter?: ContentScriptFilter) => new Promise<RegisteredContentScript[]>((resolve, reject) => {\n scripting().getRegisteredContentScripts(filter || {}, (scripts) => {\n try {\n throwRuntimeError();\n\n resolve(scripts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const insertScriptingCSS = (injection: CSSInjection) => new Promise<void>((resolve, reject) => {\n scripting().insertCSS(injection, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const removeScriptingCSS = (injection: CSSInjection) => new Promise<void>((resolve, reject) => {\n scripting().removeCSS(injection, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n","import get from \"get-value\";\n\nexport const RelayGlobalKey = 'adnbnRelay';\n\nexport type RelayType = ((...args: any[]) => Promise<any>) | { [key: string]: any | RelayType };\n\nexport interface RelayDictionary {\n [key: string]: any;\n}\n\nexport type RelayName = Extract<keyof RelayDictionary, string>;\n\nexport type PropertyOptions = {\n path?: string,\n args?: any[],\n getOptions?: get.Options,\n}\n\nexport interface RelayManager {\n property(name: RelayName, options: PropertyOptions): Promise<any>;\n\n add<K extends RelayName>(\n name: K,\n relay: RelayDictionary[K]\n ): this;\n\n get<K extends RelayName>(\n name: K\n ): RelayDictionary[K] | undefined;\n\n has(name: RelayName): boolean;\n\n remove<K extends RelayName>(\n name: K\n ): RelayDictionary[K] | undefined;\n\n clear(): this;\n}\n\n","import get from 'get-value'\nimport {\n PropertyOptions,\n RelayDictionary,\n RelayGlobalKey,\n RelayManager as RelayManagerContract,\n RelayName\n} from \"@typing/relay\";\n\nexport default class RelayManager implements RelayManagerContract {\n private relays = new Map<\n RelayName,\n RelayDictionary[RelayName]\n >();\n\n public static getInstance(): RelayManagerContract {\n return globalThis[RelayGlobalKey] ??= new RelayManager();\n }\n\n public async property(\n name: RelayName,\n options?: PropertyOptions\n ): Promise<any> {\n const {path, args, getOptions} = options || {};\n const relay = this.get(name)\n\n const property = path == null ? relay : get(relay, path, getOptions)\n\n if (property === undefined) {\n throw new Error(`Property not found at path \"${path}\" in relay \"${name}\"`)\n }\n\n if (typeof property === 'function') {\n return await property.apply(relay, args);\n }\n\n return property\n }\n\n public add<K extends RelayName>(\n name: K,\n relay: RelayDictionary[K]\n ): this {\n this.relays.set(name, relay);\n\n return this;\n }\n\n public get<K extends RelayName>(\n name: K\n ): RelayDictionary[K] | undefined {\n return this.relays.get(name) as RelayDictionary[K] | undefined;\n }\n\n public has(name: RelayName): boolean {\n return this.relays.has(name);\n }\n\n public remove<K extends RelayName>(\n name: K\n ): RelayDictionary[K] | undefined {\n const relay = this.get(name);\n\n this.relays.delete(name);\n\n return relay;\n }\n\n public clear(): this {\n this.relays.clear();\n\n return this;\n }\n}\n","import {isAvailableScripting} from \"@browser/scripting\";\nimport RelayManager from \"../RelayManager\";\n\nimport {RelayDictionary, RelayManager as RelayManagerContract, RelayName} from \"@typing/relay\";\n\nexport default class<N extends RelayName, T = RelayDictionary[N]> {\n constructor(protected readonly name: N){\n }\n\n protected get manager(): RelayManagerContract {\n return RelayManager.getInstance();\n }\n\n public get(): T {\n if(isAvailableScripting()){\n throw new Error(`Relay \"${this.name}\" can be getting only from content script`);\n }\n\n return this.manager.get(this.name);\n }\n\n public destroy(): void {\n this.manager.remove(this.name);\n }\n}\n","import {executeScript, isAvailableScripting} from \"@browser/scripting\";\n\nimport {DeepAsyncProxy} from \"@typing/helpers\";\nimport {RelayGlobalKey, RelayName, RelayDictionary} from \"@typing/relay\";\n\ntype InjectionTarget = chrome.scripting.InjectionTarget;\n\nexport default class<N extends RelayName, T = DeepAsyncProxy<RelayDictionary[N]>> {\n constructor(protected readonly name: N) {\n }\n\n private createProxy(options: number | InjectionTarget, path?: string): T {\n const wrapped = () => {\n }\n\n const target = typeof options === 'number' ? {tabId: options} : options;\n\n const proxy = new Proxy(wrapped, {\n apply: async (_target, _thisArg, args) => {\n const result = await executeScript({\n target,\n\n func: async (name: string, path: string, args: any[], key: string) => {\n try {\n return await globalThis[key].property({name, path, args})\n } catch (error) {\n console.error('ProxyRelay.createProxy()', error)\n throw error\n }\n },\n\n args: [this.name, path!, args, RelayGlobalKey],\n });\n\n return result?.[0]?.result;\n },\n\n get: (_target, propertyName, receiver) => {\n if (propertyName === '__proxy' || typeof propertyName !== 'string') {\n return Reflect.get(wrapped, propertyName, receiver);\n }\n const newPath = path == null ? propertyName : `${path}.${propertyName}`;\n return this.createProxy(options, newPath);\n },\n });\n\n proxy['__proxy'] = true;\n\n return proxy as unknown as T;\n }\n\n public get(options: number | InjectionTarget): T {\n if (!isAvailableScripting()) {\n throw new Error(`You are trying to get proxy relay \"${this.name}\" from script content. You can get original relay instead`);\n }\n\n return this.createProxy(options);\n }\n}\n","import BaseRelay from \"./BaseRelay\";\n\nimport type {RelayDictionary, RelayName} from \"@typing/relay\";\n\nexport default class<\n N extends RelayName,\n T extends object = RelayDictionary[N],\n A extends any[] = []\n> extends BaseRelay<N, T> {\n constructor(name: N, protected readonly init: (...args: A) => T) {\n super(name)\n }\n\n public register(...args: A) {\n if (this.manager.has(this.name)) {\n throw new Error(`A relay with the name \"${this.name}\" already exists. The relay name must be unique.`);\n }\n\n this.manager.add(this.name, this.init(...args))\n }\n}\n","import {BaseRelay, ProxyRelay, RegisterRelay} from \"./providers\";\n\nimport type {RelayDictionary, RelayName} from \"@typing/relay\";\n\nexport {type RelayDictionary, RegisterRelay, ProxyRelay}\n\nexport const getRelay = <N extends RelayName>(name: N): RelayDictionary[N] => {\n return new BaseRelay<N>(name).get();\n}\n"]}
@@ -12,6 +12,28 @@ var browser = () => {
12
12
  return chrome;
13
13
  };
14
14
 
15
+ // src/browser/utils.ts
16
+ function safeListener(listener) {
17
+ return (...args) => {
18
+ try {
19
+ const result = listener(...args);
20
+ if (result instanceof Promise) {
21
+ result.catch((err) => {
22
+ console.error("Listener async error:", err);
23
+ });
24
+ }
25
+ return result;
26
+ } catch (err) {
27
+ console.error("Listener sync error:", err);
28
+ }
29
+ };
30
+ }
31
+ function handleListener(target, callback) {
32
+ const listener = safeListener(callback);
33
+ target.addListener(listener);
34
+ return () => target.removeListener(listener);
35
+ }
36
+
15
37
  // src/browser/runtime.ts
16
38
  var runtime = () => browser().runtime;
17
39
  var backgroundPaths = [
@@ -38,6 +60,19 @@ var throwRuntimeError = () => {
38
60
  throw new Error(error.message);
39
61
  }
40
62
  };
63
+ var sendMessage = (message) => new Promise((resolve, reject) => {
64
+ runtime().sendMessage(message, (response) => {
65
+ try {
66
+ throwRuntimeError();
67
+ resolve(response);
68
+ } catch (e) {
69
+ reject(e);
70
+ }
71
+ });
72
+ });
73
+ var onMessage = (callback) => {
74
+ return handleListener(runtime().onMessage, callback);
75
+ };
41
76
 
42
77
  // src/types/service.ts
43
78
  var ServiceGlobalKey = "adnbnService";
@@ -80,7 +115,7 @@ var BaseService_default = class {
80
115
  }
81
116
  get() {
82
117
  if (!isBackground()) {
83
- throw new Error("BaseService.get() must be called from within the background context.");
118
+ throw new Error(`Service "${this.name}" can be getting only from background context.`);
84
119
  }
85
120
  return this.manager.get(this.name);
86
121
  }
@@ -88,6 +123,19 @@ var BaseService_default = class {
88
123
  this.manager.remove(this.name);
89
124
  }
90
125
  };
126
+
127
+ // src/browser/tab.ts
128
+ var tabs = () => browser().tabs;
129
+ var sendTabMessage = (tabId, message, options = {}) => new Promise((resolve, reject) => {
130
+ tabs().sendMessage(tabId, message, options, (response) => {
131
+ try {
132
+ throwRuntimeError();
133
+ resolve(response);
134
+ } catch (e) {
135
+ reject(e);
136
+ }
137
+ });
138
+ });
91
139
  var AbstractMessage = class {
92
140
  buildMessage(type, data) {
93
141
  return {
@@ -99,16 +147,16 @@ var AbstractMessage = class {
99
147
  }
100
148
  };
101
149
 
150
+ // src/types/message.ts
151
+ var MessageGlobalKey = "adnbnMessage";
152
+
102
153
  // src/message/MessageManager.ts
103
154
  var MessageManager = class _MessageManager {
104
155
  handlers = /* @__PURE__ */ new Set();
105
- isListenerAttached = false;
106
- static instance = null;
156
+ unsubscribe = null;
107
157
  static getInstance() {
108
- if (_MessageManager.instance === null) {
109
- _MessageManager.instance = new _MessageManager();
110
- }
111
- return _MessageManager.instance;
158
+ var _a;
159
+ return globalThis[_a = MessageGlobalKey] ?? (globalThis[_a] = new _MessageManager());
112
160
  }
113
161
  constructor() {
114
162
  this.listener = this.listener.bind(this);
@@ -126,12 +174,11 @@ var MessageManager = class _MessageManager {
126
174
  this.updateListener();
127
175
  }
128
176
  updateListener() {
129
- if (this.handlers.size > 0 && !this.isListenerAttached) {
130
- browser().runtime.onMessage.addListener(this.listener);
131
- this.isListenerAttached = true;
132
- } else if (this.handlers.size === 0 && this.isListenerAttached) {
133
- browser().runtime.onMessage.removeListener(this.listener);
134
- this.isListenerAttached = false;
177
+ if (this.handlers.size > 0 && !this.unsubscribe) {
178
+ this.unsubscribe = onMessage(this.listener);
179
+ } else if (this.handlers.size === 0 && this.unsubscribe) {
180
+ this.unsubscribe();
181
+ this.unsubscribe = null;
135
182
  }
136
183
  }
137
184
  listener(message, sender, sendResponse) {
@@ -205,34 +252,24 @@ var GeneralHandler = class extends AbstractHandler {
205
252
  };
206
253
 
207
254
  // src/message/providers/Message.ts
208
- var Message = class extends AbstractMessage {
209
- manager = MessageManager.getInstance();
255
+ var Message = class _Message extends AbstractMessage {
256
+ static instance = null;
257
+ static getInstance() {
258
+ return this.instance ?? (this.instance = new _Message());
259
+ }
260
+ get manager() {
261
+ return MessageManager.getInstance();
262
+ }
210
263
  send(type, data, options) {
211
264
  const message = this.buildMessage(type, data);
212
- if (typeof options === "number" || typeof options === "object") {
213
- const tabId = typeof options === "number" ? options : options.tabId;
214
- const frameId = typeof options === "object" && options.frameId !== void 0 ? options.frameId : void 0;
215
- return new Promise((resolve, reject) => {
216
- browser().tabs.sendMessage(tabId, message, { frameId }, (response) => {
217
- try {
218
- throwRuntimeError();
219
- resolve(response);
220
- } catch (e) {
221
- reject(e);
222
- }
223
- });
224
- });
265
+ if (options) {
266
+ if (typeof options === "number") {
267
+ return sendTabMessage(options, message);
268
+ }
269
+ const { tabId, ...other } = options;
270
+ return sendTabMessage(tabId, message, other);
225
271
  }
226
- return new Promise((resolve, reject) => {
227
- browser().runtime.sendMessage(message, (response) => {
228
- try {
229
- throwRuntimeError();
230
- resolve(response);
231
- } catch (e) {
232
- reject(e);
233
- }
234
- });
235
- });
272
+ return sendMessage(message);
236
273
  }
237
274
  watch(arg1, arg2) {
238
275
  let handler;
@@ -281,7 +318,7 @@ var ProxyService_default = class extends BaseService_default {
281
318
  }
282
319
  get() {
283
320
  if (isBackground()) {
284
- throw new Error("ProxyService.get() cannot be called in the background");
321
+ throw new Error(`You are trying to get proxy service "${this.name}" from background. You can get original service instead`);
285
322
  }
286
323
  return this.createProxy();
287
324
  }
@@ -316,7 +353,7 @@ var RegisterService_default = class extends ProxyService_default {
316
353
  }
317
354
  get() {
318
355
  if (!isBackground()) {
319
- throw new Error("RegisterService.get() must be called from within the background context.");
356
+ throw new Error(`Service "${this.name}" can be getting only from background context.`);
320
357
  }
321
358
  return this.manager.get(this.name);
322
359
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/browser/browser.ts","../../src/browser/runtime.ts","../../src/types/service.ts","../../src/service/ServiceManager.ts","../../src/service/providers/BaseService.ts","../../src/message/providers/AbstractMessage.ts","../../src/message/MessageManager.ts","../../src/message/handlers/AbstractHandler.ts","../../src/message/handlers/SingleHandler.ts","../../src/message/handlers/MapHandler.ts","../../src/message/handlers/GeneralHandler.ts","../../src/message/providers/Message.ts","../../src/service/providers/ProxyService.ts","../../src/service/providers/RegisterService.ts","../../src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IAAA,CAAA,OAAA,CAAQ,MAAM,IAAK,CAAA,UAAA,EAAY,OAAO,EAAC,IAAA,EAAM,IAAAC,EAAAA,KAAAA,EAAU,KAAA;AACxD,MAAI,IAAA;AACA,QAAA,MAAM,WAAW,IAAQ,IAAA,IAAA,GAAO,OAAU,GAAAC,oBAAA,CAAI,SAAS,IAAI,CAAA;AAE3D,QAAA,IAAI,aAAa,KAAW,CAAA,EAAA;AACxB,UAAA,MAAM,IAAI,KAAM,CAAA,CAAA,4BAAA,EAA+B,IAAI,CAAiB,cAAA,EAAA,IAAA,CAAK,IAAI,CAAG,CAAA,CAAA,CAAA;AAAA;AAGpF,QAAI,IAAA,OAAO,aAAa,UAAY,EAAA;AAChC,UAAA,OAAO,MAAM,QAAA,CAAS,KAAM,CAAA,OAAA,EAASD,KAAI,CAAA;AAAA;AAG7C,QAAO,OAAA,QAAA;AAAA,eAEF,KAAO,EAAA;AACZ,QAAQ,OAAA,CAAA,KAAA,CAAM,8BAA8B,KAAK,CAAA;AAEjD,QAAM,MAAA,KAAA;AAAA;AACV,KACH,CAAA;AAED,IAAO,OAAA,OAAA;AAAA;AACX,EAEO,GAAS,GAAA;AACZ,IAAI,IAAA,CAAC,cAAgB,EAAA;AACjB,MAAM,MAAA,IAAI,MAAM,0EAA0E,CAAA;AAAA;AAG9F,IAAA,OAAO,IAAK,CAAA,OAAA,CAAQ,GAAI,CAAA,IAAA,CAAK,IAAI,CAAA;AAAA;AAEzC;;;AC5Ca,IAAA,UAAA,GAAa,CAAwB,IAAkC,KAAA;AAChF,EAAA,OAAO,IAAI,mBAAA,CAAY,IAAI,CAAA,CAAE,GAAI,EAAA;AACrC","file":"index.cjs","sourcesContent":["export const browser = (): typeof chrome => {\n return chrome;\n}","import {browser} from \"./browser\";\nimport {ManifestVersion} from \"@typing/manifest\";\n\ntype Manifest = chrome.runtime.Manifest;\ntype PlatformInfo = chrome.runtime.PlatformInfo;\ntype ContextFilter = chrome.runtime.ContextFilter;\ntype ExtensionContext = chrome.runtime.ExtensionContext;\n\nconst runtime = () => browser().runtime;\n\nconst backgroundPaths = [\n '/_generated_background_page.view',\n];\n\nexport const getId = (): string => runtime().id;\n\nexport const getUrl = (path: string) => runtime().getURL(path);\n\nexport const getManifest = (): Manifest => runtime().getManifest();\n\nexport const getManifestVersion = (): ManifestVersion => getManifest().manifest_version;\n\nexport const getRuntimeContexts = (filter: ContextFilter) => new Promise<ExtensionContext[]>((resolve, reject) => {\n runtime().getContexts(filter, contexts => {\n try {\n throwRuntimeError();\n\n resolve(contexts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getPlatformInfo = (): Promise<PlatformInfo> => new Promise<PlatformInfo>((resolve, reject) => {\n runtime().getPlatformInfo((platformInfo) => {\n try {\n throwRuntimeError();\n\n resolve(platformInfo);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const isManifestVersion3 = (): boolean => getManifestVersion() === 3;\n\nexport const isBackground = (): boolean => {\n if (!getId()) {\n return false;\n }\n\n const manifest = getManifest();\n\n if (!manifest.background) {\n return false;\n }\n\n if (manifest.manifest_version === 3) {\n return typeof window === \"undefined\";\n }\n\n return window !== undefined && backgroundPaths.includes(location.pathname);\n}\n\nexport const throwRuntimeError = (): void => {\n const error = runtime().lastError;\n\n if (error) {\n throw new Error(error.message);\n }\n}\n\nexport const onRuntimeInstalled = (callback: Parameters<typeof chrome.runtime.onInstalled.addListener>[0]): () => void => {\n runtime().onInstalled.addListener(callback);\n\n return () => runtime().onInstalled.removeListener(callback);\n}\n","import {Required} from \"utility-types\";\n\nimport {EntrypointBuilder, EntrypointOptions} from \"@typing/entrypoint\";\nimport {BackgroundConfig} from \"@typing/background\";\nimport {Awaiter, DeepAsyncProxy} from \"@typing/helpers\";\n\nexport const ServiceGlobalKey = 'adnbnService';\n\nexport type ServiceType = ((...args: any[]) => Promise<any>) | { [key: string]: any | ServiceType };\n\nexport interface ServiceDictionary {\n [key: string]: any;\n}\n\nexport type ServiceName = Extract<keyof ServiceDictionary, string>;\n\nexport type ServiceTarget<T extends ServiceDictionary, K extends keyof T> = T[K];\n\nexport type ServiceProxyTarget<T extends ServiceDictionary, K extends keyof T> = DeepAsyncProxy<T[K]>;\n\nexport interface ServiceManager {\n add<K extends ServiceName>(\n name: K,\n instance: ServiceDictionary[K]\n ): this;\n\n get<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined;\n\n has(name: ServiceName): boolean;\n\n remove<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined;\n\n clear(): this;\n}\n\nexport interface ServiceConfig extends BackgroundConfig {\n name: string;\n}\n\nexport type ServiceOptions = ServiceConfig & EntrypointOptions;\n\nexport type ServiceEntrypointOptions = Partial<ServiceOptions>;\n\nexport type ServiceInitGetter<T extends ServiceType> = (options: ServiceOptions) => T;\n\nexport type ServiceMainHandler<T extends ServiceType> = (service: T, options: ServiceOptions) => Awaiter<void>;\n\nexport type ServiceDefinition<T extends ServiceType> = ServiceEntrypointOptions & {\n init?: ServiceInitGetter<T>;\n main?: ServiceMainHandler<T>;\n};\n\nexport type ServiceUnresolvedDefinition<T extends ServiceType> = Partial<ServiceDefinition<T>>;\n\nexport type ServiceResolvedDefinition<T extends ServiceType> = Required<ServiceDefinition<T>, 'name' | 'init'>;\n\nexport type ServiceBuilder = EntrypointBuilder;\n","import {\n ServiceDictionary,\n ServiceGlobalKey,\n ServiceManager as ServiceManagerContract,\n ServiceName\n} from \"@typing/service\";\n\nexport default class ServiceManager implements ServiceManagerContract {\n private services = new Map<\n ServiceName,\n ServiceDictionary[ServiceName]\n >();\n\n public static getInstance(): ServiceManagerContract {\n return globalThis[ServiceGlobalKey] ??= new ServiceManager();\n }\n\n public add<K extends ServiceName>(\n name: K,\n instance: ServiceDictionary[K]\n ): this {\n this.services.set(name, instance);\n\n return this;\n }\n\n public get<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined {\n return this.services.get(name) as ServiceDictionary[K] | undefined;\n }\n\n public has(name: ServiceName): boolean {\n return this.services.has(name);\n }\n\n public remove<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined {\n const service = this.get(name);\n\n this.services.delete(name);\n\n return service;\n }\n\n public clear(): this {\n this.services.clear();\n\n return this;\n }\n}","import {isBackground} from \"@browser/runtime\";\n\nimport ServiceManager from \"../ServiceManager\";\n\nimport {ServiceDictionary, ServiceManager as ServiceManagerContract, ServiceName} from \"@typing/service\";\n\nexport default class<N extends ServiceName, T = ServiceDictionary[N]> {\n constructor(protected readonly name: N) {\n }\n\n protected get manager(): ServiceManagerContract {\n return ServiceManager.getInstance();\n }\n\n public get(): T {\n if (!isBackground()) {\n throw new Error('BaseService.get() must be called from within the background context.');\n }\n\n return this.manager.get(this.name);\n }\n\n public destroy(): void {\n this.manager.remove(this.name);\n }\n}\n","import {nanoid} from 'nanoid';\nimport {\n MessageBody,\n MessageData,\n MessageGeneralHandler,\n MessageTargetHandler,\n MessageMap,\n MessageMapHandler,\n MessageProvider,\n MessageResponse,\n MessageType\n} from '@typing/message'\n\nexport default abstract class AbstractMessage<T extends MessageMap, TOptions> implements MessageProvider<T, TOptions> {\n public abstract send<K extends MessageType<T>>(type: K, data: MessageData<T, K>, options?: TOptions): Promise<MessageResponse<T, K>>;\n\n public abstract watch<K extends MessageType<T>>(type: K, handler: MessageTargetHandler<T, K>): () => void;\n\n public abstract watch(map: MessageMapHandler<T>): () => void;\n\n public abstract watch<K extends MessageType<T>>(general: MessageGeneralHandler<T, K>): () => void;\n\n protected buildMessage<K extends MessageType<T>>(type: K, data: MessageData<T, K>): MessageBody<T, K> {\n return {\n id: nanoid(),\n type,\n data,\n timestamp: Date.now(),\n };\n }\n}\n","import {browser} from '@browser/browser'\n\nimport {MessageBody, MessageHandler, MessageMap, MessageSender, MessageType} from '@typing/message';\n\nexport default class MessageManager<T extends MessageMap> {\n private handlers: Set<MessageHandler<T>> = new Set()\n private isListenerAttached = false;\n\n private static instance: MessageManager<MessageMap> | null = null;\n\n public static getInstance<T extends MessageMap>(): MessageManager<T> {\n if (MessageManager.instance === null) {\n MessageManager.instance = new MessageManager<T>();\n }\n return MessageManager.instance;\n }\n\n constructor() {\n this.listener = this.listener.bind(this);\n }\n\n public add(handler: MessageHandler<T>) {\n this.handlers.add(handler);\n this.updateListener();\n }\n\n public remove(handler: MessageHandler<T>) {\n this.handlers.delete(handler);\n this.updateListener();\n }\n\n public clear() {\n this.handlers = new Set();\n this.updateListener();\n }\n\n private updateListener() {\n if (this.handlers.size > 0 && !this.isListenerAttached) {\n browser().runtime.onMessage.addListener(this.listener);\n this.isListenerAttached = true;\n } else if (this.handlers.size === 0 && this.isListenerAttached) {\n browser().runtime.onMessage.removeListener(this.listener);\n this.isListenerAttached = false;\n }\n }\n\n private listener<K extends MessageType<T>>(\n message: MessageBody<T, K>,\n sender: MessageSender,\n sendResponse: (response?: any) => void\n ): boolean | void {\n if (!message || typeof message !== 'object' || !message.type) return;\n\n const results: Promise<any>[] = [];\n\n for (const handler of this.handlers) {\n try {\n const result = handler.run(message.type, message.data, sender);\n if (result !== undefined) {\n results.push(Promise.resolve(result));\n }\n } catch (err) {\n console.error('Message handler error:', err);\n }\n }\n\n if (results.length > 1) {\n throw new Error(`Message type \"${message.type}\" has multiple handlers returning a response. Only one response is allowed.`)\n }\n\n if (results.length === 1) {\n results[0].then(sendResponse);\n return true;\n }\n };\n}\n","import {MessageHandler, MessageData, MessageMap, MessageResponse, MessageSender, MessageType} from \"@typing/message\";\n\nexport default abstract class AbstractHandler<T extends MessageMap> implements MessageHandler<T> {\n\n public abstract run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {MessageData, MessageTargetHandler, MessageMap, MessageResponse, MessageSender, MessageType} from \"@typing/message\";\n\nexport default class SingleHandler<T extends MessageMap> extends AbstractHandler<T> {\n\n constructor(private messageType: MessageType<T>, private handler: MessageTargetHandler<T, MessageType<T>>) {\n super();\n }\n\n public run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n if (type === this.messageType) {\n return this.handler(data, sender);\n }\n }\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {\n MessageData,\n MessageMap,\n MessageMapHandler,\n MessageResponse,\n MessageSender,\n MessageType\n} from \"@typing/message\";\n\nexport default class MapHandler<T extends MessageMap> extends AbstractHandler<T> {\n\n constructor(private map: MessageMapHandler<T>) {\n super();\n }\n\n public run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n const handler = this.map[type];\n if (handler) {\n if (typeof handler === \"function\") {\n return handler(data, sender);\n } else {\n throw new Error(`Handler for type \"${type}\" in object of handlers must be a function`)\n }\n }\n }\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {\n MessageData,\n MessageGeneralHandler,\n MessageMap,\n MessageResponse,\n MessageSender,\n MessageType\n} from \"@typing/message\";\n\nexport default class GeneralHandler<T extends MessageMap, K extends MessageType<T>> extends AbstractHandler<T> {\n\n constructor(private generalHandler: MessageGeneralHandler<T, K>) {\n super();\n }\n\n public run(type: K, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n return this.generalHandler(type, data, sender);\n }\n}\n","import {browser} from '@browser/browser'\nimport {throwRuntimeError} from \"@browser/runtime\";\nimport {\n MessageData,\n MessageGeneralHandler,\n MessageHandler,\n MessageMap,\n MessageMapHandler,\n MessageResponse,\n MessageTargetHandler,\n MessageType\n} from '@typing/message';\n\nimport AbstractMessage from './AbstractMessage';\nimport MessageManager from \"../MessageManager\";\n\nimport {GeneralHandler, MapHandler, SingleHandler} from \"../handlers\";\n\nexport type MessageSendOptions = number | { tabId: number; frameId?: number };\n\nexport default class Message<T extends MessageMap> extends AbstractMessage<T, MessageSendOptions> {\n protected manager = MessageManager.getInstance<T>();\n\n public send<K extends MessageType<T>>(type: K, data: MessageData<T, K>, options?: MessageSendOptions): Promise<MessageResponse<T, K>> {\n const message = this.buildMessage(type, data);\n\n if (typeof options === 'number' || typeof options === 'object') {\n const tabId = typeof options === 'number' ? options : options.tabId;\n const frameId = typeof options === 'object' && options.frameId !== undefined ? options.frameId : undefined;\n\n return new Promise((resolve, reject) => {\n browser().tabs.sendMessage(tabId, message, {frameId}, (response) => {\n try {\n throwRuntimeError()\n resolve(response);\n } catch (e) {\n reject(e)\n }\n });\n });\n }\n\n return new Promise((resolve, reject) => {\n browser().runtime.sendMessage(message, (response) => {\n try {\n throwRuntimeError()\n resolve(response);\n } catch (e) {\n reject(e)\n }\n });\n });\n }\n\n public watch<K extends MessageType<T>>(\n arg1: K | MessageMapHandler<T> | MessageGeneralHandler<T, K>,\n arg2?: MessageTargetHandler<T, K>\n ): () => void {\n\n let handler: MessageHandler<T>\n\n if (typeof arg1 === 'function') {\n handler = new GeneralHandler<T, K>(arg1);\n } else if (typeof arg1 === 'object' && arg2 === undefined) {\n handler = new MapHandler<T>(arg1);\n } else if (typeof arg1 === 'string' && arg2) {\n handler = new SingleHandler<T>(arg1, arg2);\n } else {\n throw new Error('Invalid arguments passed to watch()');\n }\n\n this.manager.add(handler);\n\n return () => this.manager.remove(handler);\n }\n}\n","import {isBackground} from \"@browser/runtime\";\nimport {Message, MessageSendOptions} from \"@message/providers\";\n\nimport BaseService from \"./BaseService\";\n\nimport {ServiceDictionary, ServiceName} from \"@typing/service\";\nimport {MessageMap, MessageProvider} from \"@typing/message\";\nimport {DeepAsyncProxy} from \"@typing/helpers\";\n\nexport default class<N extends ServiceName, T = DeepAsyncProxy<ServiceDictionary[N]>> extends BaseService<N, T> {\n private _message?: MessageProvider<MessageMap, MessageSendOptions>;\n\n protected readonly messageKey: string;\n\n constructor(name: N) {\n super(name);\n\n this.messageKey = `service.${this.name}`;\n }\n\n protected get message(): MessageProvider<MessageMap, MessageSendOptions> {\n return this._message ??= new Message();\n }\n\n private createProxy(path?: string): T {\n const wrapped = () => {\n }\n\n const proxy = new Proxy(wrapped, {\n apply: async (_target, _thisArg, args) => {\n return this.message.send(this.messageKey, {path, args});\n },\n\n get: (_target, propertyName, receiver) => {\n if (propertyName === '__proxy' || typeof propertyName !== 'string') {\n return Reflect.get(wrapped, propertyName, receiver);\n }\n\n const newPath = path == null ? propertyName : `${path}.${String(propertyName)}`;\n\n return this.createProxy(newPath);\n },\n });\n\n // @ts-expect-error — Adding a hidden property\n proxy.__proxy = true;\n\n return proxy as unknown as T;\n }\n\n public get(): T {\n if (isBackground()) {\n throw new Error('ProxyService.get() cannot be called in the background');\n }\n\n return this.createProxy();\n }\n}\n","import get from 'get-value';\n\nimport ProxyService from \"./ProxyService\";\n\nimport {isBackground} from \"@browser/runtime\";\n\nimport type {ServiceDictionary, ServiceName} from \"@typing/service\";\n\nexport default class<\n N extends ServiceName,\n T extends object = ServiceDictionary[N],\n A extends any[] = []\n> extends ProxyService<N, T> {\n constructor(name: N, protected readonly init: (...args: A) => T) {\n super(name)\n }\n\n public register(...args: A): T {\n if (this.manager.has(this.name)) {\n throw new Error(`A service with the name \"${this.name}\" already exists. The service name must be unique.`);\n }\n\n const service = this.init(...args);\n\n this.manager.add(this.name, service);\n\n this.message.watch(this.messageKey, async ({path, args}) => {\n try {\n const property = path == null ? service : get(service, path);\n\n if (property === undefined) {\n throw new Error(`Property not found at path \"${path}\" in service \"${this.name}\"`);\n }\n\n if (typeof property === 'function') {\n return await property.apply(service, args);\n }\n\n return property\n\n } catch (error) {\n console.error('RegisterService.register()', error);\n\n throw error;\n }\n });\n\n return service;\n }\n\n public get(): T {\n if (!isBackground()) {\n throw new Error('RegisterService.get() must be called from within the background context.');\n }\n\n return this.manager.get(this.name);\n }\n}\n","import {BaseService, ProxyService, RegisterService} from \"./providers\";\n\nimport {ServiceDictionary, ServiceName, ServiceProxyTarget, ServiceTarget} from \"@typing/service\";\n\nexport {\n type ServiceDictionary,\n type ServiceName,\n type ServiceTarget,\n type ServiceProxyTarget,\n RegisterService,\n ProxyService\n};\n\nexport const getService = <N extends ServiceName>(name: N): ServiceDictionary[N] => {\n return new BaseService(name).get();\n}\n"]}
1
+ {"version":3,"sources":["../../src/browser/browser.ts","../../src/browser/utils.ts","../../src/browser/runtime.ts","../../src/types/service.ts","../../src/service/ServiceManager.ts","../../src/service/providers/BaseService.ts","../../src/browser/tab.ts","../../src/message/providers/AbstractMessage.ts","../../src/types/message.ts","../../src/message/MessageManager.ts","../../src/message/handlers/AbstractHandler.ts","../../src/message/handlers/SingleHandler.ts","../../src/message/handlers/MapHandler.ts","../../src/message/handlers/GeneralHandler.ts","../../src/message/providers/Message.ts","../../src/service/providers/ProxyService.ts","../../src/service/providers/RegisterService.ts","../../src/service/index.ts"],"names":["nanoid","args","get"],"mappings":";;;;;;;;;;AAAO,IAAM,UAAU,MAAqB;AACxC,EAAO,OAAA,MAAA;AACX,CAAA;;;ACAO,SAAS,aAAgD,QAAgB,EAAA;AAC5E,EAAA,OAAQ,IAAI,IAA8C,KAAA;AACtD,IAAI,IAAA;AACA,MAAM,MAAA,MAAA,GAAS,QAAS,CAAA,GAAG,IAAI,CAAA;AAE/B,MAAA,IAAI,kBAAkB,OAAS,EAAA;AAC3B,QAAA,MAAA,CAAO,MAAM,CAAO,GAAA,KAAA;AAChB,UAAQ,OAAA,CAAA,KAAA,CAAM,yBAAyB,GAAG,CAAA;AAAA,SAC7C,CAAA;AAAA;AAGL,MAAO,OAAA,MAAA;AAAA,aACF,GAAK,EAAA;AACV,MAAQ,OAAA,CAAA,KAAA,CAAM,wBAAwB,GAAG,CAAA;AAAA;AAC7C,GACJ;AACJ;AAEO,SAAS,cAAA,CACZ,QACA,QACU,EAAA;AACV,EAAM,MAAA,QAAA,GAAW,aAAa,QAAQ,CAAA;AAEtC,EAAA,MAAA,CAAO,YAAY,QAAQ,CAAA;AAE3B,EAAO,OAAA,MAAM,MAAO,CAAA,cAAA,CAAe,QAAQ,CAAA;AAC/C;;;AClBA,IAAM,OAAA,GAAU,MAAM,OAAA,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,CAAK,IAAI,CAAA;AAAA;AAEzC;;;AC5Ca,IAAA,UAAA,GAAa,CAAwB,IAAkC,KAAA;AAChF,EAAA,OAAO,IAAI,mBAAA,CAAY,IAAI,CAAA,CAAE,GAAI,EAAA;AACrC","file":"index.cjs","sourcesContent":["export const browser = (): typeof chrome => {\n return chrome;\n}","type Event<T extends Function> = chrome.events.Event<T>\n\nexport function safeListener<T extends (...args: any[]) => any>(listener: T): T {\n return ((...args: Parameters<T>): ReturnType<T> | void => {\n try {\n const result = listener(...args);\n\n if (result instanceof Promise) {\n result.catch(err => {\n console.error('Listener async error:', err);\n });\n }\n\n return result;\n } catch (err) {\n console.error('Listener sync error:', err);\n }\n }) as T;\n}\n\nexport function handleListener<T extends (...args: any[]) => void>(\n target: Event<T>,\n callback: T,\n): () => void {\n const listener = safeListener(callback);\n\n target.addListener(listener);\n\n return () => target.removeListener(listener);\n}","import {browser} from \"./browser\";\nimport {handleListener} from \"./utils\";\n\nimport {ManifestVersion} from \"@typing/manifest\";\nimport {MessageBody, MessageDictionary, MessageResponse, MessageType} from \"@typing/message\";\n\ntype Manifest = chrome.runtime.Manifest;\ntype PlatformInfo = chrome.runtime.PlatformInfo;\ntype ContextFilter = chrome.runtime.ContextFilter;\ntype ExtensionContext = chrome.runtime.ExtensionContext;\n\nconst runtime = () => browser().runtime;\n\nconst backgroundPaths = [\n '/_generated_background_page.view',\n];\n\nexport const getId = (): string => runtime().id;\n\nexport const getUrl = (path: string) => runtime().getURL(path);\n\nexport const getManifest = (): Manifest => runtime().getManifest();\n\nexport const getManifestVersion = (): ManifestVersion => getManifest().manifest_version;\n\nexport const getRuntimeContexts = (filter: ContextFilter) => new Promise<ExtensionContext[]>((resolve, reject) => {\n runtime().getContexts(filter, contexts => {\n try {\n throwRuntimeError();\n\n resolve(contexts);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getPlatformInfo = (): Promise<PlatformInfo> => new Promise<PlatformInfo>((resolve, reject) => {\n runtime().getPlatformInfo((platformInfo) => {\n try {\n throwRuntimeError();\n\n resolve(platformInfo);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const isManifestVersion3 = (): boolean => getManifestVersion() === 3;\n\nexport const isBackground = (): boolean => {\n if (!getId()) {\n return false;\n }\n\n const manifest = getManifest();\n\n if (!manifest.background) {\n return false;\n }\n\n if (manifest.manifest_version === 3) {\n return typeof window === \"undefined\";\n }\n\n return window !== undefined && backgroundPaths.includes(location.pathname);\n}\n\nexport const throwRuntimeError = (): void => {\n const error = runtime().lastError;\n\n if (error) {\n throw new Error(error.message);\n }\n}\n\nexport const sendMessage = <\n T extends MessageDictionary,\n K extends MessageType<T>\n>(\n message: MessageBody<T, K>\n): Promise<MessageResponse<T, K>> => new Promise<MessageResponse<T, K>>((resolve, reject) => {\n runtime().sendMessage(message, (response) => {\n try {\n throwRuntimeError();\n\n resolve(response);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const onRuntimeInstalled = (callback: Parameters<typeof chrome.runtime.onInstalled.addListener>[0]): () => void => {\n return handleListener(runtime().onInstalled, callback)\n}\n\nexport const onMessage = (callback: Parameters<typeof chrome.runtime.onMessage.addListener>[0]): () => void => {\n return handleListener(runtime().onMessage, callback)\n}\n","import {Required} from \"utility-types\";\n\nimport {EntrypointBuilder, EntrypointOptions} from \"@typing/entrypoint\";\nimport {BackgroundConfig} from \"@typing/background\";\nimport {Awaiter, DeepAsyncProxy} from \"@typing/helpers\";\n\nexport const ServiceGlobalKey = 'adnbnService';\n\nexport type ServiceType = ((...args: any[]) => Promise<any>) | { [key: string]: any | ServiceType };\n\nexport interface ServiceDictionary {\n [key: string]: any;\n}\n\nexport type ServiceName = Extract<keyof ServiceDictionary, string>;\n\nexport type ServiceTarget<T extends ServiceDictionary, K extends keyof T> = T[K];\n\nexport type ServiceProxyTarget<T extends ServiceDictionary, K extends keyof T> = DeepAsyncProxy<T[K]>;\n\nexport interface ServiceManager {\n add<K extends ServiceName>(\n name: K,\n instance: ServiceDictionary[K]\n ): this;\n\n get<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined;\n\n has(name: ServiceName): boolean;\n\n remove<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined;\n\n clear(): this;\n}\n\nexport interface ServiceConfig extends BackgroundConfig {\n name: string;\n}\n\nexport type ServiceOptions = ServiceConfig & EntrypointOptions;\n\nexport type ServiceEntrypointOptions = Partial<ServiceOptions>;\n\nexport type ServiceInitGetter<T extends ServiceType> = (options: ServiceOptions) => T;\n\nexport type ServiceMainHandler<T extends ServiceType> = (service: T, options: ServiceOptions) => Awaiter<void>;\n\nexport type ServiceDefinition<T extends ServiceType> = ServiceEntrypointOptions & {\n init?: ServiceInitGetter<T>;\n main?: ServiceMainHandler<T>;\n};\n\nexport type ServiceUnresolvedDefinition<T extends ServiceType> = Partial<ServiceDefinition<T>>;\n\nexport type ServiceResolvedDefinition<T extends ServiceType> = Required<ServiceDefinition<T>, 'name' | 'init'>;\n\nexport type ServiceBuilder = EntrypointBuilder;\n","import {\n ServiceDictionary,\n ServiceGlobalKey,\n ServiceManager as ServiceManagerContract,\n ServiceName\n} from \"@typing/service\";\n\nexport default class ServiceManager implements ServiceManagerContract {\n private services = new Map<\n ServiceName,\n ServiceDictionary[ServiceName]\n >();\n\n public static getInstance(): ServiceManagerContract {\n return globalThis[ServiceGlobalKey] ??= new ServiceManager();\n }\n\n public add<K extends ServiceName>(\n name: K,\n instance: ServiceDictionary[K]\n ): this {\n this.services.set(name, instance);\n\n return this;\n }\n\n public get<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined {\n return this.services.get(name) as ServiceDictionary[K] | undefined;\n }\n\n public has(name: ServiceName): boolean {\n return this.services.has(name);\n }\n\n public remove<K extends ServiceName>(\n name: K\n ): ServiceDictionary[K] | undefined {\n const service = this.get(name);\n\n this.services.delete(name);\n\n return service;\n }\n\n public clear(): this {\n this.services.clear();\n\n return this;\n }\n}","import {isBackground} from \"@browser/runtime\";\n\nimport ServiceManager from \"../ServiceManager\";\n\nimport {ServiceDictionary, ServiceManager as ServiceManagerContract, ServiceName} from \"@typing/service\";\n\nexport default class<N extends ServiceName, T = ServiceDictionary[N]> {\n constructor(protected readonly name: N) {\n }\n\n protected get manager(): ServiceManagerContract {\n return ServiceManager.getInstance();\n }\n\n public get(): T {\n if (!isBackground()) {\n throw new Error(`Service \"${this.name}\" can be getting only from background context.`);\n }\n\n return this.manager.get(this.name);\n }\n\n public destroy(): void {\n this.manager.remove(this.name);\n }\n}\n","import {browser} from \"./browser\";\nimport {handleListener} from \"./utils\";\nimport {throwRuntimeError} from \"./runtime\";\nimport {MessageBody, MessageDictionary, MessageResponse, MessageType} from \"@typing/message\";\n\ntype Tab = chrome.tabs.Tab;\n\ntype Window = chrome.windows.Window;\n\ntype ZoomSettings = chrome.tabs.ZoomSettings;\ntype InjectDetails = chrome.tabs.InjectDetails;\n\ntype QueryInfo = chrome.tabs.QueryInfo;\ntype HighlightInfo = chrome.tabs.HighlightInfo;\n\ntype GroupOptions = chrome.tabs.GroupOptions;\ntype CaptureVisibleTabOptions = chrome.tabs.CaptureVisibleTabOptions;\n\ntype MoveProperties = chrome.tabs.MoveProperties;\ntype UpdateProperties = chrome.tabs.UpdateProperties;\ntype CreateProperties = chrome.tabs.CreateProperties;\n\ntype MessageSendOptions = chrome.tabs.MessageSendOptions;\n\nconst tabs = () => browser().tabs;\n\n// =================================\n// ============ METHODS ============\n// =================================\nexport const captureVisibleTab = (windowId: number, options: CaptureVisibleTabOptions) => new Promise<string>((resolve, reject) => {\n tabs().captureVisibleTab(windowId, options, (dataUrl) => {\n try {\n throwRuntimeError();\n\n resolve(dataUrl);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const createTab = (properties: CreateProperties): Promise<Tab> => new Promise<Tab>((resolve, reject) => {\n tabs().create(properties, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const detectTabLanguage = (tabId: number) => new Promise<string>((resolve, reject) => {\n // For an unknown/undefined language, und is returned.\n tabs().detectLanguage(tabId, (language) => {\n try {\n throwRuntimeError();\n\n resolve(language);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const discardTab = (tabId: number) => new Promise<Tab>((resolve, reject) => {\n tabs().discard(tabId, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const duplicateTab = (tabId: number) => new Promise<Tab | undefined>((resolve, reject) => {\n tabs().duplicate(tabId, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getTab = (tabId: number) => new Promise<Tab>((resolve, reject) => {\n tabs().get(tabId, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getCurrentTab = () => new Promise<Tab | undefined>((resolve, reject) => {\n // Returns undefined if called from a non-tab context (for example, a background view or popup view)\n tabs().getCurrent((tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getTabZoom = (tabId: number) => new Promise<number>((resolve, reject) => {\n tabs().getZoom(tabId, (zoomFactor) => {\n try {\n throwRuntimeError();\n\n resolve(zoomFactor);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const getTabZoomSettings = (tabId: number) => new Promise<ZoomSettings>((resolve, reject) => {\n tabs().getZoomSettings(tabId, (zoomSettings) => {\n try {\n throwRuntimeError();\n\n resolve(zoomSettings);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const goTabBack = (tabId: number) => new Promise<void>((resolve, reject) => {\n tabs().goBack(tabId, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const goTabForward = (tabId: number) => new Promise<void>((resolve, reject) => {\n tabs().goForward(tabId, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const groupTabs = (options: GroupOptions) => new Promise<number>((resolve, reject) => {\n tabs().group(options, (tabs) => {\n try {\n throwRuntimeError();\n\n resolve(tabs);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const highlightTab = (highlightInfo: HighlightInfo) => new Promise<Window>((resolve, reject) => {\n tabs().highlight(highlightInfo, (window) => {\n try {\n throwRuntimeError();\n\n resolve(window);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const moveTab = (tabId: number, moveProperties: MoveProperties) => new Promise<Tab>((resolve, reject) => {\n tabs().move(tabId, moveProperties, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const moveTabs = (tabIds: number[], moveProperties: MoveProperties) => new Promise<Tab[]>((resolve, reject) => {\n tabs().move(tabIds, moveProperties, (tabs) => {\n try {\n throwRuntimeError();\n\n resolve(tabs);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const queryTabs = (queryInfo?: QueryInfo) => new Promise<Tab[]>((resolve, reject) => {\n tabs().query(queryInfo || {}, (tabs) => {\n try {\n throwRuntimeError();\n\n resolve(tabs);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const reloadTab = (tabId: number, bypassCache?: boolean | undefined) => new Promise<void>((resolve, reject) => {\n tabs().reload(tabId, {bypassCache}, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const removeTab = (tabId: number): Promise<void> => new Promise<void>((resolve, reject) => {\n tabs().remove(tabId, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const setTabZoom = (tabId: number, zoomFactor: number) => new Promise<void>((resolve, reject) => {\n tabs().setZoom(tabId, zoomFactor, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const setTabZoomSettings = (tabId: number, zoomSettings: ZoomSettings) => new Promise<void>((resolve, reject) => {\n tabs().setZoomSettings(tabId, zoomSettings, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const ungroupTab = (tabIds: number | number[]) => new Promise<void>((resolve, reject) => {\n tabs().ungroup(tabIds, () => {\n try {\n throwRuntimeError();\n\n resolve();\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const updateTab = (tabId: number, updateProperties: UpdateProperties) => new Promise<Tab | undefined>((resolve, reject) => {\n tabs().update(tabId, updateProperties, (tab) => {\n try {\n throwRuntimeError();\n\n resolve(tab);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const executeScriptTab = (tabId: number, details: InjectDetails) => new Promise<any[]>((resolve, reject) => {\n tabs().executeScript(tabId, details, (result) => {\n try {\n throwRuntimeError();\n\n resolve(result);\n } catch (e) {\n reject(e);\n }\n });\n});\n\nexport const sendTabMessage = <\n T extends MessageDictionary,\n K extends MessageType<T>\n>(\n tabId: number,\n message: MessageBody<T, K>,\n options: MessageSendOptions = {}\n): Promise<MessageResponse<T, K>> => new Promise<MessageResponse<T, K>>((resolve, reject) => {\n tabs().sendMessage(tabId, message, options, (response) => {\n try {\n throwRuntimeError();\n\n resolve(response);\n } catch (e) {\n reject(e);\n }\n });\n});\n\n// =================================\n// ======== CUSTOM METHODS =========\n// =================================\nexport const getTabUrl = async (tabId: number): Promise<string> => {\n const tab = await findTabById(tabId);\n\n if (!tab) {\n throw new Error(`Tab id \"${tabId}\" not exist`);\n }\n\n const {url} = tab;\n\n if (!url) {\n throw new Error(`URL not exist by tab id ${tabId}`);\n }\n\n return url;\n}\n\nexport const getActiveTab = async (): Promise<Tab> => {\n const tabs = await queryTabs({active: true, currentWindow: true})\n\n if (!tabs || !tabs[0]) {\n throw new Error('Tab not found');\n }\n\n return tabs[0];\n}\n\nexport const queryTabIds = async (queryInfo?: QueryInfo): Promise<number[]> => (await queryTabs(queryInfo)).reduce((ids, {id}) => {\n if (typeof id === 'number') {\n return [...ids, id];\n }\n\n return ids;\n}, [] as number[]);\n\nexport const findTab = (queryInfo?: QueryInfo): Promise<Tab | undefined> => queryTabs(queryInfo).then(tabs => tabs.length ? tabs[0] : undefined);\n\nexport const findTabById = (tabId: number): Promise<Tab | undefined> => getTab(tabId).then(tab => tab).catch(() => undefined);\n\nexport const updateTabAsSelected = (tabId: number) => updateTab(tabId, {highlighted: true});\n\nexport const updateTabAsActive = (tabId: number) => updateTab(tabId, {active: true});\n\nexport const openOrCreateTab = async (tab: Tab): Promise<void> => {\n const {id, url} = tab;\n\n if (id && url) {\n const existTab = await findTab({url});\n\n if (existTab && id && existTab.id === id) {\n await updateTabAsSelected(id);\n\n return;\n }\n }\n\n await createTab({url});\n}\n\n// =================================\n// ============ EVENTS =============\n// =================================\nexport const onTabActivated = (callback: Parameters<typeof chrome.tabs.onActivated.addListener>[0]): () => void => {\n return handleListener(tabs().onActivated, callback)\n}\n\nexport const onTabAttached = (callback: Parameters<typeof chrome.tabs.onAttached.addListener>[0]): () => void => {\n return handleListener(tabs().onAttached, callback)\n}\n\nexport const onTabCreated = (callback: Parameters<typeof chrome.tabs.onCreated.addListener>[0]): () => void => {\n return handleListener(tabs().onCreated, callback)\n}\n\nexport const onTabDetached = (callback: Parameters<typeof chrome.tabs.onDetached.addListener>[0]): () => void => {\n return handleListener(tabs().onDetached, callback)\n}\n\nexport const onTabHighlighted = (callback: Parameters<typeof chrome.tabs.onHighlighted.addListener>[0]): () => void => {\n return handleListener(tabs().onHighlighted, callback)\n};\n\nexport const onTabMoved = (callback: Parameters<typeof chrome.tabs.onMoved.addListener>[0]): () => void => {\n return handleListener(tabs().onMoved, callback)\n}\n\nexport const onTabRemoved = (callback: Parameters<typeof chrome.tabs.onRemoved.addListener>[0]): () => void => {\n return handleListener(tabs().onRemoved, callback)\n};\n\nexport const onTabReplaced = (callback: Parameters<typeof chrome.tabs.onReplaced.addListener>[0]): () => void => {\n return handleListener(tabs().onReplaced, callback)\n};\n\nexport const onTabUpdated = (callback: Parameters<typeof chrome.tabs.onUpdated.addListener>[0]): () => void => {\n return handleListener(tabs().onUpdated, callback)\n}\n\nexport const onTabZoomChange = (callback: Parameters<typeof chrome.tabs.onZoomChange.addListener>[0]): () => void => {\n return handleListener(tabs().onZoomChange, callback)\n}\n","import {nanoid} from 'nanoid';\nimport {\n MessageBody,\n MessageData,\n MessageDictionary,\n MessageGeneralHandler,\n MessageMapHandler,\n MessageProvider,\n MessageResponse,\n MessageTargetHandler,\n MessageType\n} from '@typing/message'\n\nexport default abstract class AbstractMessage<T extends MessageDictionary, TOptions> implements MessageProvider<T, TOptions> {\n public abstract send<K extends MessageType<T>>(type: K, data: MessageData<T, K>, options?: TOptions): Promise<MessageResponse<T, K>>;\n\n public abstract watch<K extends MessageType<T>>(type: K, handler: MessageTargetHandler<T, K>): () => void;\n\n public abstract watch(map: MessageMapHandler<T>): () => void;\n\n public abstract watch<K extends MessageType<T>>(general: MessageGeneralHandler<T, K>): () => void;\n\n protected buildMessage<K extends MessageType<T>>(type: K, data: MessageData<T, K>): MessageBody<T, K> {\n return {\n id: nanoid(),\n type,\n data,\n timestamp: Date.now(),\n };\n }\n}\n","export const MessageGlobalKey = 'adnbnMessage';\n\nexport type MessageSender = chrome.runtime.MessageSender;\n\nexport type MessageDictionary = Record<string, (data: any) => any>;\n\nexport type MessageType<T extends MessageDictionary> = Extract<keyof T, string>;\nexport type MessageData<T extends MessageDictionary, K extends MessageType<T>> = Parameters<T[K]>[0];\nexport type MessageResponse<T extends MessageDictionary, K extends MessageType<T>> = ReturnType<T[K]>;\n\nexport type MessageTargetHandler<T extends MessageDictionary, K extends MessageType<T>> = (data: MessageData<T, K>, sender: MessageSender) => MessageResponse<T, K>\nexport type MessageMapHandler<T extends MessageDictionary> = { [K in MessageType<T>]?: MessageTargetHandler<T, K> }\nexport type MessageGeneralHandler<T extends MessageDictionary, K extends MessageType<T>> = (type: K, data: MessageData<T, K>, sender: MessageSender) => any\n\nexport interface MessageBody<T extends MessageDictionary, K extends MessageType<T>> {\n id: string;\n type: K;\n data: MessageData<T, K>;\n timestamp: number;\n}\n\nexport interface MessageProvider<T extends MessageDictionary, TOptions = void> {\n send<K extends MessageType<T>>(type: K, data: MessageData<T, K>, options?: TOptions): Promise<MessageResponse<T, K>>;\n\n watch<K extends MessageType<T>>(type: K, handler: MessageTargetHandler<T, K>): () => void;\n\n watch(map: MessageMapHandler<T>): () => void;\n\n watch<K extends MessageType<T>>(general: MessageGeneralHandler<T, K>): () => void;\n}\n\nexport interface MessageHandler<T extends MessageDictionary> {\n run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined;\n}\n","import {onMessage} from '@browser/runtime'\n\nimport {\n MessageBody,\n MessageDictionary,\n MessageGlobalKey,\n MessageHandler,\n MessageSender,\n MessageType\n} from '@typing/message';\n\nexport default class MessageManager<T extends MessageDictionary> {\n private handlers: Set<MessageHandler<T>> = new Set()\n private unsubscribe: (() => void) | null = null;\n\n public static getInstance<T extends MessageDictionary>(): MessageManager<T> {\n return globalThis[MessageGlobalKey] ??= new MessageManager<T>();\n }\n\n constructor() {\n this.listener = this.listener.bind(this);\n }\n\n public add(handler: MessageHandler<T>) {\n this.handlers.add(handler);\n this.updateListener();\n }\n\n public remove(handler: MessageHandler<T>) {\n this.handlers.delete(handler);\n this.updateListener();\n }\n\n public clear() {\n this.handlers = new Set();\n this.updateListener();\n }\n\n private updateListener() {\n if (this.handlers.size > 0 && !this.unsubscribe) {\n this.unsubscribe = onMessage(this.listener);\n } else if (this.handlers.size === 0 && this.unsubscribe) {\n this.unsubscribe();\n this.unsubscribe = null;\n }\n }\n\n private listener<K extends MessageType<T>>(\n message: MessageBody<T, K>,\n sender: MessageSender,\n sendResponse: (response?: any) => void\n ): boolean | void {\n if (!message || typeof message !== 'object' || !message.type) return;\n\n const results: Promise<any>[] = [];\n\n for (const handler of this.handlers) {\n try {\n const result = handler.run(message.type, message.data, sender);\n if (result !== undefined) {\n results.push(Promise.resolve(result));\n }\n } catch (err) {\n console.error('Message handler error:', err);\n }\n }\n\n if (results.length > 1) {\n throw new Error(`Message type \"${message.type}\" has multiple handlers returning a response. Only one response is allowed.`)\n }\n\n if (results.length === 1) {\n results[0].then(sendResponse);\n return true;\n }\n };\n}\n","import {\n MessageData,\n MessageDictionary,\n MessageHandler,\n MessageResponse,\n MessageSender,\n MessageType\n} from \"@typing/message\";\n\nexport default abstract class AbstractHandler<T extends MessageDictionary> implements MessageHandler<T> {\n\n public abstract run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {\n MessageData,\n MessageDictionary,\n MessageResponse,\n MessageSender,\n MessageTargetHandler,\n MessageType\n} from \"@typing/message\";\n\nexport default class SingleHandler<T extends MessageDictionary> extends AbstractHandler<T> {\n\n constructor(private messageType: MessageType<T>, private handler: MessageTargetHandler<T, MessageType<T>>) {\n super();\n }\n\n public run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n if (type === this.messageType) {\n return this.handler(data, sender);\n }\n }\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {\n MessageData,\n MessageDictionary,\n MessageMapHandler,\n MessageResponse,\n MessageSender,\n MessageType\n} from \"@typing/message\";\n\nexport default class MapHandler<T extends MessageDictionary> extends AbstractHandler<T> {\n\n constructor(private map: MessageMapHandler<T>) {\n super();\n }\n\n public run(type: MessageType<T>, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n const handler = this.map[type];\n if (handler) {\n if (typeof handler === \"function\") {\n return handler(data, sender);\n } else {\n throw new Error(`Handler for type \"${type}\" in object of handlers must be a function`)\n }\n }\n }\n}\n","import AbstractHandler from \"./AbstractHandler\";\nimport {\n MessageData,\n MessageDictionary,\n MessageGeneralHandler,\n MessageResponse,\n MessageSender,\n MessageType\n} from \"@typing/message\";\n\nexport default class GeneralHandler<T extends MessageDictionary, K extends MessageType<T>> extends AbstractHandler<T> {\n\n constructor(private generalHandler: MessageGeneralHandler<T, K>) {\n super();\n }\n\n public run(type: K, data: MessageData<T, MessageType<T>>, sender: MessageSender): MessageResponse<T, MessageType<T>> | undefined {\n return this.generalHandler(type, data, sender);\n }\n}\n","import {sendMessage} from \"@browser/runtime\";\nimport {sendTabMessage} from \"@browser/tab\";\nimport {\n MessageData,\n MessageDictionary,\n MessageGeneralHandler,\n MessageHandler,\n MessageMapHandler,\n MessageResponse,\n MessageTargetHandler,\n MessageType\n} from '@typing/message';\n\nimport AbstractMessage from './AbstractMessage';\nimport MessageManager from \"../MessageManager\";\n\nimport {GeneralHandler, MapHandler, SingleHandler} from \"../handlers\";\n\nexport type MessageSendOptions = number | { tabId: number; frameId?: number; documentId?: string };\n\nexport default class Message<T extends MessageDictionary> extends AbstractMessage<T, MessageSendOptions> {\n private static instance: Message<MessageDictionary> | null = null;\n\n public static getInstance<T extends MessageDictionary>() {\n return this.instance ??= new Message<T>();\n }\n\n protected get manager(): MessageManager<T> {\n return MessageManager.getInstance<T>();\n }\n\n public send<K extends MessageType<T>>(type: K, data: MessageData<T, K>, options?: MessageSendOptions): Promise<MessageResponse<T, K>> {\n const message = this.buildMessage(type, data);\n\n if (options) {\n if (typeof options === 'number') {\n return sendTabMessage(options, message)\n }\n\n const {tabId, ...other} = options;\n\n return sendTabMessage(tabId, message, other);\n\n }\n\n return sendMessage(message)\n }\n\n public watch<K extends MessageType<T>>(\n arg1: K | MessageMapHandler<T> | MessageGeneralHandler<T, K>,\n arg2?: MessageTargetHandler<T, K>\n ): () => void {\n\n let handler: MessageHandler<T>\n\n if (typeof arg1 === 'function') {\n handler = new GeneralHandler<T, K>(arg1);\n } else if (typeof arg1 === 'object' && arg2 === undefined) {\n handler = new MapHandler<T>(arg1);\n } else if (typeof arg1 === 'string' && arg2) {\n handler = new SingleHandler<T>(arg1, arg2);\n } else {\n throw new Error('Invalid arguments passed to watch()');\n }\n\n this.manager.add(handler);\n\n return () => this.manager.remove(handler);\n }\n}\n","import {isBackground} from \"@browser/runtime\";\nimport {Message, MessageSendOptions} from \"@message/providers\";\n\nimport BaseService from \"./BaseService\";\n\nimport {ServiceDictionary, ServiceName} from \"@typing/service\";\nimport {MessageDictionary, MessageProvider} from \"@typing/message\";\nimport {DeepAsyncProxy} from \"@typing/helpers\";\n\nexport default class<N extends ServiceName, T = DeepAsyncProxy<ServiceDictionary[N]>> extends BaseService<N, T> {\n private _message?: MessageProvider<MessageDictionary, MessageSendOptions>;\n\n protected readonly messageKey: string;\n\n constructor(name: N) {\n super(name);\n\n this.messageKey = `service.${this.name}`;\n }\n\n protected get message(): MessageProvider<MessageDictionary, MessageSendOptions> {\n return this._message ??= new Message();\n }\n\n private createProxy(path?: string): T {\n const wrapped = () => {\n }\n\n const proxy = new Proxy(wrapped, {\n apply: async (_target, _thisArg, args) => {\n return this.message.send(this.messageKey, {path, args});\n },\n\n get: (_target, propertyName, receiver) => {\n if (propertyName === '__proxy' || typeof propertyName !== 'string') {\n return Reflect.get(wrapped, propertyName, receiver);\n }\n\n const newPath = path == null ? propertyName : `${path}.${String(propertyName)}`;\n\n return this.createProxy(newPath);\n },\n });\n\n // @ts-expect-error — Adding a hidden property\n proxy.__proxy = true;\n\n return proxy as unknown as T;\n }\n\n public get(): T {\n if (isBackground()) {\n throw new Error(`You are trying to get proxy service \"${this.name}\" from background. You can get original service instead`);\n }\n\n return this.createProxy();\n }\n}\n","import get from 'get-value';\n\nimport ProxyService from \"./ProxyService\";\n\nimport {isBackground} from \"@browser/runtime\";\n\nimport type {ServiceDictionary, ServiceName} from \"@typing/service\";\n\nexport default class<\n N extends ServiceName,\n T extends object = ServiceDictionary[N],\n A extends any[] = []\n> extends ProxyService<N, T> {\n constructor(name: N, protected readonly init: (...args: A) => T) {\n super(name)\n }\n\n public register(...args: A): T {\n if (this.manager.has(this.name)) {\n throw new Error(`A service with the name \"${this.name}\" already exists. The service name must be unique.`);\n }\n\n const service = this.init(...args);\n\n this.manager.add(this.name, service);\n\n this.message.watch(this.messageKey, async ({path, args}) => {\n try {\n const property = path == null ? service : get(service, path);\n\n if (property === undefined) {\n throw new Error(`Property not found at path \"${path}\" in service \"${this.name}\"`);\n }\n\n if (typeof property === 'function') {\n return await property.apply(service, args);\n }\n\n return property\n\n } catch (error) {\n console.error('RegisterService.register()', error);\n\n throw error;\n }\n });\n\n return service;\n }\n\n public get(): T {\n if (!isBackground()) {\n throw new Error(`Service \"${this.name}\" can be getting only from background context.`);\n }\n\n return this.manager.get(this.name);\n }\n}\n","import {BaseService, ProxyService, RegisterService} from \"./providers\";\n\nimport {ServiceDictionary, ServiceName, ServiceProxyTarget, ServiceTarget} from \"@typing/service\";\n\nexport {\n type ServiceDictionary,\n type ServiceName,\n type ServiceTarget,\n type ServiceProxyTarget,\n RegisterService,\n ProxyService\n};\n\nexport const getService = <N extends ServiceName>(name: N): ServiceDictionary[N] => {\n return new BaseService(name).get();\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { c as ServiceName, d as ServiceDictionary, e as ServiceManager } from '../service-DBsQltUm.mjs';
2
2
  export { g as ServiceProxyTarget, f as ServiceTarget } from '../service-DBsQltUm.mjs';
3
3
  import { MessageSendOptions } from '../message/index.mjs';
4
- import { c as MessageProvider, a as MessageMap } from '../message-DO3GF4T9.mjs';
4
+ import { c as MessageProvider, a as MessageDictionary } from '../message-CXzpmlXD.mjs';
5
5
  import { D as DeepAsyncProxy } from '../helpers-CS2jFMJ_.mjs';
6
6
  import '../entrypoint-Bozi3iE2.mjs';
7
7
  import '../background-Bb-Jb4ux.mjs';
@@ -18,7 +18,7 @@ declare class export_default$1<N extends ServiceName, T = DeepAsyncProxy<Service
18
18
  private _message?;
19
19
  protected readonly messageKey: string;
20
20
  constructor(name: N);
21
- protected get message(): MessageProvider<MessageMap, MessageSendOptions>;
21
+ protected get message(): MessageProvider<MessageDictionary, MessageSendOptions>;
22
22
  private createProxy;
23
23
  get(): T;
24
24
  }
@@ -1,7 +1,7 @@
1
1
  import { c as ServiceName, d as ServiceDictionary, e as ServiceManager } from '../service-DQ_4M0d4.js';
2
2
  export { g as ServiceProxyTarget, f as ServiceTarget } from '../service-DQ_4M0d4.js';
3
3
  import { MessageSendOptions } from '../message/index.js';
4
- import { c as MessageProvider, a as MessageMap } from '../message-DO3GF4T9.js';
4
+ import { c as MessageProvider, a as MessageDictionary } from '../message-CXzpmlXD.js';
5
5
  import { D as DeepAsyncProxy } from '../helpers-CS2jFMJ_.js';
6
6
  import '../entrypoint-Bozi3iE2.js';
7
7
  import '../background-BX-FOksS.js';
@@ -18,7 +18,7 @@ declare class export_default$1<N extends ServiceName, T = DeepAsyncProxy<Service
18
18
  private _message?;
19
19
  protected readonly messageKey: string;
20
20
  constructor(name: N);
21
- protected get message(): MessageProvider<MessageMap, MessageSendOptions>;
21
+ protected get message(): MessageProvider<MessageDictionary, MessageSendOptions>;
22
22
  private createProxy;
23
23
  get(): T;
24
24
  }