config.js 3.3 KB

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