( session: NativeExportSession, plugin: CodeSpacePlugin, popupWindow: Window )
| 166 | } |
| 167 | |
| 168 | function installPopupPrintObserver( |
| 169 | session: NativeExportSession, |
| 170 | plugin: CodeSpacePlugin, |
| 171 | popupWindow: Window |
| 172 | ): () => void { |
| 173 | let cleanedUp = false; |
| 174 | let observer: MutationObserver | null = null; |
| 175 | let intervalId: number | null = null; |
| 176 | let unloadHandler: (() => void) | null = null; |
| 177 | |
| 178 | const cleanup = () => { |
| 179 | if (cleanedUp) return; |
| 180 | cleanedUp = true; |
| 181 | observer?.disconnect(); |
| 182 | if (intervalId !== null) { |
| 183 | session.ownerWindow.clearInterval(intervalId); |
| 184 | } |
| 185 | if (unloadHandler) { |
| 186 | popupWindow.removeEventListener("beforeunload", unloadHandler); |
| 187 | popupWindow.removeEventListener("unload", unloadHandler); |
| 188 | } |
| 189 | session.popupCleanups.delete(cleanup); |
| 190 | session.popupOpened = false; |
| 191 | maybeCleanupSession(session, "popup closed"); |
| 192 | }; |
| 193 | |
| 194 | const scanPopup = () => { |
| 195 | if (popupWindow.closed) { |
| 196 | debugNativePdf(session, "popup window closed"); |
| 197 | cleanup(); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | const popupDoc = popupWindow.document; |
| 202 | const popupBody = popupDoc.body; |
| 203 | if (!popupBody) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | const printRoots = Array.from(popupDoc.querySelectorAll<HTMLElement>(".print")); |
| 208 | const fileEmbedCount = popupDoc.querySelectorAll(".file-embed").length; |
| 209 | const signature = `print:${printRoots.length}|embed:${fileEmbedCount}`; |
| 210 | if (session.lastPopupScanSignature !== signature) { |
| 211 | session.lastPopupScanSignature = signature; |
| 212 | debugNativePdf(session, "popup scan state changed", { |
| 213 | printRoots: printRoots.length, |
| 214 | fileEmbeds: fileEmbedCount, |
| 215 | url: popupWindow.location.href, |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | if (printRoots.length > 0) { |
| 220 | for (const root of printRoots) { |
| 221 | void replacePopupCodeEmbeds(root, plugin, session.sourceFile.path); |
| 222 | } |
| 223 | return; |
| 224 | } |
| 225 |
no test coverage detected