content-bootstrap.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2010-2019 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 browser, window, addEventListener, removeEventListener, document, location, setTimeout */
  24. this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.core.content.bootstrap || (async () => {
  25. const singlefile = this.singlefile;
  26. const PUSH_STATE_NOTIFICATION_EVENT_NAME = "single-file-push-state";
  27. let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage, pageAutoSaved;
  28. singlefile.extension.core.content.updatedResources = {};
  29. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  30. options = message.options;
  31. autoSaveEnabled = message.autoSaveEnabled;
  32. refresh();
  33. });
  34. browser.runtime.onMessage.addListener(message => { onMessage(message); });
  35. browser.runtime.sendMessage({ method: "ui.processInit" });
  36. addEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, () => browser.runtime.sendMessage({ method: "ui.processInit" }));
  37. return {};
  38. async function onMessage(message) {
  39. if (autoSaveEnabled && message.method == "content.autosave") {
  40. options = message.options;
  41. await autoSavePage();
  42. if (options.autoSaveRepeat) {
  43. setTimeout(() => {
  44. if (autoSaveEnabled && !autoSavingPage) {
  45. pageAutoSaved = false;
  46. options.autoSaveDelay = 0;
  47. onMessage(message);
  48. }
  49. }, options.autoSaveRepeatDelay * 1000);
  50. }
  51. }
  52. if (message.method == "content.init") {
  53. options = message.options;
  54. autoSaveEnabled = message.autoSaveEnabled;
  55. refresh();
  56. }
  57. if (message.method == "devtools.resourceCommitted") {
  58. singlefile.extension.core.content.updatedResources[message.url] = { content: message.content, type: message.type, encoding: message.encoding };
  59. }
  60. }
  61. async function autoSavePage() {
  62. const helper = singlefile.lib.helper;
  63. if ((!autoSavingPage || autoSaveTimeout) && !pageAutoSaved) {
  64. autoSavingPage = true;
  65. if (options.autoSaveDelay && !autoSaveTimeout) {
  66. await new Promise(resolve => autoSaveTimeout = setTimeout(resolve, options.autoSaveDelay * 1000));
  67. await autoSavePage();
  68. } else {
  69. let frames = [];
  70. autoSaveTimeout = null;
  71. if (!options.removeFrames && singlefile.lib.processors.frameTree.content.frames && window.frames && window.frames.length) {
  72. frames = await singlefile.lib.processors.frameTree.content.frames.getAsync(options);
  73. }
  74. if (options.userScriptEnabled && helper.waitForUserScript) {
  75. await helper.waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  76. }
  77. const docData = helper.preProcessDoc(document, window, options);
  78. savePage(docData, frames);
  79. helper.postProcessDoc(document, docData.markedElements);
  80. if (options.userScriptEnabled && helper.waitForUserScript) {
  81. await helper.waitForUserScript(helper.ON_AFTER_CAPTURE_EVENT_NAME);
  82. }
  83. pageAutoSaved = true;
  84. autoSavingPage = false;
  85. }
  86. }
  87. }
  88. async function refresh() {
  89. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  90. if (!unloadListenerAdded) {
  91. addEventListener("unload", onUnload);
  92. addEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, onUnload);
  93. unloadListenerAdded = true;
  94. }
  95. } else {
  96. removeEventListener("unload", onUnload);
  97. removeEventListener(PUSH_STATE_NOTIFICATION_EVENT_NAME, onUnload);
  98. unloadListenerAdded = false;
  99. }
  100. }
  101. function onUnload() {
  102. const helper = singlefile.lib.helper;
  103. if (!pageAutoSaved || options.autoSaveUnload) {
  104. let frames = [];
  105. if (!options.removeFrames && singlefile.lib.processors.frameTree.content.frames && window.frames && window.frames.length) {
  106. frames = singlefile.lib.processors.frameTree.content.frames.getSync(options);
  107. }
  108. if (options.userScriptEnabled && helper.waitForUserScript) {
  109. helper.waitForUserScript(helper.ON_BEFORE_CAPTURE_EVENT_NAME);
  110. }
  111. const docData = helper.preProcessDoc(document, window, options);
  112. savePage(docData, frames);
  113. }
  114. }
  115. function savePage(docData, frames) {
  116. const helper = singlefile.lib.helper;
  117. const updatedResources = singlefile.extension.core.content.updatedResources;
  118. Object.keys(updatedResources).forEach(url => updatedResources[url].retrieved = false);
  119. browser.runtime.sendMessage({
  120. method: "autosave.save",
  121. content: helper.serialize(document),
  122. canvases: docData.canvases,
  123. fonts: docData.fonts,
  124. stylesheets: docData.stylesheets,
  125. images: docData.images,
  126. posters: docData.posters,
  127. usedFonts: docData.usedFonts,
  128. shadowRoots: docData.shadowRoots,
  129. imports: docData.imports,
  130. referrer: docData.referrer,
  131. frames: frames,
  132. url: location.href,
  133. updatedResources
  134. });
  135. }
  136. })();