options.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, document */
  21. (async () => {
  22. const bgPage = await browser.runtime.getBackgroundPage();
  23. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  24. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  25. const removeFramesLabel = document.getElementById("removeFramesLabel");
  26. const removeImportsLabel = document.getElementById("removeImportsLabel");
  27. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  28. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  29. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  30. const compressCSSLabel = document.getElementById("compressCSSLabel");
  31. const lazyLoadImagesLabel = document.getElementById("lazyLoadImagesLabel");
  32. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  33. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  34. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  35. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  36. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  37. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  38. const removeAudioLabel = document.getElementById("removeAudioLabel");
  39. const removeVideoLabel = document.getElementById("removeVideoLabel");
  40. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  41. const displayStatsLabel = document.getElementById("displayStatsLabel");
  42. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  43. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  44. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  45. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  46. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  47. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  48. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  49. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  50. const titleLabel = document.getElementById("titleLabel");
  51. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  52. const filenameLabel = document.getElementById("filenameLabel");
  53. const htmlContentLabel = document.getElementById("htmlContentLabel");
  54. const imagesLabel = document.getElementById("imagesLabel");
  55. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  56. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  57. const autoSaveLabel = document.getElementById("autoSaveLabel");
  58. const miscLabel = document.getElementById("miscLabel");
  59. const helpLabel = document.getElementById("helpLabel");
  60. const resetButton = document.getElementById("resetButton");
  61. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  62. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  63. const removeFramesInput = document.getElementById("removeFramesInput");
  64. const removeImportsInput = document.getElementById("removeImportsInput");
  65. const removeScriptsInput = document.getElementById("removeScriptsInput");
  66. const saveRawPageInput = document.getElementById("saveRawPageInput");
  67. const compressHTMLInput = document.getElementById("compressHTMLInput");
  68. const compressCSSInput = document.getElementById("compressCSSInput");
  69. const lazyLoadImagesInput = document.getElementById("lazyLoadImagesInput");
  70. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  71. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  72. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  73. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  74. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  75. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  76. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  77. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  78. const displayInfobarInput = document.getElementById("displayInfobarInput");
  79. const displayStatsInput = document.getElementById("displayStatsInput");
  80. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  81. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  82. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  83. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  84. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  85. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  86. const removeSrcSetInput = document.getElementById("removeSrcSetInput");
  87. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  88. const expandAllButton = document.getElementById("expandAllButton");
  89. let pendingSave = Promise.resolve();
  90. resetButton.addEventListener("click", async () => {
  91. await bgPage.singlefile.config.reset();
  92. await refresh();
  93. await update();
  94. }, false);
  95. maxResourceSizeEnabledInput.addEventListener("click", () => maxResourceSizeInput.disabled = !maxResourceSizeEnabledInput.checked, false);
  96. autoSaveUnloadInput.addEventListener("click", async () => {
  97. autoSaveDelayInput.disabled = autoSaveUnloadInput.checked;
  98. await bgPage.singlefile.ui.autosave.refresh();
  99. }, false);
  100. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  101. autoSaveUnloadInput.disabled = autoSaveLoadInput.disabled = autoSaveLoadOrUnloadInput.checked;
  102. if (autoSaveLoadOrUnloadInput.checked) {
  103. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  104. } else {
  105. autoSaveUnloadInput.checked = false;
  106. autoSaveLoadInput.checked = true;
  107. }
  108. await bgPage.singlefile.ui.autosave.refresh();
  109. }, false);
  110. expandAllButton.addEventListener("click", () => {
  111. if (expandAllButton.className) {
  112. expandAllButton.className = "";
  113. } else {
  114. expandAllButton.className = "opened";
  115. }
  116. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  117. }, false);
  118. document.body.onchange = update;
  119. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  120. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  121. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  122. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  123. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  124. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  125. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  126. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  127. lazyLoadImagesLabel.textContent = browser.i18n.getMessage("optionLazyLoadImages");
  128. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  129. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  130. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  131. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  132. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  133. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  134. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  135. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  136. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  137. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  138. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  139. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  140. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  141. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  142. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  143. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  144. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  145. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  146. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  147. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  148. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  149. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  150. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  151. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  152. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  153. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  154. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  155. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  156. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  157. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  158. refresh();
  159. async function refresh() {
  160. const config = await bgPage.singlefile.config.get();
  161. removeHiddenElementsInput.checked = config.removeHiddenElements;
  162. removeUnusedStylesInput.checked = config.removeUnusedStyles;
  163. removeFramesInput.checked = config.removeFrames;
  164. removeImportsInput.checked = config.removeImports;
  165. removeScriptsInput.checked = config.removeScripts;
  166. saveRawPageInput.checked = config.saveRawPage;
  167. compressHTMLInput.checked = config.compressHTML;
  168. compressCSSInput.checked = config.compressCSS;
  169. lazyLoadImagesInput.checked = config.lazyLoadImages;
  170. contextMenuEnabledInput.checked = config.contextMenuEnabled;
  171. filenameTemplateInput.value = config.filenameTemplate;
  172. shadowEnabledInput.checked = config.shadowEnabled;
  173. maxResourceSizeEnabledInput.checked = config.maxResourceSizeEnabled;
  174. maxResourceSizeInput.value = config.maxResourceSize;
  175. maxResourceSizeInput.disabled = !config.maxResourceSizeEnabled;
  176. confirmFilenameInput.checked = config.confirmFilename;
  177. removeAudioSrcInput.checked = config.removeAudioSrc;
  178. removeVideoSrcInput.checked = config.removeVideoSrc;
  179. displayInfobarInput.checked = config.displayInfobar;
  180. displayStatsInput.checked = config.displayStats;
  181. backgroundSaveInput.checked = config.backgroundSave;
  182. autoSaveDelayInput.value = config.autoSaveDelay;
  183. autoSaveDelayInput.disabled = config.autoSaveUnload;
  184. autoSaveLoadInput.checked = !config.autoSaveLoadOrUnload && config.autoSaveLoad;
  185. autoSaveLoadOrUnloadInput.checked = config.autoSaveLoadOrUnload;
  186. autoSaveUnloadInput.checked = !config.autoSaveLoadOrUnload && config.autoSaveUnload;
  187. autoSaveLoadInput.disabled = config.autoSaveLoadOrUnload;
  188. autoSaveUnloadInput.disabled = config.autoSaveLoadOrUnload;
  189. removeAlternativeFontsInput.checked = config.removeAlternativeFonts;
  190. removeSrcSetInput.checked = config.removeSrcSet;
  191. removeAlternativeMediasInput.checked = config.removeAlternativeMedias;
  192. }
  193. async function update() {
  194. await pendingSave;
  195. pendingSave = bgPage.singlefile.config.set({
  196. removeHiddenElements: removeHiddenElementsInput.checked,
  197. removeUnusedStyles: removeUnusedStylesInput.checked,
  198. removeFrames: removeFramesInput.checked,
  199. removeImports: removeImportsInput.checked,
  200. removeScripts: removeScriptsInput.checked,
  201. saveRawPage: saveRawPageInput.checked,
  202. compressHTML: compressHTMLInput.checked,
  203. compressCSS: compressCSSInput.checked,
  204. lazyLoadImages: lazyLoadImagesInput.checked,
  205. contextMenuEnabled: contextMenuEnabledInput.checked,
  206. filenameTemplate: filenameTemplateInput.value,
  207. shadowEnabled: shadowEnabledInput.checked,
  208. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  209. maxResourceSize: maxResourceSizeInput.value,
  210. confirmFilename: confirmFilenameInput.checked,
  211. removeAudioSrc: removeAudioSrcInput.checked,
  212. removeVideoSrc: removeVideoSrcInput.checked,
  213. displayInfobar: displayInfobarInput.checked,
  214. displayStats: displayStatsInput.checked,
  215. backgroundSave: backgroundSaveInput.checked,
  216. autoSaveDelay: autoSaveDelayInput.value,
  217. autoSaveLoad: autoSaveLoadInput.checked,
  218. autoSaveUnload: autoSaveUnloadInput.checked,
  219. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  220. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  221. removeSrcSet: removeSrcSetInput.checked,
  222. removeAlternativeMedias: removeAlternativeMediasInput.checked
  223. });
  224. await pendingSave;
  225. await bgPage.singlefile.ui.menu.refresh();
  226. }
  227. })();