config.js 3.5 KB

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