content-bootstrap.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. browser.runtime.sendMessage({ method: "autosave.init" }).then(message => {
  28. options = message.options;
  29. autoSaveEnabled = message.autoSaveEnabled;
  30. refresh();
  31. });
  32. browser.runtime.onMessage.addListener(message => onMessage(message));
  33. browser.runtime.sendMessage({ method: "ui.loadURL" });
  34. return {};
  35. async function onMessage(message) {
  36. if (message.method == "content.autosave") {
  37. options = message.options;
  38. await autoSavePage();
  39. if (options.autoSaveRepeat) {
  40. pageAutoSaved = false;
  41. options.autoSaveDelay = 0;
  42. setTimeout(() => onMessage(message), options.autoSaveRepeatDelay * 1000);
  43. }
  44. }
  45. if (message.method == "content.init") {
  46. options = message.options;
  47. autoSaveEnabled = message.autoSaveEnabled;
  48. refresh();
  49. }
  50. }
  51. async function autoSavePage() {
  52. const helper = singlefile.lib.helper;
  53. const frames = singlefile.lib.frameTree.content.frames;
  54. if ((!autoSavingPage || autoSaveTimeout) && !pageAutoSaved) {
  55. autoSavingPage = true;
  56. if (options.autoSaveDelay && !autoSaveTimeout) {
  57. autoSaveTimeout = setTimeout(() => {
  58. autoSavePage();
  59. }, options.autoSaveDelay * 1000);
  60. } else {
  61. const docData = helper.preProcessDoc(document, window, options);
  62. let framesData = [];
  63. autoSaveTimeout = null;
  64. if (!options.removeFrames && frames) {
  65. framesData = await frames.getAsync(options);
  66. }
  67. browser.runtime.sendMessage({
  68. method: "autosave.save",
  69. content: helper.serialize(document, false),
  70. canvasData: docData.canvasData,
  71. fontsData: docData.fontsData,
  72. stylesheetsData: docData.stylesheetsData,
  73. imagesData: docData.imagesData,
  74. postersData: docData.postersData,
  75. usedFonts: docData.usedFonts,
  76. shadowRootsData: docData.shadowRootsData,
  77. referrer: docData.referrer,
  78. framesData,
  79. url: location.href
  80. });
  81. helper.postProcessDoc(document, options);
  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. const docData = helper.preProcessDoc(document, window, options);
  104. let framesData = [];
  105. if (!options.removeFrames && singlefile.lib.frameTree.content.frames) {
  106. framesData = singlefile.lib.frameTree.content.frames.getSync(options);
  107. }
  108. browser.runtime.sendMessage({
  109. method: "autosave.save",
  110. content: helper.serialize(document),
  111. canvasData: docData.canvasData,
  112. fontsData: docData.fontsData,
  113. stylesheetsData: docData.stylesheetsData,
  114. imagesData: docData.imagesData,
  115. postersData: docData.postersData,
  116. usedFonts: docData.usedFonts,
  117. shadowRootsData: docData.shadowRootsData,
  118. referrer: docData.referrer,
  119. framesData,
  120. url: location.href
  121. });
  122. }
  123. }
  124. })();