mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Proof of concept using chrome.streamsPrivate API
This method captures all application/pdf streams, loads the viewer and passes the stream to the PDF.js viewer. This commit shows a proof of concept using the chrome.streamsPrivate API. Advantages of new method: - Access to the response body of the original request, thus fewer network requests. - PDFs from non-GET requests (e.g. POST) are now supported. - FTP files are also supported. Possible improvements: - Use declared content scripts instead of dynamic chrome.tabs.executeScript. This allows the extension to render the viewer in frames when the extension is disallowed to run executeScript for the top URL. - Use chrome.declarativeWebRequest instead of webRequest, and replace background page with event page (don't forget to profile the difference & will the background/event page still work as intended?).
This commit is contained in:
parent
ab4f27b272
commit
cdadb0db4d
5 changed files with 198 additions and 2 deletions
46
web/chromecom.js
Normal file
46
web/chromecom.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* Copyright 2013 Mozilla Foundation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* globals chrome */
|
||||
'use strict';
|
||||
|
||||
var ChromeCom = (function ChromeComClosure() {
|
||||
return {
|
||||
/**
|
||||
* Creates an event that the extension is listening for and will
|
||||
* asynchronously respond by calling the callback.
|
||||
* @param {String} action The action to trigger.
|
||||
* @param {String} data Optional data to send.
|
||||
* @param {Function} callback Optional response callback that will be called
|
||||
* with one data argument. When the request cannot be handled, the callback
|
||||
* is immediately invoked with no arguments.
|
||||
*/
|
||||
request: function(action, data, callback) {
|
||||
var message = {
|
||||
action: action,
|
||||
data: data
|
||||
};
|
||||
if (!chrome.runtime) {
|
||||
console.error('chrome.runtime is undefined.');
|
||||
if (callback) callback();
|
||||
} else if (callback) {
|
||||
chrome.runtime.sendMessage(message, callback);
|
||||
} else {
|
||||
chrome.runtime.sendMessage(message);
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
|
@ -79,6 +79,10 @@ var mozL10n = document.mozL10n || document.webL10n;
|
|||
//#include firefoxcom.js
|
||||
//#endif
|
||||
|
||||
//#if CHROME
|
||||
//#include chromecom.js
|
||||
//#endif
|
||||
|
||||
var cache = new Cache(CACHE_SIZE);
|
||||
var currentPageNumber = 1;
|
||||
|
||||
|
@ -1770,9 +1774,39 @@ document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
|
|||
//return;
|
||||
//#endif
|
||||
|
||||
//#if !B2G
|
||||
//#if !B2G && !CHROME
|
||||
PDFView.open(file, 0);
|
||||
//#endif
|
||||
|
||||
//#if CHROME
|
||||
//ChromeCom.request('getPDFStream', file, function(response) {
|
||||
// if (response) {
|
||||
// // We will only get a response when the streamsPrivate API is available.
|
||||
//
|
||||
// var isFTPFile = /^ftp:/i.test(file);
|
||||
// var streamUrl = response.streamUrl;
|
||||
// if (streamUrl) {
|
||||
// console.log('Found data stream for ' + file);
|
||||
// // The blob stream can be used only once, so disable range requests.
|
||||
// PDFJS.disableRange = true;
|
||||
// PDFView.open(streamUrl, 0);
|
||||
// PDFView.setTitleUsingUrl(file);
|
||||
// return;
|
||||
// }
|
||||
// if (isFTPFile) {
|
||||
// // Stream not found, and it's loaded from FTP. Reload the page, because
|
||||
// // it is not possible to get resources over ftp using XMLHttpRequest.
|
||||
// // NOTE: This will not lead to an infinite redirect loop, because
|
||||
// // if the file exists, then the streamsPrivate API will capture the
|
||||
// // stream and send back the response. If the stream does not exist, then
|
||||
// // a "Webpage not available" error will be shown (not the PDF Viewer).
|
||||
// location.replace(file);
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
// PDFView.open(file, 0);
|
||||
//});
|
||||
//#endif
|
||||
}, true);
|
||||
|
||||
function updateViewarea() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue