From 5c030793d1dd1acd8e7565a44ed459fc1a46eb78 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 2 May 2023 00:52:08 +0800 Subject: [PATCH] fix: resolve DOM elements of repo meta --- src/platforms/GitHub/DOMHelper.ts | 40 ++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index e2622fb2..83216e9e 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -6,10 +6,34 @@ import { formatClass, parseIntFromElement } from 'utils/DOMHelper' import { renderReact, run } from 'utils/general' import { CopyFileButton, copyFileButtonClassName } from './CopyFileButton' +const selectors = { + globalNavigation: { + navbar: { + repositoryOwner: [ + '.AppHeader-context-item[data-hovercard-type="user"]', + '.AppHeader-context-item[data-hovercard-type="organization"]', + ].join(), + // its meant to be the element visually next to the `repositoryOwner` element + repositoryName: + 'nav[role="navigation"] ul[role="list"] li:nth-child(2) .AppHeader-context-item', + }, + }, +} + export function resolveMeta(): Partial { const metaData = { - userName: $('[itemprop="author"] > a[rel="author"]', e => e.textContent?.trim()) || undefined, - repoName: $('[itemprop="name"] > a[href]', e => e.textContent?.trim()) || undefined, + userName: + $( + '[itemprop="author"] > a[rel="author"]', + e => e.textContent?.trim(), + () => $(selectors.globalNavigation.navbar.repositoryOwner, e => e.textContent?.trim()), + ) || undefined, + repoName: + $( + '[itemprop="name"] > a[href]', + e => e.textContent?.trim(), + () => $(selectors.globalNavigation.navbar.repositoryName, e => e.textContent?.trim()), + ) || undefined, branchName: getCurrentBranch(true), } if (!metaData.userName || !metaData.repoName) { @@ -19,15 +43,15 @@ export function resolveMeta(): Partial { } export function isInRepoPage() { - const repoHeadSelector = '.repohead' // legacy + const repoHeadSelector = '.repohead' const authorNameSelector = '.author[itemprop="author"]' - const globalNavigationSelectors = [ - '.AppHeader-context-item[data-hovercard-type="user"]', - '.AppHeader-context-item[data-hovercard-type="organization"]', - ].join() return Boolean( document.querySelector( - [repoHeadSelector, authorNameSelector, globalNavigationSelectors].join(), + [ + repoHeadSelector, + authorNameSelector, + selectors.globalNavigation.navbar.repositoryOwner, + ].join(), ), ) }