OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Global PDFViewer object, accessible for testing. | 8 * Global PDFViewer object, accessible for testing. |
9 * @type Object | 9 * @type Object |
10 */ | 10 */ |
11 var viewer; | 11 var viewer; |
12 | 12 |
13 /** | |
14 * Entrypoint for starting the PDF viewer. This function obtains the details | |
15 * of the PDF 'stream' (the data that points to the PDF) and constructs a | |
16 * PDFViewer object with it. | |
17 */ | |
18 (function main() { | |
19 // If the viewer is started from the browser plugin, the view ID will be | |
20 // passed in which identifies the instance of the plugin. | |
21 var params = window.location.search.substring(1).split('='); | |
22 if (params.length == 2 && params[0] == 'id') { | |
23 var viewId = params[1]; | |
24 | 13 |
25 // Send a message to the background page to obtain the stream details. It | 14 (function() { |
26 // will run the callback function passed in to initialize the viewer. | 15 /** |
27 chrome.runtime.sendMessage( | 16 * Stores any pending messages received which should be passed to the |
28 'mhjfbmdgcfjbbpaeojofohoefgiehjai', | 17 * PDFViewer when it is created. |
29 {viewId: viewId}, | 18 * @type Array |
30 function(streamDetails) { viewer = new PDFViewer(streamDetails); }); | 19 */ |
31 return; | 20 var pendingMessages = []; |
21 | |
22 /** | |
23 * Handles events that are received prior to the PDFViewer being created. | |
24 * @param {Object} message A message event received. | |
25 */ | |
26 function handleScriptingMessage(message) { | |
27 pendingMessages.push(message); | |
32 } | 28 } |
33 | 29 |
34 // The viewer may be started directly by passing in the URL of the PDF to load | 30 /** |
35 // as the query string. This is used for print preview in particular. The URL | 31 * Initialize the global PDFViewer and pass any outstanding messages to it. |
36 // of this page will be of the form | 32 * @param {Object} streamDetails The stream object which points to the data |
37 // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url> | 33 * contained in the PDF. |
38 // part here. | 34 */ |
39 var url = window.location.search.substring(1); | 35 function initViewer(streamDetails) { |
40 var streamDetails = { | 36 // PDFViewer will handle any messages after it is created. |
41 streamUrl: url, | 37 window.removeEventListener('message', handleScriptingMessage, false); |
42 originalUrl: url, | |
43 responseHeaders: '', | |
44 embedded: window.parent != window, | |
45 tabId: -1 | |
46 }; | |
47 if (!chrome.tabs) { | |
48 viewer = new PDFViewer(streamDetails); | 38 viewer = new PDFViewer(streamDetails); |
49 return; | 39 while (pendingMessages.length > 0) |
40 viewer.handleScriptingMessage(pendingMessages.shift()); | |
50 } | 41 } |
51 chrome.tabs.getCurrent(function(tab) { | 42 |
52 if (tab && tab.id != undefined) | 43 /** |
53 streamDetails.tabId = tab.id; | 44 * Entrypoint for starting the PDF viewer. This function obtains the details |
54 viewer = new PDFViewer(streamDetails); | 45 * of the PDF 'stream' (the data that points to the PDF) and constructs a |
55 }); | 46 * PDFViewer object with it. |
47 */ | |
48 function main() { | |
49 // Set up an event listener to catch scripting messages which are sent prior | |
50 // to the PDFViewer being created. | |
51 window.addEventListener('message', handleScriptingMessage, false); | |
52 | |
53 // If the viewer is started from the browser plugin, the view ID will be | |
54 // passed in which identifies the instance of the plugin. | |
55 var params = window.location.search.substring(1).split('='); | |
56 if (params.length == 2 && params[0] == 'id') { | |
57 var viewId = params[1]; | |
58 | |
59 // Send a message to the background page to obtain the stream details. It | |
60 // will run the callback function passed in to initialize the viewer. | |
61 chrome.runtime.sendMessage( | |
62 'mhjfbmdgcfjbbpaeojofohoefgiehjai', | |
63 {viewId: viewId}, | |
64 function(streamDetails) { initViewer(streamDetails); }); | |
Sam McNally
2015/01/08 03:50:26
Pass initViewer directly.
raymes
2015/01/08 22:08:21
Done.
| |
65 return; | |
66 } | |
67 | |
68 // The viewer may be started directly by passing in the URL of the PDF to | |
69 // load as the query string. This is used for print preview in particular. | |
70 // The URL of this page will be of the form | |
71 // 'chrome-extension://<extension id>?<pdf url>'. We pull out the <pdf url> | |
72 // part here. | |
73 var url = window.location.search.substring(1); | |
74 var streamDetails = { | |
75 streamUrl: url, | |
76 originalUrl: url, | |
77 responseHeaders: '', | |
78 embedded: window.parent != window, | |
79 tabId: -1 | |
80 }; | |
81 if (!chrome.tabs) { | |
82 initViewer(streamDetails); | |
83 return; | |
84 } | |
85 chrome.tabs.getCurrent(function(tab) { | |
86 if (tab && tab.id != undefined) | |
87 streamDetails.tabId = tab.id; | |
88 initViewer(streamDetails); | |
89 }); | |
90 } | |
91 | |
92 main(); | |
Sam McNally
2015/01/08 03:50:26
How about doing the (function() {})() thing here t
raymes
2015/01/08 22:08:21
I almost did but it added more brackets everywhere
| |
56 })(); | 93 })(); |
OLD | NEW |