mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[CRX] Show nicely formatted URL in omnibox
Before commit: chrome-extension://EXTENSIONID/content/web/viewer.html?file=http%3A%2F%2Fexample.com%2Ffile.pdf After commit: chrome-extension://EXTENSIONID/http://example/file.pdf Technical details: - The extension's background page uses the webRequest API to intercept requests for <extension host>/<real path to pdf>, and redirect it to the viewer's URL. - viewer.js uses history.replaceState to rewrite the URL, so that it's easier for users to recognize and copy-paste URLs. - The fake paths /http:, /https:, /file:, etc. have been added to the web_accessible_resources section of the manifest file, in order to avoid seeing chrome-extension://invalid/ instead of the actual URL when using history back/forward to navigate from/to the PDF viewer. - Since the relative path resolving doesn't work because relative URLs are inaccurate, a <base> tag has been added. This method has already been proven to work in the Firefox add-on. Notes: - This commit has been cherry-picked from crx-using-streams-api. - Need to merge https://github.com/mozilla/pdf.js/pull/3582 to deal with a bug in Chrome <=30 - In Chrome, getting the contents of a FTP file is not possible, so there's no support for FTP files, even though the extension router recognizes the ftp: scheme.
This commit is contained in:
parent
e9cb91de39
commit
83b780af81
6 changed files with 100 additions and 7 deletions
5
web/viewer-snippet-chrome-extension.html
Normal file
5
web/viewer-snippet-chrome-extension.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
<!-- This snippet is used in the Chrome extension, see Makefile -->
|
||||
<base href="/content/web/">
|
||||
<link rel="resource" type="application/l10n" href="locale/locale.properties">
|
||||
<script type="text/javascript" src="l10n.js"></script>
|
||||
<script type="text/javascript" src="../build/pdf.js"></script>
|
|
@ -25,6 +25,9 @@ limitations under the License.
|
|||
|
||||
<!--#if FIREFOX || MOZCENTRAL-->
|
||||
<!--#include viewer-snippet-firefox-extension.html-->
|
||||
<!--#endif-->
|
||||
<!--#if CHROME-->
|
||||
<!--#include viewer-snippet-chrome-extension.html-->
|
||||
<!--#endif-->
|
||||
|
||||
<link rel="stylesheet" href="viewer.css"/>
|
||||
|
@ -53,7 +56,7 @@ limitations under the License.
|
|||
<script type="text/javascript">PDFJS.workerSrc = '../src/worker_loader.js';</script>
|
||||
<!--#endif-->
|
||||
|
||||
<!--#if GENERIC || CHROME-->
|
||||
<!--#if GENERIC -->
|
||||
<!--#include viewer-snippet.html-->
|
||||
<!--#endif-->
|
||||
|
||||
|
|
|
@ -670,15 +670,19 @@ var PDFView = {
|
|||
},
|
||||
|
||||
/**
|
||||
* For the firefox extension we prefix the full url on anchor links so they
|
||||
* don't come up as resource:// urls and so open in new tab/window works.
|
||||
* @param {String} anchor The anchor hash include the #.
|
||||
* Prefix the full url on anchor links to make sure that links are resolved
|
||||
* relative to the current URL instead of the one defined in <base href>.
|
||||
* @param {String} anchor The anchor hash, including the #.
|
||||
*/
|
||||
getAnchorUrl: function getAnchorUrl(anchor) {
|
||||
//#if !(FIREFOX || MOZCENTRAL)
|
||||
//#if !(FIREFOX || MOZCENTRAL || CHROME)
|
||||
return anchor;
|
||||
//#else
|
||||
//#if CHROME
|
||||
// return location.href.split('#')[0] + anchor;
|
||||
//#else
|
||||
// return this.url.split('#')[0] + anchor;
|
||||
//#endif
|
||||
//#endif
|
||||
},
|
||||
|
||||
|
@ -1459,15 +1463,36 @@ var DocumentOutlineView = function documentOutlineView(outline) {
|
|||
}
|
||||
};
|
||||
|
||||
//#if CHROME
|
||||
//(function rewriteUrlClosure() {
|
||||
// // Run this code outside DOMContentLoaded to make sure that the URL
|
||||
// // is rewritten as soon as possible.
|
||||
// if (location.origin + '/' !== chrome.extension.getURL('/')) {
|
||||
// DEFAULT_URL = window.location.href.split('#')[0];
|
||||
// } else {
|
||||
// var params = PDFView.parseQueryString(document.location.search.slice(1));
|
||||
// DEFAULT_URL = params.file || DEFAULT_URL;
|
||||
//
|
||||
// // Example: chrome-extension://.../http://example.com/file.pdf
|
||||
// var humanReadableUrl = '/' + DEFAULT_URL + location.hash;
|
||||
// history.replaceState(history.state, '', humanReadableUrl);
|
||||
// }
|
||||
//})();
|
||||
//#endif
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
|
||||
PDFView.initialize();
|
||||
|
||||
//#if !(FIREFOX || MOZCENTRAL)
|
||||
//#if !(FIREFOX || MOZCENTRAL || CHROME)
|
||||
var params = PDFView.parseQueryString(document.location.search.substring(1));
|
||||
var file = params.file || DEFAULT_URL;
|
||||
//#else
|
||||
//#if CHROME
|
||||
//var file = DEFAULT_URL;
|
||||
//#else
|
||||
//var file = window.location.href.split('#')[0];
|
||||
//#endif
|
||||
//#endif
|
||||
|
||||
//#if CHROME
|
||||
//if (location.protocol !== 'chrome-extension:') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue