feat: run script before the page is fully loaded
This commit is contained in:
parent
2eca89e672
commit
003377e9ec
2 changed files with 22 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
|||
import './meta'
|
||||
|
||||
import FileSaver from 'file-saver'
|
||||
import { waitForDocumentLoaded, console } from './utils'
|
||||
import { waitForSheetLoaded, console } from './utils'
|
||||
import { downloadPDF } from './pdf'
|
||||
import { downloadMscz } from './mscz'
|
||||
import { getFileUrl } from './file'
|
||||
|
@ -143,4 +143,4 @@ const main = (): void => {
|
|||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
waitForDocumentLoaded().then(main)
|
||||
waitForSheetLoaded().then(main)
|
||||
|
|
20
src/utils.ts
20
src/utils.ts
|
@ -136,3 +136,23 @@ export const waitForDocumentLoaded = (): Promise<void> => {
|
|||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run script before the page is fully loaded
|
||||
*/
|
||||
export const waitForSheetLoaded = (): Promise<void> => {
|
||||
if (document.readyState !== 'complete') {
|
||||
return new Promise(resolve => {
|
||||
const observer = new MutationObserver(() => {
|
||||
const img = document.querySelector('img')
|
||||
if (img) {
|
||||
resolve()
|
||||
observer.disconnect()
|
||||
}
|
||||
})
|
||||
observer.observe(document.body, { childList: true, subtree: true })
|
||||
})
|
||||
} else {
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue