config.js 4.6 KB

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