MCPcopy Create free account
hub / github.com/UNLINEARITY/Obsidian-CodeSpace / installPopupPrintObserver

Function installPopupPrintObserver

src/native_pdf_export_patch.ts:168–258  ·  view source on GitHub ↗
(
	session: NativeExportSession,
	plugin: CodeSpacePlugin,
	popupWindow: Window
)

Source from the content-addressed store, hash-verified

166}
167
168function 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

Callers 1

installPopupWindowHookFunction · 0.85

Calls 4

debugNativePdfFunction · 0.85
scanPopupFunction · 0.85
cleanupFunction · 0.85
warnNativePdfFunction · 0.85

Tested by

no test coverage detected