config.js 5.4 KB

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