Skip to content

Commit

Permalink
fix: update GitHub page content selector
Browse files Browse the repository at this point in the history
  • Loading branch information
EnixCoda committed Jul 2, 2022
1 parent 919ce07 commit 3ed0a8e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion __tests__/cases/non-parallel/pjax.commits-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe(`in Gitako project page`, () => {
it('should not break go back in history', async () => {
for (let i = 0; i < 3; i++) {
const commitLinks = await page.$$(
`#js-repo-pjax-container .TimelineItem-body ol li > div:nth-child(1) a[href*="/commit/"]`,
`main .TimelineItem-body ol li > div:nth-child(1) a[href*="/commit/"]`,
)
if (commitLinks.length < 2) throw new Error(`No enough commits`)
commitLinks[i].click()
Expand Down
24 changes: 12 additions & 12 deletions src/platforms/GitHub/DOMHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function getIssueTitle() {

export function getCurrentBranch(passive = false) {
const selectedBranchButtonSelector = [
'.repository-content #branch-select-menu summary',
'.repository-content .branch-select-menu summary',
'main #branch-select-menu summary',
'main .branch-select-menu summary',
].join()
const branchButtonElement = $(selectedBranchButtonSelector)
if (branchButtonElement) {
Expand All @@ -55,7 +55,7 @@ export function getCurrentBranch(passive = false) {
}

const findFileButtonSelector =
'#js-repo-pjax-container .repository-content .file-navigation a[data-hotkey="t"]'
'main .file-navigation a[data-hotkey="t"]'
const urlFromFindFileButton: string | null = $(
findFileButtonSelector,
element => (element as HTMLAnchorElement).href,
Expand Down Expand Up @@ -96,8 +96,8 @@ const PAGE_TYPES = {
*/
function getCurrentPageType() {
const blobPathSelector = '#blob-path' // path next to branch switcher
const blobWrapperSelector = '.repository-content .blob-wrapper table'
const readmeSelector = '.repository-content .readme'
const blobWrapperSelector = 'main .blob-wrapper table'
const readmeSelector = 'main .readme'
const searchResultSelector = '.codesearch-results'
return (
$(searchResultSelector, () => PAGE_TYPES.SEARCH) ||
Expand All @@ -110,7 +110,7 @@ function getCurrentPageType() {
const REPO_TYPE_PRIVATE = 'private' as const
const REPO_TYPE_PUBLIC = 'public' as const
export function getRepoPageType() {
const headerSelector = `#js-repo-pjax-container .pagehead.repohead h1`
const headerSelector = `main .pagehead.repohead h1`
return $(headerSelector, header => {
const repoPageTypes = [REPO_TYPE_PRIVATE, REPO_TYPE_PUBLIC]
for (const repoPageType of repoPageTypes) {
Expand All @@ -127,7 +127,7 @@ export function getRepoPageType() {
*/
export function getCodeElement() {
if (getCurrentPageType() === PAGE_TYPES.RAW_TEXT) {
const codeContentSelector = '.repository-content .data table'
const codeContentSelector = 'main .data table'
const codeContentElement = $(codeContentSelector)
if (!codeContentElement) {
raiseError(new Error('cannot find code content element'))
Expand Down Expand Up @@ -158,7 +158,7 @@ export function attachCopyFileBtn() {
}

if (!buttonGroup) {
const buttonGroupSelector = '.repository-content .Box-header .BtnGroup'
const buttonGroupSelector = 'main .Box-header .BtnGroup'
const buttonGroups = document.querySelectorAll(buttonGroupSelector)
const $buttonGroup = buttonGroups[buttonGroups.length - 1]
if ($buttonGroup) buttonGroup = $buttonGroup as HTMLElement
Expand All @@ -180,9 +180,9 @@ export function attachCopyFileBtn() {
}

export function attachCopySnippet() {
const readmeSelector = '.repository-content div#readme'
const readmeSelector = 'main div#readme'
return $(readmeSelector, () => {
const readmeArticleSelector = '.repository-content div#readme article'
const readmeArticleSelector = 'main div#readme article'
return $(
readmeArticleSelector,
readmeElement => {
Expand Down Expand Up @@ -227,10 +227,10 @@ export function attachCopySnippet() {
},
() => {
// in URL like `/{user}/{repo}/delete/{branch}/path/to/file
const deleteReadmeSelector = '.repository-content div#readme del'
const deleteReadmeSelector = 'main div#readme del'
if (!$(deleteReadmeSelector)) {
// in pages where readme is not markdown, e.g. txt
const plainReadmeSelector = '.repository-content div#readme .plain'
const plainReadmeSelector = 'main div#readme .plain'
if (!$(plainReadmeSelector)) {
raiseError(
new Error('cannot find mount point for copy snippet button while readme exists'),
Expand Down
5 changes: 1 addition & 4 deletions src/platforms/GitHub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ function resolvePageScope(defaultBranchName?: string) {

const pathSHAMap = new Map<string, string>()

// Try lookup PJAX containers, #js-repo-pjax-container could exist while #repo-content-pjax-container does not.
const pjaxContainerSelector = ['#repo-content-pjax-container', '#js-repo-pjax-container'].find(
selector => document.querySelector(selector),
)
const pjaxContainerSelector = 'main'
const turboContainerId = 'repo-content-turbo-frame'

export const GitHub: Platform = {
Expand Down

0 comments on commit 3ed0a8e

Please sign in to comment.