config.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: 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. };
  56. let pendingUpgradePromise;
  57. browser.runtime.onMessage.addListener(request => {
  58. if (request.getConfig) {
  59. return getConfig();
  60. }
  61. });
  62. upgrade();
  63. async function upgrade() {
  64. if (localStorage.config) {
  65. const config = JSON.parse(localStorage.config);
  66. await upgradeConfig(config);
  67. delete localStorage.config;
  68. pendingUpgradePromise = browser.storage.local.set(config);
  69. } else {
  70. const config = await browser.storage.local.get();
  71. await upgradeConfig(config);
  72. pendingUpgradePromise = browser.storage.local.set(config);
  73. }
  74. }
  75. async function upgradeConfig(config) {
  76. if (config.removeScripts === undefined) {
  77. config.removeScripts = true;
  78. }
  79. config.compressHTML = config.compressCSS = config.compress;
  80. if (config.compressCSS === undefined) {
  81. config.compressCSS = true;
  82. }
  83. if (config.compressHTML === undefined) {
  84. config.compressHTML = true;
  85. }
  86. if (config.lazyLoadImages === undefined) {
  87. config.lazyLoadImages = true;
  88. }
  89. if (config.contextMenuEnabled === undefined) {
  90. config.contextMenuEnabled = true;
  91. }
  92. if (config.filenameTemplate === undefined) {
  93. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  94. config.filenameTemplate = "{page-title} ({date-iso} {time-locale}).html";
  95. } else {
  96. config.filenameTemplate = "{page-title}.html";
  97. }
  98. delete config.appendSaveDate;
  99. }
  100. if (config.infobarTemplate === undefined) {
  101. config.infobarTemplate = "";
  102. }
  103. if (config.removeImports === undefined) {
  104. config.removeImports = true;
  105. }
  106. if (config.shadowEnabled === undefined) {
  107. config.shadowEnabled = true;
  108. }
  109. if (config.maxResourceSize === undefined) {
  110. config.maxResourceSize = 10;
  111. }
  112. if (config.maxResourceSize === 0) {
  113. config.maxResourceSize = 1;
  114. }
  115. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  116. delete config.removeUnusedCSSRules;
  117. config.removeUnusedStyles = true;
  118. }
  119. if (config.removeAudioSrc === undefined) {
  120. config.removeAudioSrc = true;
  121. }
  122. if (config.removeVideoSrc === undefined) {
  123. config.removeVideoSrc = true;
  124. }
  125. if (config.displayInfobar === undefined) {
  126. config.displayInfobar = true;
  127. }
  128. if (config.backgroundSave === undefined) {
  129. config.backgroundSave = true;
  130. }
  131. if (config.autoSaveDelay === undefined) {
  132. config.autoSaveDelay = 1;
  133. }
  134. if (config.removeAlternativeFonts === undefined) {
  135. config.removeAlternativeFonts = true;
  136. }
  137. if (config.removeAlternativeMedias === undefined) {
  138. config.removeAlternativeMedias = true;
  139. }
  140. if (config.removeAlternativeImages === undefined) {
  141. if (config.removeAlternativeImages === undefined) {
  142. config.removeAlternativeImages = true;
  143. } else {
  144. config.removeAlternativeImages = config.removeSrcSet;
  145. }
  146. }
  147. if (config.groupDuplicateImages === undefined) {
  148. config.groupDuplicateImages = true;
  149. }
  150. if (config.removeHiddenElements === undefined) {
  151. config.removeHiddenElements = true;
  152. }
  153. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  154. config.autoSaveLoadOrUnload = true;
  155. config.autoSaveLoad = false;
  156. config.autoSaveUnload = false;
  157. }
  158. if (config.maxLazyLoadImagesIdleTime === undefined) {
  159. config.maxLazyLoadImagesIdleTime = 1000;
  160. }
  161. if (config.confirmFilename === undefined) {
  162. config.confirmFilename = false;
  163. }
  164. if (config.conflictAction === undefined) {
  165. config.conflictAction = "uniquify";
  166. }
  167. }
  168. async function getConfig() {
  169. await pendingUpgradePromise;
  170. const config = await browser.storage.local.get();
  171. config.tabsData = undefined;
  172. return Object.keys(config).length ? config : DEFAULT_CONFIG;
  173. }
  174. return {
  175. async set(config) {
  176. await pendingUpgradePromise;
  177. await browser.storage.local.set(config);
  178. },
  179. async get() {
  180. return getConfig();
  181. },
  182. async reset() {
  183. await pendingUpgradePromise;
  184. const { tabsData } = await browser.storage.local.get();
  185. await browser.storage.local.clear();
  186. const config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
  187. config.tabsData = tabsData;
  188. await browser.storage.local.set(config);
  189. }
  190. };
  191. })();