[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.
2013-08-15 22:47:30 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
2014-03-09 23:14:01 +01: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.
2013-08-15 22:47:30 +02:00
|
|
|
(function ExtensionRouterClosure() {
|
2024-09-01 21:38:01 +02:00
|
|
|
var VIEWER_URL = chrome.runtime.getURL("content/web/viewer.html");
|
|
|
|
var CRX_BASE_URL = chrome.runtime.getURL("/");
|
[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.
2013-08-15 22:47:30 +02:00
|
|
|
|
2014-01-25 22:29:33 +01:00
|
|
|
var schemes = [
|
|
|
|
"http",
|
|
|
|
"https",
|
|
|
|
"file",
|
|
|
|
"chrome-extension",
|
2017-02-04 01:20:17 +01:00
|
|
|
"blob",
|
|
|
|
"data",
|
2014-01-25 22:29:33 +01:00
|
|
|
// Chromium OS
|
|
|
|
"filesystem",
|
|
|
|
// Chromium OS, shorthand for filesystem:<origin>/external/
|
|
|
|
"drive",
|
|
|
|
];
|
|
|
|
|
2013-12-07 12:32:08 +01:00
|
|
|
/**
|
|
|
|
* @param {string} url The URL prefixed with chrome-extension://.../
|
2019-10-12 18:14:29 +02:00
|
|
|
* @returns {string|undefined} The percent-encoded URL of the (PDF) file.
|
2013-12-07 12:32:08 +01:00
|
|
|
*/
|
|
|
|
function parseExtensionURL(url) {
|
|
|
|
url = url.substring(CRX_BASE_URL.length);
|
2020-06-29 17:15:14 +02:00
|
|
|
// Find the (url-encoded) colon and verify that the scheme is allowed.
|
2014-01-25 22:29:33 +01:00
|
|
|
var schemeIndex = url.search(/:|%3A/i);
|
|
|
|
if (schemeIndex === -1) {
|
2019-05-10 12:54:06 +02:00
|
|
|
return undefined;
|
2014-01-25 22:29:33 +01:00
|
|
|
}
|
|
|
|
var scheme = url.slice(0, schemeIndex).toLowerCase();
|
2018-02-10 17:06:03 +01:00
|
|
|
if (schemes.includes(scheme)) {
|
2024-02-02 13:02:31 +01:00
|
|
|
url = url.split("#", 1)[0];
|
2014-01-25 22:29:33 +01:00
|
|
|
if (url.charAt(schemeIndex) === ":") {
|
2013-12-07 12:32:08 +01:00
|
|
|
url = encodeURIComponent(url);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2019-05-10 12:54:06 +02:00
|
|
|
return undefined;
|
2013-12-07 12:32:08 +01:00
|
|
|
}
|
|
|
|
|
2024-09-02 01:34:25 +02:00
|
|
|
function resolveViewerURL(originalUrl) {
|
|
|
|
if (originalUrl.startsWith(CRX_BASE_URL)) {
|
[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.
2013-08-15 22:47:30 +02:00
|
|
|
// This listener converts chrome-extension://.../http://...pdf to
|
|
|
|
// chrome-extension://.../content/web/viewer.html?file=http%3A%2F%2F...pdf
|
2024-09-02 01:34:25 +02:00
|
|
|
var url = parseExtensionURL(originalUrl);
|
2013-12-07 12:32:08 +01:00
|
|
|
if (url) {
|
[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.
2013-08-15 22:47:30 +02:00
|
|
|
url = VIEWER_URL + "?file=" + url;
|
2024-09-02 01:34:25 +02:00
|
|
|
var i = originalUrl.indexOf("#");
|
2014-08-08 12:37:37 +02:00
|
|
|
if (i > 0) {
|
2024-09-02 01:34:25 +02:00
|
|
|
url += originalUrl.slice(i);
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 15:59:37 +01:00
|
|
|
}
|
2024-09-02 01:34:25 +02:00
|
|
|
return url;
|
2014-08-08 12:37:37 +02:00
|
|
|
}
|
2024-09-02 01:34:25 +02:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2013-11-22 11:44:43 +01:00
|
|
|
|
2024-09-02 01:34:25 +02:00
|
|
|
self.addEventListener("fetch", event => {
|
|
|
|
const req = event.request;
|
|
|
|
if (req.destination === "document") {
|
|
|
|
var url = resolveViewerURL(req.url);
|
|
|
|
if (url) {
|
|
|
|
console.log("Redirecting " + req.url + " to " + url);
|
|
|
|
event.respondWith(Response.redirect(url));
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 15:59:37 +01:00
|
|
|
}
|
2013-11-22 11:44:43 +01:00
|
|
|
}
|
2024-09-02 01:34:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ctrl + F5 bypasses service worker. the pretty extension URLs will fail to
|
|
|
|
// resolve in that case. Catch this and redirect to destination.
|
|
|
|
chrome.webNavigation.onErrorOccurred.addListener(
|
|
|
|
details => {
|
|
|
|
if (details.frameId !== 0) {
|
|
|
|
// Not a top-level frame. Cannot easily navigate a specific child frame.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const url = resolveViewerURL(details.url);
|
|
|
|
if (url) {
|
|
|
|
console.log(`Redirecting ${details.url} to ${url} (fallback)`);
|
|
|
|
chrome.tabs.update(details.tabId, { url });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ url: [{ urlPrefix: CRX_BASE_URL }] }
|
2013-11-22 11:44:43 +01:00
|
|
|
);
|
2024-09-02 01:34:25 +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.
2013-08-15 22:47:30 +02:00
|
|
|
console.log("Set up extension URL router.");
|
|
|
|
})();
|