feat: run script before the page is fully loaded

This commit is contained in:
Xmader 2020-12-28 00:51:36 -05:00
parent 2eca89e672
commit 003377e9ec
No known key found for this signature in database
GPG Key ID: A20B97FB9EB730E4
2 changed files with 22 additions and 2 deletions

View File

@ -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)

View File

@ -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()
}
}