diff --git a/src/main.ts b/src/main.ts index 2b1037a..f1643ee 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) diff --git a/src/utils.ts b/src/utils.ts index d08f4fe..6e91c33 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -136,3 +136,23 @@ export const waitForDocumentLoaded = (): Promise => { return Promise.resolve() } } + +/** + * Run script before the page is fully loaded + */ +export const waitForSheetLoaded = (): Promise => { + 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() + } +}