config.js 5.3 KB

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