Add a basic navigator polyfill for older Node.js versions
Modern Node.js versions now include a `navigator` implementation, with a few basic properties, that's actually enough for the PDF.js use-cases; please see https://nodejs.org/api/globals.html#navigator Unfortunately we still support Node.js version `20`, hence we add a basic polyfill since that allows simplifying the code slightly.
This commit is contained in:
parent
b47b248e15
commit
3d4e8bb17e
@ -67,6 +67,13 @@ if (isNodeJS) {
|
||||
warn("Cannot polyfill `Path2D`, rendering may be broken.");
|
||||
}
|
||||
}
|
||||
if (!globalThis.navigator?.language) {
|
||||
globalThis.navigator = {
|
||||
language: "en-US",
|
||||
platform: "",
|
||||
userAgent: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -643,30 +643,16 @@ class FeatureTest {
|
||||
}
|
||||
|
||||
static get platform() {
|
||||
if (
|
||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||
(typeof navigator !== "undefined" &&
|
||||
typeof navigator?.platform === "string" &&
|
||||
typeof navigator?.userAgent === "string")
|
||||
) {
|
||||
const { platform, userAgent } = navigator;
|
||||
const { platform, userAgent } = navigator;
|
||||
|
||||
return shadow(this, "platform", {
|
||||
isAndroid: userAgent.includes("Android"),
|
||||
isLinux: platform.includes("Linux"),
|
||||
isMac: platform.includes("Mac"),
|
||||
isWindows: platform.includes("Win"),
|
||||
isFirefox:
|
||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||
userAgent.includes("Firefox"),
|
||||
});
|
||||
}
|
||||
return shadow(this, "platform", {
|
||||
isAndroid: false,
|
||||
isLinux: false,
|
||||
isMac: false,
|
||||
isWindows: false,
|
||||
isFirefox: false,
|
||||
isAndroid: userAgent.includes("Android"),
|
||||
isLinux: platform.includes("Linux"),
|
||||
isMac: platform.includes("Mac"),
|
||||
isWindows: platform.includes("Win"),
|
||||
isFirefox:
|
||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||
userAgent.includes("Firefox"),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -19,13 +19,16 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||
if (
|
||||
typeof PDFJSDev !== "undefined" &&
|
||||
PDFJSDev.test("LIB") &&
|
||||
typeof navigator === "undefined"
|
||||
!globalThis.navigator?.language
|
||||
) {
|
||||
globalThis.navigator = Object.create(null);
|
||||
globalThis.navigator = {
|
||||
language: "en-US",
|
||||
maxTouchPoints: 1,
|
||||
platform: "",
|
||||
userAgent: "",
|
||||
};
|
||||
}
|
||||
const userAgent = navigator.userAgent || "";
|
||||
const platform = navigator.platform || "";
|
||||
const maxTouchPoints = navigator.maxTouchPoints || 1;
|
||||
const { maxTouchPoints, platform, userAgent } = navigator;
|
||||
|
||||
const isAndroid = /Android/.test(userAgent);
|
||||
const isIOS =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user