Repository: mozilla/pdf.js
File: web/chromecom.js (JavaScript)

Code before:
/* 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.
 */
/* globals chrome */

import { AppOptions } from "./app_options.js";
import { BaseExternalServices } from "./external_services.js";
import { BasePreferences } from "./preferences.js";
import { GenericL10n } from "./genericl10n.js";
import { GenericScripting } from "./generic_scripting.js";

// These strings are from chrome/app/resources/generated_resources_*.xtb.
import i18nFileAccessLabels from "./chrome-i18n-allow-access-to-file-urls.json" with { type: "json" };
import { SignatureStorage } from "./generic_signature_storage.js";

if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) {
  throw new Error(
    'Module "pdfjs-web/chromecom" shall not be used outside CHROME build.'
  );
}

(function rewriteUrlClosure() {
  // Run this code outside DOMContentLoaded to make sure that the URL
  // is rewritten as soon as possible.
  const queryString = document.location.search.slice(1);
  const m = /(^|&)file=([^&]*)/.exec(queryString);
  let defaultUrl = m ? decodeURIComponent(m[2]) : "";
  if (!defaultUrl && queryString.startsWith("DNR:")) {
    // Redirected via DNR, see registerPdfRedirectRule in pdfHandler.js.
    defaultUrl = queryString.slice(4);
  }

  // Example: chrome-extension://.../http://example.com/file.pdf
  const humanReadableUrl = "/" + defaultUrl + location.hash;
  history.replaceState(history.state, "", humanReadableUrl);

  AppOptions.set("defaultUrl", defaultUrl);
})();


Review comment (refactor):
Nit: Please move this to list of "regular" imports above, since the `json` file is a special-case.

Code after:
/* 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.
 */
/* globals chrome */

import { AppOptions } from "./app_options.js";
import { BaseExternalServices } from "./external_services.js";
import { BasePreferences } from "./preferences.js";
import { GenericL10n } from "./genericl10n.js";
import { GenericScripting } from "./generic_scripting.js";
import { SignatureStorage } from "./generic_signature_storage.js";

// These strings are from chrome/app/resources/generated_resources_*.xtb.
// eslint-disable-next-line sort-imports
import i18nFileAccessLabels from "./chrome-i18n-allow-access-to-file-urls.json" with { type: "json" };

if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) {
  throw new Error(
    'Module "pdfjs-web/chromecom" shall not be used outside CHROME build.'
  );
}

(function rewriteUrlClosure() {
  // Run this code outside DOMContentLoaded to make sure that the URL
  // is rewritten as soon as possible.
  const queryString = document.location.search.slice(1);
  const m = /(^|&)file=([^&]*)/.exec(queryString);
  let defaultUrl = m ? decodeURIComponent(m[2]) : "";
  if (!defaultUrl && queryString.startsWith("DNR:")) {
    // Redirected via DNR, see registerPdfRedirectRule in pdfHandler.js.
    defaultUrl = queryString.slice(4);
  }

  // Example: chrome-extension://.../http://example.com/file.pdf
  const humanReadableUrl = "/" + defaultUrl + location.hash;
  history.replaceState(history.state, "", humanReadableUrl);

  AppOptions.set("defaultUrl", defaultUrl);
})();