config.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 */
  21. singlefile.config = (() => {
  22. const DEFAULT_CONFIG = {
  23. removeHiddenElements: true,
  24. removeUnusedStyles: true,
  25. removeFrames: false,
  26. removeImports: true,
  27. removeScripts: true,
  28. rawDocument: false,
  29. compressHTML: true,
  30. compressCSS: true,
  31. lazyLoadImages: true,
  32. maxLazyLoadImagesIdleTime: 1500,
  33. filenameTemplate: "{page-title} ({date-iso} {time-locale}).html",
  34. infobarTemplate: "",
  35. confirmInfobar: false,
  36. confirmFilename: false,
  37. conflictAction: "uniquify",
  38. contextMenuEnabled: true,
  39. shadowEnabled: true,
  40. maxResourceSizeEnabled: false,
  41. maxResourceSize: 10,
  42. removeAudioSrc: true,
  43. removeVideoSrc: true,
  44. displayInfobar: true,
  45. displayStats: false,
  46. backgroundSave: true,
  47. autoSaveDelay: 1,
  48. autoSaveLoad: false,
  49. autoSaveUnload: false,
  50. autoSaveLoadOrUnload: true,
  51. removeAlternativeFonts: true,
  52. removeAlternativeMedias: true,
  53. removeAlternativeImages: true,
  54. groupDuplicateImages: true,
  55. saveRawPage: false
  56. };
  57. let pendingUpgradePromise = upgrade();
  58. browser.runtime.onMessage.addListener(request => {
  59. if (request.getConfig) {
  60. return getConfig();
  61. }
  62. });
  63. async function upgrade() {
  64. const config = await browser.storage.local.get();
  65. const defaultConfig = config;
  66. if (!config.profiles) {
  67. delete defaultConfig.tabsData;
  68. applyUpgrade(defaultConfig);
  69. const config = { profiles: {}, defaultProfile: "default" };
  70. config.profiles[config.defaultProfile] = defaultConfig;
  71. browser.storage.local.remove(Object.keys(DEFAULT_CONFIG));
  72. return browser.storage.local.set(config);
  73. } else {
  74. Object.keys(config.profiles).forEach(profileName => applyUpgrade(config.profiles[profileName]));
  75. return browser.storage.local.set({ profiles: config.profiles });
  76. }
  77. }
  78. function applyUpgrade(config) {
  79. if (config.removeScripts === undefined) {
  80. config.removeScripts = true;
  81. }
  82. config.compressHTML = config.compressCSS = config.compress;
  83. if (config.compressCSS === undefined) {
  84. config.compressCSS = true;
  85. }
  86. if (config.compressHTML === undefined) {
  87. config.compressHTML = true;
  88. }
  89. if (config.lazyLoadImages === undefined) {
  90. config.lazyLoadImages = true;
  91. }
  92. if (config.contextMenuEnabled === undefined) {
  93. config.contextMenuEnabled = true;
  94. }
  95. if (config.filenameTemplate === undefined) {
  96. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  97. config.filenameTemplate = DEFAULT_CONFIG.filenameTemplate;
  98. } else {
  99. config.filenameTemplate = "{page-title}.html";
  100. }
  101. delete config.appendSaveDate;
  102. }
  103. if (config.infobarTemplate === undefined) {
  104. config.infobarTemplate = "";
  105. }
  106. if (config.removeImports === undefined) {
  107. config.removeImports = true;
  108. }
  109. if (config.shadowEnabled === undefined) {
  110. config.shadowEnabled = true;
  111. }
  112. if (config.maxResourceSize === undefined) {
  113. config.maxResourceSize = DEFAULT_CONFIG.maxResourceSize;
  114. }
  115. if (config.maxResourceSize === 0) {
  116. config.maxResourceSize = 1;
  117. }
  118. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  119. delete config.removeUnusedCSSRules;
  120. config.removeUnusedStyles = true;
  121. }
  122. if (config.removeAudioSrc === undefined) {
  123. config.removeAudioSrc = true;
  124. }
  125. if (config.removeVideoSrc === undefined) {
  126. config.removeVideoSrc = true;
  127. }
  128. if (config.displayInfobar === undefined) {
  129. config.displayInfobar = true;
  130. }
  131. if (config.backgroundSave === undefined) {
  132. config.backgroundSave = true;
  133. }
  134. if (config.autoSaveDelay === undefined) {
  135. config.autoSaveDelay = DEFAULT_CONFIG.autoSaveDelay;
  136. }
  137. if (config.removeAlternativeFonts === undefined) {
  138. config.removeAlternativeFonts = true;
  139. }
  140. if (config.removeAlternativeMedias === undefined) {
  141. config.removeAlternativeMedias = true;
  142. }
  143. if (config.removeAlternativeImages === undefined) {
  144. if (config.removeAlternativeImages === undefined) {
  145. config.removeAlternativeImages = true;
  146. } else {
  147. config.removeAlternativeImages = config.removeSrcSet;
  148. }
  149. }
  150. if (config.groupDuplicateImages === undefined) {
  151. config.groupDuplicateImages = true;
  152. }
  153. if (config.removeHiddenElements === undefined) {
  154. config.removeHiddenElements = true;
  155. }
  156. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  157. config.autoSaveLoadOrUnload = true;
  158. config.autoSaveLoad = false;
  159. config.autoSaveUnload = false;
  160. }
  161. if (config.maxLazyLoadImagesIdleTime === undefined) {
  162. config.maxLazyLoadImagesIdleTime = DEFAULT_CONFIG.maxLazyLoadImagesIdleTime;
  163. }
  164. if (config.confirmFilename === undefined) {
  165. config.confirmFilename = false;
  166. }
  167. if (config.conflictAction === undefined) {
  168. config.conflictAction = DEFAULT_CONFIG.conflictAction;
  169. }
  170. }
  171. async function getConfig() {
  172. await pendingUpgradePromise;
  173. return await browser.storage.local.get(["profiles", "defaultProfile"]);
  174. }
  175. return {
  176. async set(profiles) {
  177. await pendingUpgradePromise;
  178. await browser.storage.local.set({ profiles });
  179. },
  180. async get() {
  181. return getConfig();
  182. },
  183. async getDefaultConfig() {
  184. const config = await getConfig();
  185. return config.profiles[config.defaultProfile];
  186. },
  187. async setDefaultConfig(defaultConfig) {
  188. const config = await getConfig();
  189. config[config.defaultProfile] = defaultConfig;
  190. await this.set(config);
  191. },
  192. async reset() {
  193. await pendingUpgradePromise;
  194. await browser.storage.local.remove(["profiles", "defaultProfile"]);
  195. await browser.storage.local.set({ profiles: { default: DEFAULT_CONFIG }, defaultProfile: "default" });
  196. }
  197. };
  198. })();