generic_scripting.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  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. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { getPdfFilenameFromUrl, loadScript } from "pdfjs-lib";
  16. async function docPropertiesLookup(pdfDocument) {
  17. const url = "",
  18. baseUrl = url.split("#")[0];
  19. // eslint-disable-next-line prefer-const
  20. let { info, metadata, contentDispositionFilename, contentLength } =
  21. await pdfDocument.getMetadata();
  22. if (!contentLength) {
  23. const { length } = await pdfDocument.getDownloadInfo();
  24. contentLength = length;
  25. }
  26. return {
  27. ...info,
  28. baseURL: baseUrl,
  29. filesize: contentLength,
  30. filename: contentDispositionFilename || getPdfFilenameFromUrl(url),
  31. metadata: metadata?.getRaw(),
  32. authors: metadata?.get("dc:creator"),
  33. numPages: pdfDocument.numPages,
  34. URL: url,
  35. };
  36. }
  37. class GenericScripting {
  38. constructor(sandboxBundleSrc) {
  39. this._ready = loadScript(
  40. sandboxBundleSrc,
  41. /* removeScriptElement = */ true
  42. ).then(() => {
  43. return window.pdfjsSandbox.QuickJSSandbox();
  44. });
  45. }
  46. async createSandbox(data) {
  47. const sandbox = await this._ready;
  48. sandbox.create(data);
  49. }
  50. async dispatchEventInSandbox(event) {
  51. const sandbox = await this._ready;
  52. setTimeout(() => sandbox.dispatchEvent(event), 0);
  53. }
  54. async destroySandbox() {
  55. const sandbox = await this._ready;
  56. sandbox.nukeSandbox();
  57. }
  58. }
  59. export { docPropertiesLookup, GenericScripting };