|
| 1 | +/** |
| 2 | + * All access to browser specific APIs should be limited to this file. |
| 3 | + * Care should be taken to ensure that any given method will work in the service worker API. So if |
| 4 | + * something isn't available in the service worker API attempt to provide reasonable defaults. |
| 5 | + */ |
| 6 | + |
| 7 | +export function isDocument() { |
| 8 | + return typeof document !== undefined; |
| 9 | +} |
| 10 | + |
| 11 | +export function isWindow() { |
| 12 | + return typeof window !== undefined; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Register an event handler on the document. If there is no document, such as when running in |
| 17 | + * a service worker, then no operation is performed. |
| 18 | + * |
| 19 | + * @param type The event type to register a handler for. |
| 20 | + * @param listener The handler to register. |
| 21 | + * @param options Event registration options. |
| 22 | + * @returns a function which unregisters the handler. |
| 23 | + */ |
| 24 | +export function addDocumentEventListener( |
| 25 | + type: string, |
| 26 | + listener: (this: Document, ev: Event) => any, |
| 27 | + options?: boolean | AddEventListenerOptions, |
| 28 | +): () => void { |
| 29 | + if (isDocument()) { |
| 30 | + document.addEventListener(type, listener, options); |
| 31 | + return () => { |
| 32 | + document.removeEventListener(type, listener, options); |
| 33 | + }; |
| 34 | + } |
| 35 | + // No document, so no need to unregister anything. |
| 36 | + return () => {}; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Register an event handler on the window. If there is no window, such as when running in |
| 41 | + * a service worker, then no operation is performed. |
| 42 | + * |
| 43 | + * @param type The event type to register a handler for. |
| 44 | + * @param listener The handler to register. |
| 45 | + * @param options Event registration options. |
| 46 | + * @returns a function which unregisters the handler. |
| 47 | + */ |
| 48 | +export function addWindowEventListener( |
| 49 | + type: string, |
| 50 | + listener: (this: Document, ev: Event) => any, |
| 51 | + options?: boolean | AddEventListenerOptions, |
| 52 | +): () => void { |
| 53 | + if (isDocument()) { |
| 54 | + window.addEventListener(type, listener, options); |
| 55 | + return () => { |
| 56 | + window.removeEventListener(type, listener, options); |
| 57 | + }; |
| 58 | + } |
| 59 | + // No document, so no need to unregister anything. |
| 60 | + return () => {}; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * For non-window code this will always be an empty string. |
| 65 | + */ |
| 66 | +export function getHref(): string { |
| 67 | + if (isWindow()) { |
| 68 | + return window.location.href; |
| 69 | + } |
| 70 | + return ''; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * For non-window code this will always be an empty string. |
| 75 | + */ |
| 76 | +export function getLocationSearch(): string { |
| 77 | + if (isWindow()) { |
| 78 | + return window.location.search; |
| 79 | + } |
| 80 | + return ''; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * For non-window code this will always be an empty string. |
| 85 | + */ |
| 86 | +export function getLocationHash(): string { |
| 87 | + if (isWindow()) { |
| 88 | + return window.location.hash; |
| 89 | + } |
| 90 | + return ''; |
| 91 | +} |
| 92 | + |
| 93 | +export function getCrypto(): Crypto { |
| 94 | + if (typeof crypto !== undefined) { |
| 95 | + return crypto; |
| 96 | + } |
| 97 | + // This would indicate running in an environment that doesn't have window.crypto or self.crypto. |
| 98 | + throw Error('Access to a web crypto API is required'); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Get the visibility state. For non-documents this will always be 'invisible'. |
| 103 | + * |
| 104 | + * @returns The document visibility. |
| 105 | + */ |
| 106 | +export function getVisibility(): string { |
| 107 | + if (isDocument()) { |
| 108 | + return document.visibilityState; |
| 109 | + } |
| 110 | + return 'visibile'; |
| 111 | +} |
| 112 | + |
| 113 | +export function querySelectorAll(selector: string): NodeListOf<Element> | undefined { |
| 114 | + if (isDocument()) { |
| 115 | + return document.querySelectorAll(selector); |
| 116 | + } |
| 117 | + return undefined; |
| 118 | +} |
0 commit comments