config.js 4.0 KB

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