From 6e8a8b09b51e93dbb2224b699840e5d4286c0d1f Mon Sep 17 00:00:00 2001 From: Enix Date: Sun, 29 Jan 2023 17:08:49 +0800 Subject: [PATCH] fix: try fragment selectors in order --- src/platforms/GitHub/API.ts | 6 +++--- src/platforms/GitHub/utils.ts | 26 +++++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 7dbf825..3bc7ac0 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -2,7 +2,7 @@ import { errors } from 'platforms' import { isEnterprise } from '.' import { is } from '../../utils/is' import { gitakoServiceHost } from '../../utils/networkService' -import { continuousLoadPages, getDOM, resolveHeaderLink } from './utils' +import { continuousLoadFragmentedPages, getDOM, resolveHeaderLink } from './utils' function isAPIRateLimitExceeded(content: JSONValue) { return ( @@ -147,7 +147,7 @@ export async function getPullPageDocuments( document?: Document, ): Promise { // Response of this API contains view of few files but is not complete. - return continuousLoadPages( + return continuousLoadFragmentedPages( document || (await getDOM(`${window.location.origin}/${userName}/${repoName}/pull/${pullId}/files`)), ) @@ -158,7 +158,7 @@ export async function getCommitPageDocuments(): Promise { repoName: string, commitId: string, */ // arguments are not used because info are collected from DOM directly - return continuousLoadPages(document) + return continuousLoadFragmentedPages(document) } export async function getBlobData( diff --git a/src/platforms/GitHub/utils.ts b/src/platforms/GitHub/utils.ts index 7fdb54b..1852ed6 100644 --- a/src/platforms/GitHub/utils.ts +++ b/src/platforms/GitHub/utils.ts @@ -78,7 +78,7 @@ export async function getDOM(url: string) { return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html') } -export async function continuousLoadPages(doc: Document, onReceivePage?: (doc: Document) => void) { +export async function continuousLoadFragmentedPages(doc: Document) { /** * doc.querySelector(selector)) + if (selector) { + // eslint-disable-next-line no-constant-condition + while (true) { + const fragment = doc.querySelector(selector) + if (!(fragment instanceof HTMLElement)) break + const src = fragment.getAttribute('src') + if (!src) break + // Using `src` without origin below would fail in Firefox if the src is an absolute path + doc = await getDOM(new URL(src, window.location.origin).href) + documents.push(doc) + } } return documents }