forked from electron/pdf-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
52 lines (45 loc) · 1.57 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Global PDFViewer object, accessible for testing.
* @type Object
*/
var viewer;
(function() {
/**
* Stores any pending messages received which should be passed to the
* PDFViewer when it is created.
* @type Array
*/
var pendingMessages = [];
/**
* Handles events that are received prior to the PDFViewer being created.
* @param {Object} message A message event received.
*/
function handleScriptingMessage(message) {
pendingMessages.push(message);
}
/**
* Initialize the global PDFViewer and pass any outstanding messages to it.
* @param {Object} browserApi An object providing an API to the browser.
*/
function initViewer(browserApi) {
// PDFViewer will handle any messages after it is created.
window.removeEventListener('message', handleScriptingMessage, false);
viewer = new PDFViewer(browserApi);
while (pendingMessages.length > 0)
viewer.handleScriptingMessage(pendingMessages.shift());
}
/**
* Entrypoint for starting the PDF viewer. This function obtains the browser
* API for the PDF and constructs a PDFViewer object with it.
*/
cr.sendWithPromise('initialize').then(function(opts) {
// Set up an event listener to catch scripting messages which are sent prior
// to the PDFViewer being created.
window.addEventListener('message', handleScriptingMessage, false);
createBrowserApi(opts).then(initViewer);
});
})()