content-bootstrap.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global singlefile, frameTree, browser, window, addEventListener, removeEventListener, document, location, docHelper, setTimeout */
  21. this.singlefile.bootstrap = this.singlefile.bootstrap || (async () => {
  22. let unloadListenerAdded, options, autoSaveEnabled, autoSaveTimeout, autoSavingPage;
  23. browser.runtime.sendMessage({ initAutoSave: true }).then(message => {
  24. options = message.options;
  25. autoSaveEnabled = message.autoSaveEnabled;
  26. refresh();
  27. });
  28. browser.runtime.onMessage.addListener(message => onMessage(message));
  29. browser.runtime.sendMessage({ loadURL: true });
  30. return {};
  31. async function onMessage(message) {
  32. if (message.autoSavePage) {
  33. autoSavingPage = false;
  34. singlefile.pageAutoSaved = false;
  35. options = message.options;
  36. await autoSavePage();
  37. if (options.autoSaveRepeat) {
  38. setTimeout(() => onMessage(message), options.autoSaveRepeatDelay * 1000);
  39. }
  40. }
  41. if (message.initAutoSave) {
  42. options = message.options;
  43. autoSaveEnabled = message.autoSaveEnabled;
  44. refresh();
  45. }
  46. }
  47. async function autoSavePage() {
  48. if ((!autoSavingPage || autoSaveTimeout) && !singlefile.pageAutoSaved) {
  49. autoSavingPage = true;
  50. if (options.autoSaveDelay && !autoSaveTimeout) {
  51. autoSaveTimeout = setTimeout(() => {
  52. autoSavePage();
  53. }, options.autoSaveDelay * 1000);
  54. } else {
  55. const docData = docHelper.preProcessDoc(document, window, options);
  56. let framesData = [];
  57. autoSaveTimeout = null;
  58. if (!options.removeFrames && this.frameTree) {
  59. framesData = await frameTree.getAsync(options);
  60. }
  61. browser.runtime.sendMessage({
  62. autoSaveContent: true,
  63. content: docHelper.serialize(document, false),
  64. canvasData: docData.canvasData,
  65. fontsData: docData.fontsData,
  66. stylesheetContents: docData.stylesheetContents,
  67. imageData: docData.imageData,
  68. postersData: docData.postersData,
  69. usedFonts: docData.usedFonts,
  70. shadowRootContents: docData.shadowRootContents,
  71. referrer: docData.referrer,
  72. framesData,
  73. url: location.href
  74. });
  75. docHelper.postProcessDoc(document, options);
  76. singlefile.pageAutoSaved = true;
  77. autoSavingPage = false;
  78. }
  79. }
  80. }
  81. async function refresh() {
  82. if (autoSaveEnabled && options && (options.autoSaveUnload || options.autoSaveLoadOrUnload)) {
  83. if (!unloadListenerAdded) {
  84. addEventListener("unload", onUnload);
  85. addEventListener("single-file-push-state", onUnload);
  86. unloadListenerAdded = true;
  87. }
  88. } else {
  89. removeEventListener("unload", onUnload);
  90. removeEventListener("single-file-push-state", onUnload);
  91. unloadListenerAdded = false;
  92. }
  93. }
  94. function onUnload() {
  95. if (!singlefile.pageAutoSaved || options.autoSaveUnload) {
  96. const docData = docHelper.preProcessDoc(document, window, options);
  97. if (this.frameTree && !options.removeFrames) {
  98. browser.runtime.sendMessage({
  99. autoSaveContent: true,
  100. content: docHelper.serialize(document),
  101. canvasData: docData.canvasData,
  102. fontsData: docData.fontsData,
  103. stylesheetContents: docData.stylesheetContents,
  104. imageData: docData.imageData,
  105. postersData: docData.postersData,
  106. usedFonts: docData.usedFonts,
  107. shadowRootContents: docData.shadowRootContents,
  108. referrer: docData.referrer,
  109. framesData: frameTree.getSync(options),
  110. url: location.href
  111. });
  112. }
  113. }
  114. }
  115. })();