config.js 5.5 KB

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