unvendor: Replace pdfjs-dist

This commit is contained in:
Cohee
2024-10-17 01:16:39 +03:00
parent 553920ee8e
commit a32fe00e34
7 changed files with 524 additions and 46 deletions

1
public/global.d.ts vendored
View File

@@ -1,6 +1,5 @@
// Global namespace modules
declare var droll;
declare var pdfjsLib;
declare var Popper;
declare var showdown;
declare var showdownKatex;

View File

@@ -22,7 +22,6 @@
"seedrandom",
"showdown-katex",
"droll",
"pdfjs-dist",
"@mozilla/readability"
]
}

View File

@@ -10,6 +10,7 @@ import Handlebars from 'handlebars';
import css from '@adobe/css-tools';
import Bowser from 'bowser';
import ePub from 'epubjs';
import * as pdfjsLib from 'pdfjs-dist/webpack.mjs';
/**
* Expose the libraries to the 'window' object.
@@ -51,6 +52,7 @@ export default {
css,
Bowser,
ePub,
pdfjsLib,
};
export {
@@ -62,4 +64,5 @@ export {
css,
Bowser,
ePub,
pdfjsLib,
};

View File

@@ -1,4 +1,4 @@
import { DOMPurify, ePub } from '../lib.js';
import { DOMPurify, ePub, pdfjsLib } from '../lib.js';
import { getContext } from './extensions.js';
import { characters, getRequestHeaders, this_chid } from '../script.js';
@@ -1493,40 +1493,6 @@ export async function getReadableText(document, textSelector = 'body') {
* @returns {Promise<string>} A promise that resolves to the parsed text.
*/
export async function extractTextFromPDF(blob) {
async function initPdfJs() {
const promises = [];
const workerPromise = new Promise((resolve, reject) => {
const workerScript = document.createElement('script');
workerScript.type = 'module';
workerScript.async = true;
workerScript.src = 'lib/pdf.worker.mjs';
workerScript.onload = resolve;
workerScript.onerror = reject;
document.head.appendChild(workerScript);
});
promises.push(workerPromise);
const pdfjsPromise = new Promise((resolve, reject) => {
const pdfjsScript = document.createElement('script');
pdfjsScript.type = 'module';
pdfjsScript.async = true;
pdfjsScript.src = 'lib/pdf.mjs';
pdfjsScript.onload = resolve;
pdfjsScript.onerror = reject;
document.head.appendChild(pdfjsScript);
});
promises.push(pdfjsPromise);
return Promise.all(promises);
}
if (!('pdfjsLib' in window)) {
await initPdfJs();
}
const buffer = await getFileBuffer(blob);
const pdf = await pdfjsLib.getDocument(buffer).promise;
const pages = [];