forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 mozilla#3017 (comment)
- Loading branch information
Showing
7 changed files
with
314 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
parsererror { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ | ||
/* | ||
Copyright 2012 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 VIEWER_URL = chrome.extension.getURL('content/web/viewer.html'); | ||
var BASE_URL = VIEWER_URL.replace(/[^\/]+$/, ''); | ||
|
||
function getViewerURL(pdf_url) { | ||
return VIEWER_URL + '?file=' + encodeURIComponent(pdf_url); | ||
} | ||
|
||
function showViewer(url) { | ||
// Cancel page load and empty document. | ||
window.stop(); | ||
document.body.textContent = ''; | ||
|
||
replaceDocumentWithViewer(url); | ||
} | ||
function makeLinksAbsolute(doc) { | ||
normalize('href', 'link[href]'); | ||
normalize('src', 'style[src],script[src]'); | ||
|
||
function normalize(attribute, selector) { | ||
var nodes = doc.querySelectorAll(selector); | ||
for (var i=0; i<nodes.length; ++i) { | ||
var node = nodes[i]; | ||
var newAttribute = makeAbsolute(node.getAttribute(attribute)); | ||
node.setAttribute(attribute, newAttribute); | ||
} | ||
} | ||
function makeAbsolute(url) { | ||
if (url.indexOf('://') !== -1) return url; | ||
return BASE_URL + url; | ||
} | ||
} | ||
function replaceDocumentWithViewer(url) { | ||
var x = new XMLHttpRequest(); | ||
x.open('GET', VIEWER_URL); | ||
x.responseType = 'document'; | ||
x.onload = function() { | ||
// Resolve all relative URLs | ||
makeLinksAbsolute(x.response); | ||
|
||
// Remove all <script> elements (added back later). | ||
// I assumed that no inline script tags exist. | ||
var scripts = []; | ||
while (x.response.scripts.length) { | ||
var script = x.response.scripts[0]; | ||
var newScript = document.createElement('script'); | ||
newScript.onload = loadNextScript; | ||
newScript.src = script.src; | ||
script.parentNode.removeChild(script); | ||
scripts.push(newScript); | ||
} | ||
|
||
// Replace document with viewer | ||
var docEl = document.adoptNode(x.response.documentElement); | ||
document.replaceChild(docEl, document.documentElement); | ||
// Force Chrome to render content | ||
// (without this line, the layout is broken and querySelector | ||
// fails to find elements, even when they appear in the doc) | ||
document.body.innerHTML += ''; | ||
|
||
// Load all scripts | ||
loadNextScript(); | ||
|
||
function loadNextScript() { | ||
if (scripts.length > 0) | ||
document.head.appendChild(scripts.shift()); | ||
else | ||
renderPDF(url); | ||
} | ||
}; | ||
x.send(); | ||
} | ||
function renderPDF(url) { | ||
var args = { | ||
BASE_URL: BASE_URL, | ||
pdf_url: url | ||
}; | ||
// The following technique is explained at | ||
// http://stackoverflow.com/a/9517879/938089 | ||
var script = document.createElement('script'); | ||
script.textContent = | ||
'(function(args) {' + | ||
' PDFJS.workerSrc = args.BASE_URL + PDFJS.workerSrc;' + | ||
' window.DEFAULT_URL = args.pdf_url;' + | ||
' window.IMAGE_DIR = args.BASE_URL + window.IMAGE_DIR;' + | ||
'})(' + JSON.stringify(args) + ');'; | ||
document.head.appendChild(script); | ||
|
||
// Trigger domready | ||
if (document.readyState === 'complete') { | ||
var event = document.createEvent('Event'); | ||
event.initEvent('DOMContentLoaded', true, true); | ||
document.dispatchEvent(event); | ||
} | ||
} | ||
|
||
|
||
// Activate the content script only once per frame (until reload) | ||
if (!window.hasRun) { | ||
window.hasRun = true; | ||
chrome.extension.onMessage.addListener(function listener(message) { | ||
if (message && message.type === 'showPDFViewer' && | ||
message.url === location.href) { | ||
chrome.extension.onMessage.removeListener(listener); | ||
showViewer(message.url); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ | ||
/* | ||
Copyright 2012 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, isPdfDownloadable */ | ||
|
||
'use strict'; | ||
|
||
// The onHeadersReceived event is not generated for local resources. | ||
// Fortunately, local PDF files will have the .pdf extension, so there's | ||
// no need to detect the Content-Type | ||
// Unfortunately, the omnibox won't show the URL. | ||
// Unfortunately, this method will not work for pages in incognito mode, | ||
// unless "incognito":"split" is used AND http:/crbug.com/224094 is fixed. | ||
|
||
// Keeping track of incognito tab IDs will become obsolete when | ||
// "incognito":"split" can be used. | ||
var incognitoTabIds = []; | ||
chrome.windows.getAll({ populate: true }, function(windows) { | ||
windows.forEach(function(win) { | ||
if (win.incognito) { | ||
win.tabs.forEach(function(tab) { | ||
incognitoTabIds.push(tab.id); | ||
}); | ||
} | ||
}); | ||
}); | ||
chrome.tabs.onCreated.addListener(function(tab) { | ||
if (tab.incognito) incognitoTabIds.push(tab.id); | ||
}); | ||
chrome.tabs.onRemoved.addListener(function(tabId) { | ||
var index = incognitoTabIds.indexOf(tabId); | ||
if (index !== -1) incognitoTabIds.splice(index, 1); | ||
}); | ||
|
||
chrome.webRequest.onBeforeRequest.addListener( | ||
function(details) { | ||
if (isPdfDownloadable(details)) // Defined in pdfHandler.js | ||
return; | ||
|
||
if (incognitoTabIds.indexOf(details.tabId) !== -1) | ||
return; // Doesn't work in incognito mode, so don't redirect. | ||
|
||
var viewerPage = 'content/web/viewer.html'; | ||
var url = chrome.extension.getURL(viewerPage) + | ||
'?file=' + encodeURIComponent(details.url); | ||
return { redirectUrl: url }; | ||
}, | ||
{ | ||
urls: [ | ||
'file://*/*.pdf', | ||
'file://*/*.PDF' | ||
], | ||
types: ['main_frame', 'sub_frame'] | ||
}, | ||
['blocking']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters