config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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: false,
  24. removeUnusedStyles: true,
  25. removeFrames: true,
  26. removeImports: true,
  27. removeScripts: true,
  28. rawDocument: false,
  29. compressHTML: true,
  30. compressCSS: true,
  31. lazyLoadImages: true,
  32. appendSaveDate: true,
  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. removeSrcSet: true
  50. };
  51. let pendingUpgradePromise;
  52. browser.runtime.onMessage.addListener(request => {
  53. if (request.getConfig) {
  54. return getConfig();
  55. }
  56. });
  57. upgrade();
  58. async function upgrade() {
  59. if (localStorage.config) {
  60. const config = JSON.parse(localStorage.config);
  61. await upgradeConfig(config);
  62. delete localStorage.config;
  63. pendingUpgradePromise = browser.storage.local.set(config);
  64. } else {
  65. const config = await browser.storage.local.get();
  66. await upgradeConfig(config);
  67. pendingUpgradePromise = browser.storage.local.set(config);
  68. }
  69. }
  70. async function upgradeConfig(config) {
  71. if (config.removeScripts === undefined) {
  72. config.removeScripts = true;
  73. }
  74. config.compressHTML = config.compressCSS = config.compress;
  75. if (config.compressCSS === undefined) {
  76. config.compressCSS = true;
  77. }
  78. if (config.compressHTML === undefined) {
  79. config.compressHTML = true;
  80. }
  81. if (config.lazyLoadImages === undefined) {
  82. config.lazyLoadImages = true;
  83. }
  84. if (config.contextMenuEnabled === undefined) {
  85. config.contextMenuEnabled = true;
  86. }
  87. if (config.appendSaveDate === undefined) {
  88. config.appendSaveDate = true;
  89. }
  90. if (config.removeImports === undefined) {
  91. config.removeImports = true;
  92. }
  93. if (config.shadowEnabled === undefined) {
  94. config.shadowEnabled = true;
  95. }
  96. if (config.maxResourceSize === undefined) {
  97. config.maxResourceSize = 10;
  98. }
  99. if (config.maxResourceSize === 0) {
  100. config.maxResourceSize = 1;
  101. }
  102. if (config.removeUnusedStyles === undefined || config.removeUnusedCSSRules) {
  103. delete config.removeUnusedCSSRules;
  104. config.removeUnusedStyles = true;
  105. }
  106. if (config.removeFrames === undefined) {
  107. config.removeFrames = true;
  108. }
  109. if (config.removeAudioSrc === undefined) {
  110. config.removeAudioSrc = true;
  111. }
  112. if (config.removeVideoSrc === undefined) {
  113. config.removeVideoSrc = true;
  114. }
  115. if (config.displayInfobar === undefined) {
  116. config.displayInfobar = true;
  117. }
  118. if (config.backgroundSave === undefined) {
  119. config.backgroundSave = true;
  120. }
  121. if (config.autoSaveDelay === undefined) {
  122. config.autoSaveDelay = 1;
  123. }
  124. if (config.removeAlternativeFonts === undefined) {
  125. config.removeAlternativeFonts = true;
  126. }
  127. if (config.removeAlternativeMedias === undefined) {
  128. config.removeAlternativeMedias = true;
  129. }
  130. if (config.removeSrcSet === undefined) {
  131. config.removeSrcSet = true;
  132. }
  133. if (config.autoSaveLoadOrUnload === undefined && !config.autoSaveUnload) {
  134. config.autoSaveLoadOrUnload = true;
  135. config.autoSaveLoad = false;
  136. config.autoSaveUnload = false;
  137. }
  138. }
  139. async function getConfig() {
  140. await pendingUpgradePromise;
  141. const config = await browser.storage.local.get();
  142. config.tabsData = undefined;
  143. return Object.keys(config).length ? config : DEFAULT_CONFIG;
  144. }
  145. return {
  146. async set(config) {
  147. await pendingUpgradePromise;
  148. await browser.storage.local.set(config);
  149. },
  150. async get() {
  151. return getConfig();
  152. },
  153. async reset() {
  154. await pendingUpgradePromise;
  155. const { tabsData } = await browser.storage.local.get();
  156. await browser.storage.local.clear();
  157. const config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
  158. config.tabsData = tabsData;
  159. await browser.storage.local.set(config);
  160. }
  161. };
  162. })();