chore(pdfjs): first attempt at intercepting store

This commit is contained in:
Elian Doran 2025-12-29 14:06:59 +02:00
parent 7836de3f08
commit fc0ea36cf3
No known key found for this signature in database
2 changed files with 35 additions and 3 deletions

View File

@ -1,4 +1,4 @@
const LOG_EVENT_BUS = true;
const LOG_EVENT_BUS = false;
async function main() {
// Wait for the PDF viewer application to be available.
@ -11,12 +11,14 @@ async function main() {
patchEventBus();
}
app.eventBus.on("documentloaded", () => {
manageSave(app);
interceptViewHistory();
manageSave();
});
await app.initializedPromise;
};
function manageSave(app: typeof window.PDFViewerApplication) {
function manageSave() {
const app = window.PDFViewerApplication;
const storage = app.pdfDocument.annotationStorage;
let timeout = null;
@ -49,6 +51,22 @@ function manageSave(app: typeof window.PDFViewerApplication) {
});
}
function interceptViewHistory() {
const app = window.PDFViewerApplication;
let activeFingerprint: string = app.pdfDocument.fingerprints[0];
const store = app.store;
store._writeToStorage = async function() {
const fileEntry = store.database.files?.find(f => f.fingerprint === activeFingerprint);
const databaseStr = JSON.stringify(fileEntry);
console.log("Write attempt.", databaseStr);
}
store._readFromStorage = async function() {
console.log("Read attempt", activeFingerprint);
return "{}";
}
}
function patchEventBus() {
const eventBus = window.PDFViewerApplication.eventBus;
const originalDispatch = eventBus.dispatch.bind(eventBus);

View File

@ -1,6 +1,19 @@
import type { PDFDocumentProxy } from "pdfjs-dist";
declare global {
/**
* @source https://github.com/mozilla/pdf.js/blob/master/web/view_history.js
*/
interface ViewHistory {
database: {
files?: {
fingerprint: string;
}[];
},
_writeToStorage: () => Promise<void>;
_readFromStorage: () => Promise<string>;
}
interface Window {
PDFViewerApplication?: {
initializedPromise: Promise<void>;
@ -9,6 +22,7 @@ declare global {
on(event: string, listener: (...args: any[]) => void): void;
dispatch(event: string, data?: any): void;
};
store: ViewHistory;
};
}
}