config.js 4.8 KB

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