diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index 7a0c776..4d2d60c 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -43,8 +43,8 @@ const selectors = { }, } -const getDOMJSON = (selector: string) => - $(selector, e => { +const getDOMJSON = (selector: string, _$ = $) => + _$(selector, e => { try { return JSON.parse(e.textContent || '') } catch (error) { diff --git a/src/utils/$.ts b/src/utils/$.ts index aee42ef..714cc71 100644 --- a/src/utils/$.ts +++ b/src/utils/$.ts @@ -1,20 +1,25 @@ -export function $(selector: string): E | null -export function $(selector: string, existCallback: (element: HTMLElement) => R1): R1 | null -export function $( - selector: string, - existCallback: (element: HTMLElement) => R1, - otherwise: () => R2, -): R1 | R2 -export function $( - selector: string, - existCallback: undefined | null, - otherwise: () => R2, -): E | R2 -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export function $(selector: string, existCallback?: any, otherwise?: any) { - const element = document.querySelector(selector) - if (element) { - return existCallback ? existCallback(element) : element - } - return otherwise ? otherwise() : null +export interface $ { + (selector: string): E | null + (selector: string, existCallback: (element: HTMLElement) => R1): R1 | null + (selector: string, existCallback: (element: HTMLElement) => R1, otherwise: () => R2): + | R1 + | R2 + ( + selector: string, + existCallback: undefined | null, + otherwise: () => R2, + ): E | R2 } + +export const make$ = + (root?: Document | HTMLElement): $ => + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (selector: string, existCallback?: any, otherwise?: any) => { + const element = (root ?? document).querySelector(selector) + if (element) { + return existCallback ? existCallback(element) : element + } + return otherwise ? otherwise() : null + } + +export const $: $ = make$()