1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Highly improved Chrome extension

Full list feature changes in this commit:
- Support for iframes
- Switched to content-type (MIME) detection instead of hard-coding a
  case-sensitive check for the .PDF extension
- The PDF's original URL is visible in the omnibox
- Support for incognito mode

Note: PDF viewer is disabled for the file:// + incognito
combination, because it's currently impossible to get the combination
to work.

See https://github.com/mozilla/pdf.js/pull/3017#issuecomment-15693432
This commit is contained in:
Rob Wu 2013-04-04 00:28:45 +02:00
parent 9c76ed0a35
commit e181a3c902
7 changed files with 314 additions and 21 deletions

View file

@ -23,25 +23,94 @@ function isPdfDownloadable(details) {
return details.url.indexOf('pdfjs.action=download') >= 0;
}
chrome.webRequest.onBeforeRequest.addListener(
function insertPDFJSForTab(tabId, url) {
chrome.tabs.executeScript(tabId, {
file: 'insertviewer.js',
allFrames: true,
runAt: 'document_start'
}, function() {
chrome.tabs.sendMessage(tabId, {
type: 'showPDFViewer',
url: url
});
});
}
function activatePDFJSForTab(tabId, url) {
chrome.tabs.onUpdated.addListener(function listener(_tabId) {
if (tabId === _tabId) {
insertPDFJSForTab(tabId, url);
chrome.tabs.onUpdated.removeListener(listener);
}
});
}
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
if (isPdfDownloadable(details))
// Check if the response is a PDF file
var isPDF = false;
var headers = details.responseHeaders;
var header, i;
var cdHeader;
if (!headers)
return;
for (i=0; i<headers.length; ++i) {
header = headers[i];
if (header.name.toLowerCase() == 'content-type') {
var headerValue = header.value.toLowerCase().split(';',1)[0].trim();
isPDF = headerValue === 'application/pdf' ||
headerValue === 'application/octet-stream' &&
details.url.toLowerCase().indexOf('.pdf') > 0;
break;
}
}
if (!isPDF)
return;
var viewerPage = 'content/web/viewer.html';
var url = chrome.extension.getURL(viewerPage) +
'?file=' + encodeURIComponent(details.url);
return { redirectUrl: url };
if (isPdfDownloadable(details)) {
// Force download by ensuring that Content-Disposition: attachment is set
if (!cdHeader) {
for (; i<headers.length; ++i) {
header = headers[i];
if (header.name.toLowerCase() == 'content-disposition') {
cdHeader = header;
break;
}
}
}
if (!cdHeader) {
cdHeader = {name: 'Content-Disposition', value: ''};
headers.push(cdHeader);
}
if (cdHeader.value.toLowerCase().indexOf('attachment') === -1) {
cdHeader.value = 'attachment' + cdHeader.value.replace(/^[^;]+/i, '');
return {
responseHeaders: headers
};
}
return;
}
// Replace frame's content with the PDF viewer
// This approach maintains the friendly URL in the
// location bar
activatePDFJSForTab(details.tabId, details.url);
return {
responseHeaders: [
// Set Cache-Control header to avoid downloading a file twice
{name:'Cache-Control',value:'max-age=600'},
// Temporary render response as XHTML.
// Since PDFs are never valid XHTML, the garbage is not going to be
// rendered. insertviewer.js will quickly replace the document with
// the PDF.js viewer.
{name:'Content-Type',value:'application/xhtml+xml; charset=US-ASCII'},
]
};
},
{
urls: [
'http://*/*.pdf',
'https://*/*.pdf',
'file://*/*.pdf',
'http://*/*.PDF',
'https://*/*.PDF',
'file://*/*.PDF'
'<all_urls>'
],
types: ['main_frame']
types: ['main_frame', 'sub_frame']
},
['blocking']);
['blocking','responseHeaders']);