config.js 3.5 KB

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