ui-options.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, window, document */
  21. (async () => {
  22. const CHROME_BROWSER_NAME = "Chrome";
  23. const [bgPage, browserInfo] = await Promise.all([browser.runtime.getBackgroundPage(), browser.runtime.getBrowserInfo()]);
  24. const singlefile = bgPage.singlefile;
  25. const prompt = browserInfo.name == CHROME_BROWSER_NAME ? (message, defaultMessage) => bgPage.prompt(message, defaultMessage) : (message, defaultMessage) => window.prompt(message, defaultMessage);
  26. const confirm = browserInfo.name == CHROME_BROWSER_NAME ? message => bgPage.confirm(message) : message => window.confirm(message);
  27. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  28. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  29. const removeFramesLabel = document.getElementById("removeFramesLabel");
  30. const removeImportsLabel = document.getElementById("removeImportsLabel");
  31. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  32. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  33. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  34. const compressCSSLabel = document.getElementById("compressCSSLabel");
  35. const lazyLoadImagesLabel = document.getElementById("lazyLoadImagesLabel");
  36. const maxLazyLoadImagesIdleTimeLabel = document.getElementById("maxLazyLoadImagesIdleTimeLabel");
  37. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  38. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  39. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  40. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  41. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  42. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  43. const conflictActionLabel = document.getElementById("conflictActionLabel");
  44. const conflictActionUniquifyLabel = document.getElementById("conflictActionUniquifyLabel");
  45. const conflictActionOverwriteLabel = document.getElementById("conflictActionOverwriteLabel");
  46. const conflictActionPromptLabel = document.getElementById("conflictActionPromptLabel");
  47. const removeAudioLabel = document.getElementById("removeAudioLabel");
  48. const removeVideoLabel = document.getElementById("removeVideoLabel");
  49. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  50. const displayStatsLabel = document.getElementById("displayStatsLabel");
  51. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  52. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  53. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  54. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  55. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  56. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  57. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  58. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  59. const titleLabel = document.getElementById("titleLabel");
  60. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  61. const filenameLabel = document.getElementById("filenameLabel");
  62. const htmlContentLabel = document.getElementById("htmlContentLabel");
  63. const imagesLabel = document.getElementById("imagesLabel");
  64. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  65. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  66. const autoSaveLabel = document.getElementById("autoSaveLabel");
  67. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  68. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  69. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  70. const miscLabel = document.getElementById("miscLabel");
  71. const helpLabel = document.getElementById("helpLabel");
  72. const addProfileButton = document.getElementById("addProfileButton");
  73. const deleteProfileButton = document.getElementById("deleteProfileButton");
  74. const renameProfileButton = document.getElementById("renameProfileButton");
  75. const resetButton = document.getElementById("resetButton");
  76. const profileNamesInput = document.getElementById("profileNamesInput");
  77. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  78. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  79. const removeFramesInput = document.getElementById("removeFramesInput");
  80. const removeImportsInput = document.getElementById("removeImportsInput");
  81. const removeScriptsInput = document.getElementById("removeScriptsInput");
  82. const saveRawPageInput = document.getElementById("saveRawPageInput");
  83. const compressHTMLInput = document.getElementById("compressHTMLInput");
  84. const compressCSSInput = document.getElementById("compressCSSInput");
  85. const lazyLoadImagesInput = document.getElementById("lazyLoadImagesInput");
  86. const maxLazyLoadImagesIdleTimeInput = document.getElementById("maxLazyLoadImagesIdleTimeInput");
  87. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  88. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  89. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  90. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  91. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  92. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  93. const conflictActionInput = document.getElementById("conflictActionInput");
  94. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  95. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  96. const displayInfobarInput = document.getElementById("displayInfobarInput");
  97. const displayStatsInput = document.getElementById("displayStatsInput");
  98. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  99. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  100. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  101. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  102. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  103. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  104. const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
  105. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  106. const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
  107. const infobarTemplateInput = document.getElementById("infobarTemplateInput");
  108. const confirmInfobarInput = document.getElementById("confirmInfobarInput");
  109. const expandAllButton = document.getElementById("expandAllButton");
  110. let pendingSave = Promise.resolve();
  111. addProfileButton.addEventListener("click", async () => {
  112. const profileName = prompt(browser.i18n.getMessage("profileAddPrompt"));
  113. if (profileName) {
  114. try {
  115. await singlefile.config.create(profileName);
  116. await await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  117. } catch (error) {
  118. // ignored
  119. }
  120. }
  121. }, false);
  122. deleteProfileButton.addEventListener("click", async () => {
  123. if (confirm(browser.i18n.getMessage("profileDeleteConfirm"))) {
  124. try {
  125. await singlefile.config.delete(profileNamesInput.value);
  126. profileNamesInput.value = null;
  127. await Promise.all([refresh(), singlefile.ui.menu.refresh()]);
  128. } catch (error) {
  129. // ignored
  130. }
  131. }
  132. }, false);
  133. renameProfileButton.addEventListener("click", async () => {
  134. const profileName = prompt(browser.i18n.getMessage("profileRenamePrompt"), profileNamesInput.value);
  135. if (profileName) {
  136. try {
  137. await singlefile.config.rename(profileNamesInput.value, profileName);
  138. await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  139. } catch (error) {
  140. // ignored
  141. }
  142. }
  143. }, false);
  144. resetButton.addEventListener("click", async () => {
  145. if (confirm(browser.i18n.getMessage("optionsResetConfirm"))) {
  146. await singlefile.config.reset();
  147. await Promise.all([refresh(), singlefile.ui.menu.refresh()]);
  148. await update();
  149. }
  150. }, false);
  151. autoSaveUnloadInput.addEventListener("click", async () => {
  152. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  153. autoSaveLoadOrUnloadInput.checked = true;
  154. }
  155. }, false);
  156. autoSaveLoadInput.addEventListener("click", async () => {
  157. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  158. autoSaveLoadOrUnloadInput.checked = true;
  159. }
  160. }, false);
  161. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  162. if (autoSaveLoadOrUnloadInput.checked) {
  163. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  164. } else {
  165. autoSaveUnloadInput.checked = false;
  166. autoSaveLoadInput.checked = true;
  167. }
  168. }, false);
  169. expandAllButton.addEventListener("click", () => {
  170. if (expandAllButton.className) {
  171. expandAllButton.className = "";
  172. } else {
  173. expandAllButton.className = "opened";
  174. }
  175. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  176. }, false);
  177. document.body.onchange = async event => {
  178. if (event.target != profileNamesInput) {
  179. await update();
  180. }
  181. await refresh();
  182. };
  183. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  184. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  185. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  186. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  187. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  188. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  189. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  190. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  191. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  192. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  193. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  194. lazyLoadImagesLabel.textContent = browser.i18n.getMessage("optionLazyLoadImages");
  195. maxLazyLoadImagesIdleTimeLabel.textContent = browser.i18n.getMessage("optionMaxLazyLoadImagesIdleTime");
  196. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  197. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  198. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  199. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  200. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  201. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  202. conflictActionLabel.textContent = browser.i18n.getMessage("optionConflictAction");
  203. conflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionConflictActionUniquify");
  204. conflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionConflictActionOverwrite");
  205. conflictActionPromptLabel.textContent = browser.i18n.getMessage("optionConflictActionPrompt");
  206. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  207. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  208. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  209. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  210. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  211. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  212. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  213. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  214. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  215. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  216. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  217. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  218. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  219. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  220. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  221. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  222. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  223. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  224. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  225. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  226. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  227. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  228. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  229. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  230. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  231. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  232. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  233. refresh();
  234. async function refresh(profileName) {
  235. const options = await bgPage.singlefile.config.get();
  236. const selectedProfileName = profileName || profileNamesInput.value || options.defaultProfile;
  237. profileNamesInput.childNodes.forEach(node => node.remove());
  238. const profileNames = Object.keys(options.profiles);
  239. profileNamesInput.options.length = 0;
  240. profileNames.forEach(profileName => {
  241. const optionElement = document.createElement("option");
  242. optionElement.value = optionElement.textContent = profileName;
  243. profileNamesInput.appendChild(optionElement);
  244. });
  245. profileNamesInput.value = selectedProfileName;
  246. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == singlefile.config.DEFAULT_PROFILE_NAME;
  247. const profileOptions = options.profiles[selectedProfileName];
  248. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  249. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  250. removeFramesInput.checked = profileOptions.removeFrames;
  251. removeImportsInput.checked = profileOptions.removeImports;
  252. removeScriptsInput.checked = profileOptions.removeScripts;
  253. saveRawPageInput.checked = profileOptions.saveRawPage;
  254. compressHTMLInput.checked = profileOptions.compressHTML;
  255. compressCSSInput.checked = profileOptions.compressCSS;
  256. lazyLoadImagesInput.checked = profileOptions.lazyLoadImages;
  257. maxLazyLoadImagesIdleTimeInput.value = profileOptions.maxLazyLoadImagesIdleTime;
  258. maxLazyLoadImagesIdleTimeInput.disabled = !profileOptions.lazyLoadImages;
  259. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  260. filenameTemplateInput.value = profileOptions.filenameTemplate;
  261. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  262. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  263. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  264. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  265. confirmFilenameInput.checked = profileOptions.confirmFilename;
  266. conflictActionInput.value = profileOptions.conflictAction;
  267. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  268. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  269. displayInfobarInput.checked = profileOptions.displayInfobar;
  270. displayStatsInput.checked = profileOptions.displayStats;
  271. backgroundSaveInput.checked = profileOptions.backgroundSave;
  272. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  273. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  274. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  275. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  276. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  277. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  278. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  279. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  280. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  281. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  282. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  283. infobarTemplateInput.value = profileOptions.infobarTemplate;
  284. confirmInfobarInput.checked = profileOptions.confirmInfobar;
  285. }
  286. async function update() {
  287. await pendingSave;
  288. pendingSave = bgPage.singlefile.config.update(profileNamesInput.value, {
  289. removeHiddenElements: removeHiddenElementsInput.checked,
  290. removeUnusedStyles: removeUnusedStylesInput.checked,
  291. removeFrames: removeFramesInput.checked,
  292. removeImports: removeImportsInput.checked,
  293. removeScripts: removeScriptsInput.checked,
  294. saveRawPage: saveRawPageInput.checked,
  295. compressHTML: compressHTMLInput.checked,
  296. compressCSS: compressCSSInput.checked,
  297. lazyLoadImages: lazyLoadImagesInput.checked,
  298. maxLazyLoadImagesIdleTime: Math.max(maxLazyLoadImagesIdleTimeInput.value, 0),
  299. contextMenuEnabled: contextMenuEnabledInput.checked,
  300. filenameTemplate: filenameTemplateInput.value,
  301. shadowEnabled: shadowEnabledInput.checked,
  302. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  303. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  304. confirmFilename: confirmFilenameInput.checked,
  305. conflictAction: conflictActionInput.value,
  306. removeAudioSrc: removeAudioSrcInput.checked,
  307. removeVideoSrc: removeVideoSrcInput.checked,
  308. displayInfobar: displayInfobarInput.checked,
  309. displayStats: displayStatsInput.checked,
  310. backgroundSave: backgroundSaveInput.checked,
  311. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  312. autoSaveLoad: autoSaveLoadInput.checked,
  313. autoSaveUnload: autoSaveUnloadInput.checked,
  314. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  315. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  316. removeAlternativeImages: removeAlternativeImagesInput.checked,
  317. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  318. groupDuplicateImages: groupDuplicateImagesInput.checked,
  319. infobarTemplate: infobarTemplateInput.value,
  320. confirmInfobar: confirmInfobarInput.checked
  321. });
  322. await pendingSave;
  323. await bgPage.singlefile.ui.menu.refresh();
  324. }
  325. })();