Move, and modernize, Util.loadScript
from src/shared/util.js
to src/display/dom_utils.js
Not only is the `Util.loadScript` helper function unused on the Worker side, even trying to use it there would throw an Error (since `document` isn't defined/available in Workers). Hence this helper function is moved, and its code modernized slightly by having it return a Promise rather than needing a callback function. Finally, to reduced code duplication, the "new" loadScript function is exported and used in the viewer.
This commit is contained in:
parent
547f119be6
commit
07d610615c
@ -21,8 +21,8 @@ import {
|
||||
UnexpectedResponseException, UnknownErrorException, unreachable, Util, warn
|
||||
} from '../shared/util';
|
||||
import {
|
||||
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, PageViewport,
|
||||
RenderingCancelledException, StatTimer
|
||||
DOMCanvasFactory, DOMCMapReaderFactory, DummyStatTimer, loadScript,
|
||||
PageViewport, RenderingCancelledException, StatTimer
|
||||
} from './dom_utils';
|
||||
import { FontFaceObject, FontLoader } from './font_loader';
|
||||
import { apiCompatibilityParams } from './api_compatibility';
|
||||
@ -1356,7 +1356,7 @@ var PDFWorker = (function PDFWorkerClosure() {
|
||||
}
|
||||
} else {
|
||||
let loader = fakeWorkerFilesLoader || function(callback) {
|
||||
Util.loadScript(getWorkerSrc(), function() {
|
||||
loadScript(getWorkerSrc()).then(function() {
|
||||
callback(window.pdfjsWorker.WorkerMessageHandler);
|
||||
});
|
||||
};
|
||||
|
@ -428,6 +428,19 @@ class DummyStatTimer {
|
||||
}
|
||||
}
|
||||
|
||||
function loadScript(src) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let script = document.createElement('script');
|
||||
script.src = src;
|
||||
|
||||
script.onload = resolve;
|
||||
script.onerror = function() {
|
||||
reject(new Error(`Cannot load script at: ${script.src}`));
|
||||
};
|
||||
(document.head || document.documentElement).appendChild(script);
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
PageViewport,
|
||||
RenderingCancelledException,
|
||||
@ -440,4 +453,5 @@ export {
|
||||
DOMSVGFactory,
|
||||
StatTimer,
|
||||
DummyStatTimer,
|
||||
loadScript,
|
||||
};
|
||||
|
@ -108,6 +108,7 @@ exports.RenderingCancelledException =
|
||||
exports.getFilenameFromUrl = pdfjsDisplayDOMUtils.getFilenameFromUrl;
|
||||
exports.LinkTarget = pdfjsDisplayDOMUtils.LinkTarget;
|
||||
exports.addLinkAttributes = pdfjsDisplayDOMUtils.addLinkAttributes;
|
||||
exports.loadScript = pdfjsDisplayDOMUtils.loadScript;
|
||||
exports.GlobalWorkerOptions = pdfjsDisplayWorkerOptions.GlobalWorkerOptions;
|
||||
exports.apiCompatibilityParams =
|
||||
pdfjsDisplayAPICompatibility.apiCompatibilityParams;
|
||||
|
@ -908,21 +908,6 @@ var Util = (function UtilClosure() {
|
||||
}
|
||||
};
|
||||
|
||||
Util.loadScript = function Util_loadScript(src, callback) {
|
||||
var script = document.createElement('script');
|
||||
var loaded = false;
|
||||
script.setAttribute('src', src);
|
||||
if (callback) {
|
||||
script.onload = function() {
|
||||
if (!loaded) {
|
||||
callback();
|
||||
}
|
||||
loaded = true;
|
||||
};
|
||||
}
|
||||
document.getElementsByTagName('head')[0].appendChild(script);
|
||||
};
|
||||
|
||||
return Util;
|
||||
})();
|
||||
|
||||
|
51
web/app.js
51
web/app.js
@ -22,8 +22,8 @@ import {
|
||||
} from './ui_utils';
|
||||
import {
|
||||
build, createBlob, getDocument, getFilenameFromUrl, GlobalWorkerOptions,
|
||||
InvalidPDFException, LinkTarget, MissingPDFException, OPS, PDFWorker, shadow,
|
||||
UnexpectedResponseException, UNSUPPORTED_FEATURES, version
|
||||
InvalidPDFException, LinkTarget, loadScript, MissingPDFException, OPS,
|
||||
PDFWorker, shadow, UnexpectedResponseException, UNSUPPORTED_FEATURES, version
|
||||
} from 'pdfjs-lib';
|
||||
import { CursorTool, PDFCursorTools } from './pdf_cursor_tools';
|
||||
import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue';
|
||||
@ -1551,11 +1551,11 @@ if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
|
||||
}
|
||||
|
||||
function loadFakeWorker() {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (!GlobalWorkerOptions.workerSrc) {
|
||||
GlobalWorkerOptions.workerSrc = AppOptions.get('workerSrc');
|
||||
}
|
||||
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
|
||||
if (!GlobalWorkerOptions.workerSrc) {
|
||||
GlobalWorkerOptions.workerSrc = AppOptions.get('workerSrc');
|
||||
}
|
||||
if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (typeof SystemJS === 'object') {
|
||||
SystemJS.import('pdfjs/core/worker').then((worker) => {
|
||||
window.pdfjsWorker = worker;
|
||||
@ -1568,37 +1568,18 @@ function loadFakeWorker() {
|
||||
reject(new Error(
|
||||
'SystemJS or CommonJS must be used to load fake worker.'));
|
||||
}
|
||||
} else {
|
||||
let script = document.createElement('script');
|
||||
script.src = PDFWorker.getWorkerSrc();
|
||||
script.onload = function() {
|
||||
resolve();
|
||||
};
|
||||
script.onerror = function() {
|
||||
reject(new Error(`Cannot load fake worker at: ${script.src}`));
|
||||
};
|
||||
(document.head || document.documentElement).appendChild(script);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
return loadScript(PDFWorker.getWorkerSrc());
|
||||
}
|
||||
|
||||
function loadAndEnablePDFBug(enabledTabs) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let appConfig = PDFViewerApplication.appConfig;
|
||||
let script = document.createElement('script');
|
||||
script.src = appConfig.debuggerScriptPath;
|
||||
script.onload = function () {
|
||||
PDFBug.enable(enabledTabs);
|
||||
PDFBug.init({
|
||||
OPS,
|
||||
}, appConfig.mainContainer);
|
||||
resolve();
|
||||
};
|
||||
script.onerror = function () {
|
||||
reject(new Error('Cannot load debugger at ' + script.src));
|
||||
};
|
||||
(document.getElementsByTagName('head')[0] || document.body).
|
||||
appendChild(script);
|
||||
let appConfig = PDFViewerApplication.appConfig;
|
||||
return loadScript(appConfig.debuggerScriptPath).then(function() {
|
||||
PDFBug.enable(enabledTabs);
|
||||
PDFBug.init({
|
||||
OPS,
|
||||
}, appConfig.mainContainer);
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user