fix: try fragment selectors in order

This commit is contained in:
Enix 2023-01-29 17:08:49 +08:00 committed by GitHub
parent 156c8c913e
commit 6e8a8b09b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 14 deletions

View file

@ -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<Document[]> {
// 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<Document[]> {
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(

View file

@ -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) {
/**
* <include-fragment
* src="..."
@ -91,17 +91,21 @@ export async function continuousLoadPages(doc: Document, onReceivePage?: (doc: D
'include-fragment[data-targets="diff-file-filter.progressiveLoaders"]',
'.js-diff-progressive-container include-fragment[src]', // legacy support
]
const documents: Document[] = [doc]
// eslint-disable-next-line no-constant-condition
while (true) {
const fragment = doc.querySelector(fragmentSelectors.join()) as HTMLElement
if (!fragment) break
const src = fragment.getAttribute('src')
if (!src) break
// Using `src` directly 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)
onReceivePage?.(doc)
const selector = fragmentSelectors.find(selector => 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
}