config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. removeUnusedCSSRules: true,
  25. removeFrames: true,
  26. removeImports: true,
  27. removeScripts: true,
  28. rawDocument: false,
  29. compress: true,
  30. lazyLoadImages: true,
  31. appendSaveDate: true,
  32. confirmFilename: false,
  33. contextMenuEnabled: true,
  34. shadowEnabled: true,
  35. maxResourceSizeEnabled: false,
  36. maxResourceSize: 10
  37. };
  38. let pendingUpgradePromise;
  39. upgrade();
  40. async function upgrade() {
  41. if (localStorage.config) {
  42. const config = JSON.parse(localStorage.config);
  43. upgradeConfig(config);
  44. delete localStorage.config;
  45. pendingUpgradePromise = browser.storage.local.set(config);
  46. } else {
  47. const config = await browser.storage.local.get();
  48. upgradeConfig(config);
  49. pendingUpgradePromise = browser.storage.local.set(config);
  50. }
  51. }
  52. function upgradeConfig(config) {
  53. if (config.removeScripts === undefined) {
  54. config.removeScripts = true;
  55. }
  56. if (config.compress === undefined) {
  57. config.compress = true;
  58. }
  59. if (config.lazyLoadImages === undefined) {
  60. config.lazyLoadImages = true;
  61. }
  62. if (config.contextMenuEnabled === undefined) {
  63. config.contextMenuEnabled = true;
  64. }
  65. if (config.appendSaveDate === undefined) {
  66. config.appendSaveDate = true;
  67. }
  68. if (config.removeImports === undefined) {
  69. config.removeImports = true;
  70. }
  71. if (config.shadowEnabled === undefined) {
  72. config.shadowEnabled = true;
  73. }
  74. if (config.maxResourceSize === undefined) {
  75. config.maxResourceSize = 10;
  76. }
  77. if (config.maxResourceSize === 0) {
  78. config.maxResourceSize = 1;
  79. }
  80. if (config.removeUnusedCSSRules === undefined) {
  81. config.removeUnusedCSSRules = true;
  82. }
  83. if (config.removeFrames === undefined) {
  84. config.removeFrames = true;
  85. }
  86. }
  87. return {
  88. async set(config) {
  89. await pendingUpgradePromise;
  90. await browser.storage.local.set(config);
  91. },
  92. async get() {
  93. await pendingUpgradePromise;
  94. const config = await browser.storage.local.get();
  95. return Object.keys(config).length ? config : DEFAULT_CONFIG;
  96. },
  97. async reset() {
  98. await pendingUpgradePromise;
  99. await browser.storage.local.clear();
  100. }
  101. };
  102. })();