From f22f27052346e8392abe7961558386a698b32670 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 13 Nov 2020 16:55:56 +0800 Subject: [PATCH] fix: resolve sha from partially loaded DOM can be unreliable --- src/platforms/GitHub/API.ts | 6 ++++-- src/platforms/GitHub/index.ts | 7 +------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index d0a6657..083912e 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -115,9 +115,11 @@ export async function getPullPageDocument( const filesDOM = await getDOM( `https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files?_pjax=%23js-repo-pjax-container`, ) + const hookElement: HTMLDivElement | null = filesDOM.querySelector('div.js-pull-refresh-on-pjax') + const hookSearchParams = new URLSearchParams(hookElement?.dataset.url) const [baseSHA, headSHA] = [ - filesDOM.querySelector('input[name="comparison_start_oid"]')?.getAttribute('value'), - filesDOM.querySelector('input[name="comparison_end_oid"]')?.getAttribute('value'), + hookSearchParams.get('start_commit_oid'), + hookSearchParams.get('end_commit_oid'), ] if (!baseSHA || !headSHA) throw new Error(`Cannot fetch SHA for comparison`) diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 38c6efb..9aba232 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -249,12 +249,7 @@ export const GitHub: Platform = { } async function createPullFileResolver(userName: string, repoName: string, pullId: string) { - let doc: Document - if (URLHelper.parse().path[1] === 'files') { - doc = document - } else { - doc = await API.getPullPageDocument(userName, repoName, pullId) - } + const doc = await API.getPullPageDocument(userName, repoName, pullId) return (path: string) => { const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id