feat: migrate pdfjs to local & bump ver

This commit is contained in:
2026-03-29 20:25:06 +10:30
parent e049f34a5e
commit 343fa7ca9f
5 changed files with 68 additions and 7 deletions
+26
View File
@@ -0,0 +1,26 @@
import * as pdfjs from "pdfjs-dist";
import browser from "webextension-polyfill";
import pdfWorkerHref from "pdfjs-dist/build/pdf.worker.min.mjs?url";
import pdfLegacyHref from "pdfjs-dist/legacy/build/pdf.min.mjs?url";
function extensionAssetUrl(viteAssetHref: string): string {
const path = viteAssetHref.replace(/^\/+/, "");
return browser.runtime.getURL(path);
}
let workerConfigured = false;
/** Required before pdfjs spawns a worker (content-script / extension isolate). */
export function ensurePdfjsWorker(): void {
if (workerConfigured) return;
pdfjs.GlobalWorkerOptions.workerSrc = extensionAssetUrl(pdfWorkerHref);
workerConfigured = true;
}
/** Page-context script on Firefox must load these chrome-extension:// URLs (see web_accessible_resources). */
export function getPdfjsPageContextUrls(): { lib: string; worker: string } {
return {
lib: extensionAssetUrl(pdfLegacyHref),
worker: extensionAssetUrl(pdfWorkerHref),
};
}
+6 -1
View File
@@ -32,7 +32,12 @@
],
"web_accessible_resources": [
{
"resources": ["resources/icons/*", "resources/update-image.webp"],
"resources": [
"resources/icons/*",
"resources/update-image.webp",
"resources/pdfjs/pdf.worker.min.mjs",
"resources/pdfjs/pdf.legacy.min.mjs"
],
"matches": ["*://*/*"]
}
]
@@ -1,8 +1,12 @@
import { getUserInfo } from "@/seqta/ui/AddBetterSEQTAElements.ts";
import ReactFiber from "@/seqta/utils/ReactFiber.ts";
import {
ensurePdfjsWorker,
getPdfjsPageContextUrls,
} from "@/lib/pdfjsExtension.ts";
import * as pdfjs from "pdfjs-dist";
pdfjs.GlobalWorkerOptions.workerSrc =
`https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
ensurePdfjsWorker();
export async function initStorage(api: any) {
await api.storage.loaded;
@@ -219,6 +223,12 @@ async function fetchPDFAsArrayBuffer(url: string): Promise<ArrayBuffer> {
export async function extractPDFText(url: string): Promise<string> {
try {
if (isFirefox) {
const { lib: pdfLibUrl, worker: pdfWorkerUrl } = getPdfjsPageContextUrls();
const escJsSingleQuoted = (s: string) =>
s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
const pdfLibInj = escJsSingleQuoted(pdfLibUrl);
const pdfWorkerInj = escJsSingleQuoted(pdfWorkerUrl);
return new Promise((resolve, reject) => {
const script = document.createElement("script");
const requestId = `pdf-extract-${Date.now()}-${Math.random()}`;
@@ -232,13 +242,15 @@ export async function extractPDFText(url: string): Promise<string> {
(function() {
const requestId = '${requestId}';
const url = '${escapedUrl}';
const pdfLibSrc = '${pdfLibInj}';
const pdfWorkerSrc = '${pdfWorkerInj}';
if (window.pdfjsLib) {
extractPDF();
} else {
const pdfjsScript = document.createElement('script');
pdfjsScript.src = 'https://cdn.jsdelivr.net/npm/pdfjs-dist/build/pdf.min.js';
pdfjsScript.type = 'text/javascript';
pdfjsScript.src = pdfLibSrc;
pdfjsScript.type = 'module';
pdfjsScript.onload = function() {
extractPDF();
@@ -256,7 +268,7 @@ export async function extractPDFText(url: string): Promise<string> {
function extractPDF() {
try {
window.pdfjsLib.GlobalWorkerOptions.workerSrc = '';
window.pdfjsLib.GlobalWorkerOptions.workerSrc = pdfWorkerSrc;
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);