Skip to content

Instantly share code, notes, and snippets.

@moorage
Created January 20, 2024 08:21
Show Gist options
  • Save moorage/908bca6c0976df8ea2ee1021d666bfa6 to your computer and use it in GitHub Desktop.
Save moorage/908bca6c0976df8ea2ee1021d666bfa6 to your computer and use it in GitHub Desktop.
shim to get clerk in background worker for chrome extension
import { i18n } from 'webextension-polyfill';
if (typeof window === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).window = {
fetch: fetch,
navigator: navigator,
location: {
hash: '',
host: i18n.getMessage(extensionIdString),
hostname: i18n.getMessage(extensionIdString),
href: 'chrome-extension://' + i18n.getMessage(extensionIdString),
origin: 'chrome-extension://' + i18n.getMessage(extensionIdString),
pathname: '/',
port: '',
protocol: 'chrome-extension:',
search: '',
},
name: '',
status: '',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addEventListener: (...args: any[]) => {
console.debug('fake window.addEventListener', args);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
removeEventListener: (...args: any[]) => {
console.debug('fake window.removeEventListener', args);
},
// localStorage: localStorage,
};
}
if (typeof document === 'undefined') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).document = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
addEventListener: (...args: any[]) => {
console.debug('fake document.addEventListener', args);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
querySelector: (...args: any[]) => {
console.debug('fake document.querySelector', args);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
querySelectorAll: (...args: any[]) => {
console.debug('fake document.querySelectorAll', args);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getElementById: (...args: any[]) => {
console.debug('fake document.getElementById', args);
},
hasFocus: () => {
return true; // assume we always want this to be true
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appendChild: (...args: any[]) => {
console.debug('fake document.body.appendChild', args);
},
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createElement: (...args: any[]) => {
console.debug('fake document.createElement', args);
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setAttribute: (...args2: any[]) => {
console.debug('fake document.createElement.setAttribute', args2);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appendChild: (...args2: any[]) => {
console.debug('fake document.createElement.appendChild', args2);
},
};
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appendChild: (...args: any[]) => {
console.debug('fake document.appendChild', args);
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment