config.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_PROFILE_NAME = "__Default_Settings__";
  23. const DEFAULT_CONFIG = {
  24. removeHiddenElements: true,
  25. removeUnusedStyles: true,
  26. removeFrames: false,
  27. removeImports: true,
  28. removeScripts: true,
  29. rawDocument: false,
  30. compressHTML: true,
  31. compressCSS: true,
  32. lazyLoadImages: true,
  33. maxLazyLoadImagesIdleTime: 1500,
  34. filenameTemplate: "{page-title} ({date-iso} {time-locale}).html",
  35. infobarTemplate: "",
  36. confirmInfobar: false,
  37. confirmFilename: false,
  38. conflictAction: "uniquify",
  39. contextMenuEnabled: true,
  40. shadowEnabled: true,
  41. maxResourceSizeEnabled: false,
  42. maxResourceSize: 10,
  43. removeAudioSrc: true,
  44. removeVideoSrc: true,
  45. displayInfobar: true,
  46. displayStats: false,
  47. backgroundSave: true,
  48. autoSaveDelay: 1,
  49. autoSaveLoad: false,
  50. autoSaveUnload: false,
  51. autoSaveLoadOrUnload: true,
  52. removeAlternativeFonts: true,
  53. removeAlternativeMedias: true,
  54. removeAlternativeImages: true,
  55. groupDuplicateImages: true,
  56. saveRawPage: false
  57. };
  58. let pendingUpgradePromise = upgrade();
  59. browser.runtime.onMessage.addListener(request => {
  60. if (request.getConfig) {
  61. return getConfig();
  62. }
  63. });
  64. async function upgrade() {
  65. const config = await browser.storage.local.get();
  66. const defaultConfig = config;
  67. if (!config.profiles) {
  68. delete defaultConfig.tabsData;
  69. applyUpgrade(defaultConfig);
  70. const config = { profiles: {}, defaultProfile: DEFAULT_PROFILE_NAME };
  71. config.profiles[config.defaultProfile] = defaultConfig;
  72. browser.storage.local.remove(Object.keys(DEFAULT_CONFIG));
  73. return browser.storage.local.set(config);
  74. } else {
  75. Object.keys(config.profiles).forEach(profileName => applyUpgrade(config.profiles[profileName]));
  76. return browser.storage.local.set({ profiles: config.profiles });
  77. }
  78. }
  79. function applyUpgrade(config) {
  80. if (config.removeScripts === undefined) {
  81. config.removeScripts = true;
  82. }
  83. config.compressHTML = config.compressCSS = config.compress;
  84. if (config.compressCSS === undefined) {
  85. config.compressCSS = true;
  86. }
  87. if (config.compressHTML === undefined) {
  88. config.compressHTML = true;
  89. }
  90. if (config.lazyLoadImages === undefined) {
  91. config.lazyLoadImages = true;
  92. }
  93. if (config.contextMenuEnabled === undefined) {
  94. config.contextMenuEnabled = true;
  95. }
  96. if (config.filenameTemplate === undefined) {
  97. if (config.appendSaveDate || config.appendSaveDate === undefined) {
  98. config.filenameTemplate = DEFAULT_CONFIG.filenameTemplate;
  99. } else {
  100. config.filenameTemplate = "{page-title}.html";
  101. }
  102. delete config.appendSaveDate;
  103. }
  104. if (config.infobarTemplate === undefined) {
  105. config.infobarTemplate = "";
  106. }
  107. if (config.removeImports === undefined) {
  108. config.removeImports = true;
  109. }
  110. if (config.shadowEnabled === undefined) {
  111. config.shadowEnabled = true;
  112. }
  113. if (config.maxResourceSize === undefined) {
  114. config.maxResourceSize = DEFAULT_CONFIG.maxResourceSize;
  115. }
  116. if (config.maxResourceSize === 0) {
  117. config.maxResourceSize = 1;
  118. }
  119. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  120. delete config.removeUnusedCSSRules;
  121. config.removeUnusedStyles = true;
  122. }
  123. if (config.removeAudioSrc === undefined) {
  124. config.removeAudioSrc = true;
  125. }
  126. if (config.removeVideoSrc === undefined) {
  127. config.removeVideoSrc = true;
  128. }
  129. if (config.displayInfobar === undefined) {
  130. config.displayInfobar = true;
  131. }
  132. if (config.backgroundSave === undefined) {
  133. config.backgroundSave = true;
  134. }
  135. if (config.autoSaveDelay === undefined) {
  136. config.autoSaveDelay = DEFAULT_CONFIG.autoSaveDelay;
  137. }
  138. if (config.removeAlternativeFonts === undefined) {
  139. config.removeAlternativeFonts = true;
  140. }
  141. if (config.removeAlternativeMedias === undefined) {
  142. config.removeAlternativeMedias = true;
  143. }
  144. if (config.removeAlternativeImages === undefined) {
  145. if (config.removeAlternativeImages === undefined) {
  146. config.removeAlternativeImages = true;
  147. } else {
  148. config.removeAlternativeImages = config.removeSrcSet;
  149. }
  150. }
  151. if (config.groupDuplicateImages === undefined) {
  152. config.groupDuplicateImages = true;
  153. }
  154. if (config.removeHiddenElements === undefined) {
  155. config.removeHiddenElements = true;
  156. }
  157. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  158. config.autoSaveLoadOrUnload = true;
  159. config.autoSaveLoad = false;
  160. config.autoSaveUnload = false;
  161. }
  162. if (config.maxLazyLoadImagesIdleTime === undefined) {
  163. config.maxLazyLoadImagesIdleTime = DEFAULT_CONFIG.maxLazyLoadImagesIdleTime;
  164. }
  165. if (config.confirmFilename === undefined) {
  166. config.confirmFilename = false;
  167. }
  168. if (config.conflictAction === undefined) {
  169. config.conflictAction = DEFAULT_CONFIG.conflictAction;
  170. }
  171. }
  172. async function getConfig() {
  173. await pendingUpgradePromise;
  174. return await browser.storage.local.get(["profiles", "defaultProfile"]);
  175. }
  176. return {
  177. DEFAULT_PROFILE_NAME,
  178. async create(profileName) {
  179. const config = await getConfig();
  180. if (Object.keys(config.profiles).includes(profileName)) {
  181. throw new Error("Duplicate profile name");
  182. }
  183. config.profiles[profileName] = DEFAULT_CONFIG;
  184. await browser.storage.local.set({ profiles: config.profiles, defaultProfile: DEFAULT_PROFILE_NAME });
  185. },
  186. async get() {
  187. return getConfig();
  188. },
  189. async update(profileName, data) {
  190. const config = await getConfig();
  191. if (!Object.keys(config.profiles).includes(profileName)) {
  192. throw new Error("Profile not found");
  193. }
  194. config.profiles[profileName] = data;
  195. await browser.storage.local.set({ profiles: config.profiles });
  196. },
  197. async rename(oldProfileName, profileName) {
  198. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  199. if (!Object.keys(config.profiles).includes(oldProfileName)) {
  200. throw new Error("Profile not found");
  201. }
  202. if (Object.keys(config.profiles).includes(profileName)) {
  203. throw new Error("Duplicate profile name");
  204. }
  205. if (oldProfileName == DEFAULT_PROFILE_NAME) {
  206. throw new Error("Default settings cannot be renamed");
  207. }
  208. if (tabsData.profileName == oldProfileName) {
  209. tabsData.profileName = profileName;
  210. await singlefile.tabsData.set(tabsData);
  211. }
  212. config.profiles[profileName] = config.profiles[oldProfileName];
  213. delete config.profiles[oldProfileName];
  214. await browser.storage.local.set({ profiles: config.profiles });
  215. },
  216. async delete(profileName) {
  217. const [config, tabsData] = await Promise.all([getConfig(), singlefile.tabsData.get()]);
  218. if (!Object.keys(config.profiles).includes(profileName)) {
  219. throw new Error("Profile not found");
  220. }
  221. if (profileName == DEFAULT_PROFILE_NAME) {
  222. throw new Error("Default settings cannot be deleted");
  223. }
  224. if (tabsData.profileName == profileName) {
  225. delete tabsData.profileName;
  226. await singlefile.tabsData.set(tabsData);
  227. }
  228. delete config.profiles[profileName];
  229. await browser.storage.local.set({ profiles: config.profiles });
  230. },
  231. async reset() {
  232. await pendingUpgradePromise;
  233. const tabsData = await singlefile.tabsData.get();
  234. delete tabsData.profileName;
  235. await singlefile.tabsData.set(tabsData);
  236. await browser.storage.local.remove(["profiles", "defaultProfile"]);
  237. await browser.storage.local.set({ profiles: { [DEFAULT_PROFILE_NAME]: DEFAULT_CONFIG }, defaultProfile: DEFAULT_PROFILE_NAME });
  238. }
  239. };
  240. })();