content-bootstrap.js 5.3 KB

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