config.js 3.1 KB

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