config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 singlefile, localStorage */
  21. singlefile.config = (() => {
  22. const DEFAULT_CONFIG = {
  23. removeHidden: false,
  24. removeUnusedCSSRules: false,
  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. contextMenuEnabled: true,
  34. shadowEnabled: true,
  35. maxResourceSizeEnabled: false,
  36. maxResourceSize: 10
  37. };
  38. const browser = this.browser || this.chrome;
  39. let pendingUpgradePromise;
  40. upgrade();
  41. function upgrade() {
  42. if (localStorage.config) {
  43. const config = JSON.parse(localStorage.config);
  44. upgradeConfig(config);
  45. delete localStorage.config;
  46. pendingUpgradePromise = new Promise(resolve => browser.storage.local.set(config, resolve));
  47. } else {
  48. pendingUpgradePromise = new Promise(resolve => browser.storage.local.get(config => {
  49. upgradeConfig(config);
  50. resolve();
  51. }));
  52. }
  53. }
  54. function upgradeConfig(config) {
  55. if (config.removeScripts === undefined) {
  56. config.removeScripts = true;
  57. }
  58. if (config.compressHTML === undefined) {
  59. config.compressHTML = true;
  60. }
  61. if (config.compressCSS === undefined) {
  62. config.compressCSS = 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. }
  86. return {
  87. async set(config) {
  88. await pendingUpgradePromise;
  89. return new Promise(resolve => browser.storage.local.set(config, resolve));
  90. },
  91. async get() {
  92. await pendingUpgradePromise;
  93. return new Promise(resolve => browser.storage.local.get(config => resolve(Object.keys(config).length ? config : DEFAULT_CONFIG)));
  94. },
  95. async reset() {
  96. await pendingUpgradePromise;
  97. return new Promise(resolve => browser.storage.local.clear(resolve));
  98. }
  99. };
  100. })();