config.js 4.5 KB

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