config.js 2.8 KB

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