config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2018 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 browser, singlefile, localStorage */
  21. singlefile.config = (() => {
  22. const DEFAULT_CONFIG = {
  23. removeHiddenElements: false,
  24. removeUnusedStyles: true,
  25. removeFrames: true,
  26. removeImports: true,
  27. removeScripts: true,
  28. rawDocument: false,
  29. compressHTML: true,
  30. compressCSS: true,
  31. lazyLoadImages: true,
  32. filenameTemplate: "{page-title} ({date-iso} {time-locale}).html",
  33. confirmFilename: false,
  34. contextMenuEnabled: true,
  35. shadowEnabled: true,
  36. maxResourceSizeEnabled: false,
  37. maxResourceSize: 10,
  38. removeAudioSrc: true,
  39. removeVideoSrc: true,
  40. displayInfobar: true,
  41. displayStats: false,
  42. backgroundSave: true,
  43. autoSaveDelay: 1,
  44. autoSaveLoad: false,
  45. autoSaveUnload: false,
  46. autoSaveLoadOrUnload: true,
  47. removeAlternativeFonts: true,
  48. removeAlternativeMedias: true,
  49. removeSrcSet: true
  50. };
  51. let pendingUpgradePromise;
  52. browser.runtime.onMessage.addListener(request => {
  53. if (request.getConfig) {
  54. return getConfig();
  55. }
  56. });
  57. upgrade();
  58. async function upgrade() {
  59. if (localStorage.config) {
  60. const config = JSON.parse(localStorage.config);
  61. await upgradeConfig(config);
  62. delete localStorage.config;
  63. pendingUpgradePromise = browser.storage.local.set(config);
  64. } else {
  65. const config = await browser.storage.local.get();
  66. await upgradeConfig(config);
  67. pendingUpgradePromise = browser.storage.local.set(config);
  68. }
  69. }
  70. async function upgradeConfig(config) {
  71. if (config.removeScripts === undefined) {
  72. config.removeScripts = true;
  73. }
  74. config.compressHTML = config.compressCSS = config.compress;
  75. if (config.compressCSS === undefined) {
  76. config.compressCSS = true;
  77. }
  78. if (config.compressHTML === undefined) {
  79. config.compressHTML = true;
  80. }
  81. if (config.lazyLoadImages === undefined) {
  82. config.lazyLoadImages = true;
  83. }
  84. if (config.contextMenuEnabled === undefined) {
  85. config.contextMenuEnabled = true;
  86. }
  87. if (config.filenameTemplate === undefined) {
  88. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  89. config.filenameTemplate = "{page-title} ({date-iso} {time-locale}).html";
  90. } else {
  91. config.filenameTemplate = "{page-title}.html";
  92. }
  93. delete config.appendSaveDate;
  94. }
  95. if (config.removeImports === undefined) {
  96. config.removeImports = true;
  97. }
  98. if (config.shadowEnabled === undefined) {
  99. config.shadowEnabled = true;
  100. }
  101. if (config.maxResourceSize === undefined) {
  102. config.maxResourceSize = 10;
  103. }
  104. if (config.maxResourceSize === 0) {
  105. config.maxResourceSize = 1;
  106. }
  107. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  108. delete config.removeUnusedCSSRules;
  109. config.removeUnusedStyles = true;
  110. }
  111. if (config.removeFrames === undefined) {
  112. config.removeFrames = true;
  113. }
  114. if (config.removeAudioSrc === undefined) {
  115. config.removeAudioSrc = true;
  116. }
  117. if (config.removeVideoSrc === undefined) {
  118. config.removeVideoSrc = true;
  119. }
  120. if (config.displayInfobar === undefined) {
  121. config.displayInfobar = true;
  122. }
  123. if (config.backgroundSave === undefined) {
  124. config.backgroundSave = true;
  125. }
  126. if (config.autoSaveDelay === undefined) {
  127. config.autoSaveDelay = 1;
  128. }
  129. if (config.removeAlternativeFonts === undefined) {
  130. config.removeAlternativeFonts = true;
  131. }
  132. if (config.removeAlternativeMedias === undefined) {
  133. config.removeAlternativeMedias = true;
  134. }
  135. if (config.removeSrcSet === undefined) {
  136. config.removeSrcSet = true;
  137. }
  138. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  139. config.autoSaveLoadOrUnload = true;
  140. config.autoSaveLoad = false;
  141. config.autoSaveUnload = false;
  142. }
  143. }
  144. async function getConfig() {
  145. await pendingUpgradePromise;
  146. const config = await browser.storage.local.get();
  147. config.tabsData = undefined;
  148. return Object.keys(config).length ? config : DEFAULT_CONFIG;
  149. }
  150. return {
  151. async set(config) {
  152. await pendingUpgradePromise;
  153. await browser.storage.local.set(config);
  154. },
  155. async get() {
  156. return getConfig();
  157. },
  158. async reset() {
  159. await pendingUpgradePromise;
  160. const { tabsData } = await browser.storage.local.get();
  161. await browser.storage.local.clear();
  162. const config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
  163. config.tabsData = tabsData;
  164. await browser.storage.local.set(config);
  165. }
  166. };
  167. })();