All files / lib/esm/Util DomEnvironment.js

0% Statements 0/3
0% Branches 0/4
0% Functions 0/2
0% Lines 0/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22                                           
/**
 * Helpers for detecting a real browser document.
 *
 * MV3 service workers (and Node) have no `document`. Core crypto/fetch paths are SW-safe;
 * UI-only helpers ({@link DownloadService}, OTP QR canvas rendering) must call these guards
 * so they fail with a clear error instead of a cryptic `document is not defined`.
 */
export function isDocumentAvailable() {
    return typeof document !== "undefined"
        && typeof document.createElement === "function";
}
/**
 * Throws if `document` is not available (e.g. MV3 service worker / Node).
 * @param feature Human-readable name of the DOM-only feature for the error message
 */
export function assertDocumentAvailable(feature) {
    if (!isDocumentAvailable()) {
        throw new Error(`${feature} requires a DOM (document) and cannot run in an MV3 service worker. ` +
            `Call it from a page, popup, or offscreen document, or use the non-DOM API instead.`);
    }
}