diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index 43e8f14..c9c386b 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -31,7 +31,7 @@ export function isInCodePage() { } export function getIssueTitle() { - const title: string | undefined = $('.gh-header-title')?.textContent + const title = $('.gh-header-title')?.textContent return title?.trim().replace(/\n/g, '') } @@ -40,7 +40,7 @@ export function getCurrentBranch(passive = false) { '.repository-content #branch-select-menu summary', '.repository-content .branch-select-menu summary', ].join() - const branchButtonElement: HTMLElement = $(selectedBranchButtonSelector) + const branchButtonElement = $(selectedBranchButtonSelector) if (branchButtonElement) { const branchNameSpanElement = branchButtonElement.querySelector('span') if (branchNameSpanElement) { diff --git a/src/platforms/Gitea/DOMHelper.ts b/src/platforms/Gitea/DOMHelper.ts index d5ae608..acd7043 100644 --- a/src/platforms/Gitea/DOMHelper.ts +++ b/src/platforms/Gitea/DOMHelper.ts @@ -13,12 +13,11 @@ export function isInCodePage() { export function getCurrentBranch() { const branchListSelector = '.reference' - const branchButtonElement: HTMLElement = $(branchListSelector) - const branchNameElement = branchButtonElement.querySelector('.text > strong') + const branchButtonElement = $(branchListSelector) + const branchNameElement = branchButtonElement?.querySelector('.text > strong') if (branchNameElement) { - return branchNameElement.textContent; + return branchNameElement.textContent } raiseError(new Error('cannot get current branch')) } - diff --git a/src/platforms/Gitee/DOMHelper.ts b/src/platforms/Gitee/DOMHelper.ts index 4b99612..2075b56 100644 --- a/src/platforms/Gitee/DOMHelper.ts +++ b/src/platforms/Gitee/DOMHelper.ts @@ -15,14 +15,11 @@ export function isInCodePage() { } export function getCurrentBranch() { - const selectedBranchButtonSelector = '#git-project-branch' - const branchButtonElement: HTMLElement = $(selectedBranchButtonSelector) - if (branchButtonElement) { - const branchNameSpanElement = branchButtonElement.querySelector('.text') - if (branchNameSpanElement) { - const partialBranchNameFromInnerText = branchNameSpanElement.textContent - if (!partialBranchNameFromInnerText?.includes('…')) return partialBranchNameFromInnerText - } + const selectedBranchButtonSelector = '#git-project-branch .text' + const element = $(selectedBranchButtonSelector) + if (element) { + const partialBranchNameFromInnerText = element.textContent + if (!partialBranchNameFromInnerText?.includes('…')) return partialBranchNameFromInnerText } raiseError(new Error('cannot get current branch')) diff --git a/src/utils/DOMHelper.ts b/src/utils/DOMHelper.ts index 38f9fcd..8651b7a 100644 --- a/src/utils/DOMHelper.ts +++ b/src/utils/DOMHelper.ts @@ -26,20 +26,22 @@ export function setBodyIndent(shouldShowGitako: boolean) { } } -export function $ any, O extends () => any>( +export function $(selector: string): HTMLElement | null +export function $(selector: string, existCallback: (element: HTMLElement) => T1): T1 +export function $( selector: string, - existCallback?: E, - otherwise?: O, -): E extends never - ? O extends never - ? Element | null - : ReturnType | null - : O extends never - ? ReturnType | null - : ReturnType | ReturnType { + existCallback: (element: HTMLElement) => T1, + otherwise: () => T2, +): T1 | T2 +export function $( + selector: string, + existCallback: undefined | null, + otherwise: () => T2, +): HTMLElement | null | T2 +export function $(selector: string, existCallback?: any, otherwise?: any) { const element = document.querySelector(selector) if (element) { - return existCallback ? existCallback(element as EE) : element + return existCallback ? existCallback(element) : element } return otherwise ? otherwise() : null }