extension-router.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2013 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. "use strict";
  14. (function ExtensionRouterClosure() {
  15. var VIEWER_URL = chrome.extension.getURL("content/web/viewer.html");
  16. var CRX_BASE_URL = chrome.extension.getURL("/");
  17. var schemes = [
  18. "http",
  19. "https",
  20. "ftp",
  21. "file",
  22. "chrome-extension",
  23. "blob",
  24. "data",
  25. // Chromium OS
  26. "filesystem",
  27. // Chromium OS, shorthand for filesystem:<origin>/external/
  28. "drive",
  29. ];
  30. /**
  31. * @param {string} url The URL prefixed with chrome-extension://.../
  32. * @returns {string|undefined} The percent-encoded URL of the (PDF) file.
  33. */
  34. function parseExtensionURL(url) {
  35. url = url.substring(CRX_BASE_URL.length);
  36. // Find the (url-encoded) colon and verify that the scheme is allowed.
  37. var schemeIndex = url.search(/:|%3A/i);
  38. if (schemeIndex === -1) {
  39. return undefined;
  40. }
  41. var scheme = url.slice(0, schemeIndex).toLowerCase();
  42. if (schemes.includes(scheme)) {
  43. url = url.split("#")[0];
  44. if (url.charAt(schemeIndex) === ":") {
  45. url = encodeURIComponent(url);
  46. }
  47. return url;
  48. }
  49. return undefined;
  50. }
  51. // TODO(rob): Use declarativeWebRequest once declared URL-encoding is
  52. // supported, see http://crbug.com/273589
  53. // (or rewrite the query string parser in viewer.js to get it to
  54. // recognize the non-URL-encoded PDF URL.)
  55. chrome.webRequest.onBeforeRequest.addListener(
  56. function (details) {
  57. // This listener converts chrome-extension://.../http://...pdf to
  58. // chrome-extension://.../content/web/viewer.html?file=http%3A%2F%2F...pdf
  59. var url = parseExtensionURL(details.url);
  60. if (url) {
  61. url = VIEWER_URL + "?file=" + url;
  62. var i = details.url.indexOf("#");
  63. if (i > 0) {
  64. url += details.url.slice(i);
  65. }
  66. console.log("Redirecting " + details.url + " to " + url);
  67. return { redirectUrl: url };
  68. }
  69. return undefined;
  70. },
  71. {
  72. types: ["main_frame", "sub_frame"],
  73. urls: schemes.map(function (scheme) {
  74. // Format: "chrome-extension://[EXTENSIONID]/<scheme>*"
  75. return CRX_BASE_URL + scheme + "*";
  76. }),
  77. },
  78. ["blocking"]
  79. );
  80. // When session restore is used, viewer pages may be loaded before the
  81. // webRequest event listener is attached (= page not found).
  82. // Or the extension could have been crashed (OOM), leaving a sad tab behind.
  83. // Reload these tabs.
  84. chrome.tabs.query(
  85. {
  86. url: CRX_BASE_URL + "*:*",
  87. },
  88. function (tabsFromLastSession) {
  89. for (const { id } of tabsFromLastSession) {
  90. chrome.tabs.reload(id);
  91. }
  92. }
  93. );
  94. console.log("Set up extension URL router.");
  95. Object.keys(localStorage).forEach(function (key) {
  96. // The localStorage item is set upon unload by chromecom.js.
  97. var parsedKey = /^unload-(\d+)-(true|false)-(.+)/.exec(key);
  98. if (parsedKey) {
  99. var timeStart = parseInt(parsedKey[1], 10);
  100. var isHidden = parsedKey[2] === "true";
  101. var url = parsedKey[3];
  102. if (Date.now() - timeStart < 3000) {
  103. // Is it a new item (younger than 3 seconds)? Assume that the extension
  104. // just reloaded, so restore the tab (work-around for crbug.com/511670).
  105. chrome.tabs.create({
  106. url:
  107. chrome.runtime.getURL("restoretab.html") +
  108. "?" +
  109. encodeURIComponent(url) +
  110. "#" +
  111. encodeURIComponent(localStorage.getItem(key)),
  112. active: !isHidden,
  113. });
  114. }
  115. localStorage.removeItem(key);
  116. }
  117. });
  118. })();