autosave.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, SingleFileBrowser, URL, Blob */
  21. singlefile.autosave = (() => {
  22. return {
  23. onMessage,
  24. onMessageExternal,
  25. onTabUpdated,
  26. isEnabled,
  27. refreshTabs
  28. };
  29. async function onMessage(message, sender) {
  30. if (message.initAutoSave) {
  31. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(sender.tab.url, true), isEnabled(sender.tab)]);
  32. return { options, autoSaveEnabled };
  33. }
  34. if (message.autoSaveContent) {
  35. return saveContent(message, sender.tab);
  36. }
  37. }
  38. async function onMessageExternal(message, currentTab) {
  39. if (message.method == "enableAutoSave") {
  40. const tabsData = await singlefile.tabsData.get(currentTab.id);
  41. tabsData[currentTab.id].autoSave = message.enabled;
  42. await singlefile.tabsData.set(tabsData);
  43. singlefile.ui.refresh(currentTab);
  44. }
  45. if (message.method == "isAutoSaveEnabled") {
  46. return await isEnabled(currentTab);
  47. }
  48. }
  49. async function onTabUpdated(tabId, changeInfo, tab) {
  50. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tab.url, true), isEnabled(tab)]);
  51. if (options && ((options.autoSaveLoad || options.autoSaveLoadOrUnload) && autoSaveEnabled)) {
  52. if (changeInfo.status == "complete") {
  53. singlefile.core.saveTab(tab, { autoSave: true });
  54. }
  55. }
  56. }
  57. async function isEnabled(tab) {
  58. const [tabsData, rule] = await Promise.all([singlefile.tabsData.get(), singlefile.config.getRule(tab.url)]);
  59. return singlefile.util.isAllowedURL(tab.url) && Boolean(tabsData.autoSaveAll || (tabsData.autoSaveUnpinned && !tab.pinned) || tabsData[tab.id].autoSave) && (!rule || rule.autoSaveProfile != singlefile.config.DISABLED_PROFILE_NAME);
  60. }
  61. async function refreshTabs() {
  62. const tabs = await singlefile.tabs.get({});
  63. return Promise.all(tabs.map(async tab => {
  64. try {
  65. const [options, autoSaveEnabled] = await Promise.all([singlefile.config.getOptions(tab.url, true), isEnabled(tab)]);
  66. await singlefile.tabs.sendMessage(tab.id, { initAutoSave: true, autoSaveEnabled, options });
  67. } catch (error) {
  68. /* ignored */
  69. }
  70. }));
  71. }
  72. async function saveContent(message, tab) {
  73. const options = await singlefile.config.getOptions(tab.url, true);
  74. const tabId = tab.id;
  75. options.content = message.content;
  76. options.url = message.url;
  77. options.framesData = message.framesData;
  78. options.canvasData = message.canvasData;
  79. options.fontsData = message.fontsData;
  80. options.stylesheetContents = message.stylesheetContents;
  81. options.imageData = message.imageData;
  82. options.postersData = message.postersData;
  83. options.usedFonts = message.usedFonts;
  84. options.shadowRootContents = message.shadowRootContents;
  85. options.referrer = message.referrer;
  86. options.insertSingleFileComment = true;
  87. options.insertFaviconLink = true;
  88. options.backgroundTab = true;
  89. options.autoSave = true;
  90. options.incognito = tab.incognito;
  91. options.tabId = tabId;
  92. let index = 0, maxIndex = 0;
  93. options.onprogress = async event => {
  94. if (event.type == event.RESOURCES_INITIALIZED) {
  95. maxIndex = event.detail.max;
  96. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  97. }
  98. if (event.type == event.RESOURCE_LOADED) {
  99. index++;
  100. singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
  101. } else if (event.type == event.PAGE_ENDED) {
  102. singlefile.ui.onEnd(tabId, { autoSave: true });
  103. }
  104. };
  105. const processor = new (SingleFileBrowser.getClass())(options);
  106. await processor.run();
  107. const page = await processor.getPageData();
  108. page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
  109. return singlefile.download.downloadPage(page, options);
  110. }
  111. })();