config.js 4.4 KB

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