Commit 1044a86b authored by Robert Knight's avatar Robert Knight

Simplify PDF viewer initialization in the dev server

Remove logic that is no longer required with the current Hypothesis
client and PDF.js version.

See https://github.com/hypothesis/browser-extension/pull/658.
parent ed132877
'use strict'; 'use strict';
// Note: This file is not transpiled. /* global PDFViewerApplication, PDFViewerApplicationOptions */
// Listen for `webviewerloaded` event to configure the viewer after its files // Listen for `webviewerloaded` event to configure the viewer after its files
// have been loaded but before it is initialized. // have been loaded but before it is initialized.
document.addEventListener('webviewerloaded', () => { document.addEventListener('webviewerloaded', () => {
const appOptions = window.PDFViewerApplicationOptions;
const app = window.PDFViewerApplication; const app = window.PDFViewerApplication;
// Ensure that PDF.js viewer events such as "documentloaded" are dispatched
// to the DOM. The client relies on this.
appOptions.set('eventBusDispatchToDOM', true);
// Disable preferences support, as otherwise this will result in `eventBusDispatchToDOM`
// being overridden with the default value of `false`.
appOptions.set('disablePreferences', true);
// Prevent loading of default viewer PDF. // Prevent loading of default viewer PDF.
appOptions.set('defaultUrl', ''); PDFViewerApplicationOptions.set('defaultUrl', '');
// Read configuration rendered into template as global vars. // Read configuration rendered into template as global vars.
const documentUrl = window.DOCUMENT_URL; const documentUrl = window.DOCUMENT_URL;
const url = window.PDF_URL; const url = window.PDF_URL;
const clientEmbedUrl = window.CLIENT_URL; const clientEmbedUrl = window.CLIENT_URL;
// Wait for the PDF viewer to be fully initialized and then load the Hypothesis client. PDFViewerApplication.initializedPromise.then(() => {
//
// This is required because the client currently assumes that `PDFViewerApplication`
// is fully initialized when it loads. Note that "fully initialized" only means
// that the PDF viewer application's components have been initialized. The
// PDF itself will still be loading, and the client will wait for that to
// complete before fetching annotations.
//
const pdfjsInitialized = new Promise(resolve => {
// Poll `app.initialized` as there doesn't appear to be an event that
// we can listen to.
const timer = setInterval(function () {
if (app.initialized) {
clearTimeout(timer);
resolve();
}
}, 20);
});
pdfjsInitialized.then(() => {
// Prevent PDF.js' `Promise` polyfill, if it was used, from being
// overwritten by the one that ships with Hypothesis (both from core-js).
//
// See https://github.com/hypothesis/via/issues/81#issuecomment-531121534
if (
typeof Promise === 'function' &&
typeof PromiseRejectionEvent === 'undefined'
) {
window.PromiseRejectionEvent = function FakePromiseRejectionEvent() {
// core-js doesn't actually use this, it just tests for `typeof PromiseRejectionEvent`
console.warn('Tried to construct fake `PromiseRejectionEvent`');
};
}
// Load the Hypothesis client. // Load the Hypothesis client.
const embedScript = document.createElement('script'); const embedScript = document.createElement('script');
embedScript.src = clientEmbedUrl; embedScript.src = clientEmbedUrl;
...@@ -72,7 +31,7 @@ document.addEventListener('webviewerloaded', () => { ...@@ -72,7 +31,7 @@ document.addEventListener('webviewerloaded', () => {
// and https://github.com/mozilla/pdf.js/issues/10435#issuecomment-452706770 // and https://github.com/mozilla/pdf.js/issues/10435#issuecomment-452706770
app.open({ app.open({
// Load PDF through Via to work around CORS restrictions. // Load PDF through Via to work around CORS restrictions.
url: url, url,
// Make sure `PDFViewerApplication.url` returns the original URL, as this // Make sure `PDFViewerApplication.url` returns the original URL, as this
// is the URL associated with annotations. // is the URL associated with annotations.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment