index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global globalThis */
  24. import * as processors from "./processors/index.js";
  25. import * as vendor from "./vendor/index.js";
  26. import * as modules from "./modules/index.js";
  27. import * as core from "./single-file-core.js";
  28. import * as helper from "./single-file-helper.js";
  29. import * as util from "./single-file-util.js";
  30. let SingleFile;
  31. export {
  32. init,
  33. getPageData,
  34. processors,
  35. vendor,
  36. modules,
  37. helper,
  38. SingleFile
  39. };
  40. function init(initOptions) {
  41. SingleFile = core.getClass(util.getInstance(initOptions), vendor.cssTree);
  42. }
  43. async function getPageData(options = {}, initOptions, doc = globalThis.document, win = globalThis) {
  44. const frames = processors.frameTree;
  45. let framesSessionId;
  46. init(initOptions);
  47. if (doc && win) {
  48. helper.initDoc(doc);
  49. const preInitializationPromises = [];
  50. if (!options.saveRawPage) {
  51. if (!options.removeFrames && frames && globalThis.frames && globalThis.frames.length) {
  52. let frameTreePromise;
  53. if (options.loadDeferredImages) {
  54. frameTreePromise = new Promise(resolve => globalThis.setTimeout(() => resolve(frames.getAsync(options)), options.loadDeferredImagesMaxIdleTime - frames.TIMEOUT_INIT_REQUEST_MESSAGE));
  55. } else {
  56. frameTreePromise = frames.getAsync(options);
  57. }
  58. preInitializationPromises.push(frameTreePromise);
  59. }
  60. if (options.loadDeferredImages) {
  61. preInitializationPromises.push(processors.lazy.process(options));
  62. }
  63. }
  64. [options.frames] = await Promise.all(preInitializationPromises);
  65. framesSessionId = options.frames && options.frames.sessionId;
  66. }
  67. options.doc = doc;
  68. options.win = win;
  69. options.insertCanonicalLink = true;
  70. options.onprogress = event => {
  71. if (event.type === event.RESOURCES_INITIALIZED && doc && win && options.loadDeferredImages) {
  72. processors.lazy.resetZoomLevel(options);
  73. }
  74. };
  75. const processor = new SingleFile(options);
  76. await processor.run();
  77. if (framesSessionId) {
  78. frames.cleanup(framesSessionId);
  79. }
  80. return await processor.getPageData();
  81. }