mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-06 03:34:40 +00:00
feat: migrate pdfjs to local & bump ver
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "betterseqtaplus",
|
||||
"version": "3.5.1",
|
||||
"version": "3.5.2",
|
||||
"type": "module",
|
||||
"description": "Enhance SEQTA Learn's usability and aesthetics! A fork of BetterSEQTA to continue development add add heaps more features!",
|
||||
"browserslist": "> 0.5%, last 2 versions, not dead",
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -84,6 +84,24 @@ export default defineConfig(({ command }) => ({
|
||||
settings: join(__dirname, "src", "interface", "index.html"),
|
||||
pageState: join(__dirname, "src", "pageState.js"),
|
||||
},
|
||||
output: {
|
||||
assetFileNames(info) {
|
||||
const labels = [
|
||||
...(info.names ?? []),
|
||||
...(info.originalFileNames ?? []),
|
||||
].join(" ");
|
||||
if (
|
||||
labels.includes("pdf.worker.min") ||
|
||||
labels.includes("pdf.worker.mjs")
|
||||
) {
|
||||
return "resources/pdfjs/pdf.worker.min.mjs";
|
||||
}
|
||||
if (labels.includes("legacy") && labels.includes("pdf.min")) {
|
||||
return "resources/pdfjs/pdf.legacy.min.mjs";
|
||||
}
|
||||
return "assets/[name]-[hash][extname]";
|
||||
},
|
||||
},
|
||||
onwarn(warning, warn) {
|
||||
if (warning.code === "FILE_NAME_CONFLICT") return;
|
||||
warn(warning);
|
||||
|
||||
Reference in New Issue
Block a user