config.js 3.6 KB

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