ui-options.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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) => { document.body.style.opacity = 0; const value = window.prompt(message, defaultMessage); document.body.style.opacity = 1; return value; };
  26. const confirm = browserInfo.name == CHROME_BROWSER_NAME ? message => bgPage.confirm(message) : message => { document.body.style.opacity = 0; const value = window.confirm(message); document.body.style.opacity = 1; return value; };
  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 autoSettingsLabel = document.getElementById("autoSettingsLabel");
  68. const autoSettingsUrlLabel = document.getElementById("autoSettingsUrlLabel");
  69. const autoSettingsProfileLabel = document.getElementById("autoSettingsProfileLabel");
  70. const autoSettingsAutoSaveProfileLabel = document.getElementById("autoSettingsAutoSaveProfileLabel");
  71. const showAllProfilesLabel = document.getElementById("showAllProfilesLabel");
  72. const showAutoSaveProfileLabel = document.getElementById("showAutoSaveProfileLabel");
  73. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  74. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  75. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  76. const miscLabel = document.getElementById("miscLabel");
  77. const helpLabel = document.getElementById("helpLabel");
  78. const addProfileButton = document.getElementById("addProfileButton");
  79. const deleteProfileButton = document.getElementById("deleteProfileButton");
  80. const renameProfileButton = document.getElementById("renameProfileButton");
  81. const resetButton = document.getElementById("resetButton");
  82. const profileNamesInput = document.getElementById("profileNamesInput");
  83. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  84. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  85. const removeFramesInput = document.getElementById("removeFramesInput");
  86. const removeImportsInput = document.getElementById("removeImportsInput");
  87. const removeScriptsInput = document.getElementById("removeScriptsInput");
  88. const saveRawPageInput = document.getElementById("saveRawPageInput");
  89. const compressHTMLInput = document.getElementById("compressHTMLInput");
  90. const compressCSSInput = document.getElementById("compressCSSInput");
  91. const lazyLoadImagesInput = document.getElementById("lazyLoadImagesInput");
  92. const maxLazyLoadImagesIdleTimeInput = document.getElementById("maxLazyLoadImagesIdleTimeInput");
  93. const contextMenuEnabledInput = document.getElementById("contextMenuEnabledInput");
  94. const filenameTemplateInput = document.getElementById("filenameTemplateInput");
  95. const shadowEnabledInput = document.getElementById("shadowEnabledInput");
  96. const maxResourceSizeInput = document.getElementById("maxResourceSizeInput");
  97. const maxResourceSizeEnabledInput = document.getElementById("maxResourceSizeEnabledInput");
  98. const confirmFilenameInput = document.getElementById("confirmFilenameInput");
  99. const conflictActionInput = document.getElementById("conflictActionInput");
  100. const removeAudioSrcInput = document.getElementById("removeAudioSrcInput");
  101. const removeVideoSrcInput = document.getElementById("removeVideoSrcInput");
  102. const displayInfobarInput = document.getElementById("displayInfobarInput");
  103. const displayStatsInput = document.getElementById("displayStatsInput");
  104. const backgroundSaveInput = document.getElementById("backgroundSaveInput");
  105. const autoSaveDelayInput = document.getElementById("autoSaveDelayInput");
  106. const autoSaveLoadInput = document.getElementById("autoSaveLoadInput");
  107. const autoSaveUnloadInput = document.getElementById("autoSaveUnloadInput");
  108. const autoSaveLoadOrUnloadInput = document.getElementById("autoSaveLoadOrUnloadInput");
  109. const removeAlternativeFontsInput = document.getElementById("removeAlternativeFontsInput");
  110. const removeAlternativeImagesInput = document.getElementById("removeAlternativeImagesInput");
  111. const removeAlternativeMediasInput = document.getElementById("removeAlternativeMediasInput");
  112. const groupDuplicateImagesInput = document.getElementById("groupDuplicateImagesInput");
  113. const infobarTemplateInput = document.getElementById("infobarTemplateInput");
  114. const confirmInfobarInput = document.getElementById("confirmInfobarInput");
  115. const expandAllButton = document.getElementById("expandAllButton");
  116. const ruleUrlInput = document.getElementById("ruleUrlInput");
  117. const ruleProfileInput = document.getElementById("ruleProfileInput");
  118. const ruleAutoSaveProfileInput = document.getElementById("ruleAutoSaveProfileInput");
  119. const ruleEditProfileInput = document.getElementById("ruleEditProfileInput");
  120. const ruleEditAutoSaveProfileInput = document.getElementById("ruleEditAutoSaveProfileInput");
  121. const ruleAddButton = document.getElementById("ruleAddButton");
  122. const rulesElement = document.querySelector(".rules-table");
  123. const rulesContainerElement = document.querySelector(".rules-table-container");
  124. const ruleEditUrlInput = document.getElementById("ruleEditUrlInput");
  125. const ruleEditButton = document.getElementById("ruleEditButton");
  126. const showAllProfilesInput = document.getElementById("showAllProfilesInput");
  127. const showAutoSaveProfileInput = document.getElementById("showAutoSaveProfileInput");
  128. let pendingSave = Promise.resolve();
  129. ruleAddButton.addEventListener("click", async () => {
  130. try {
  131. await singlefile.config.addRule(ruleUrlInput.value, ruleProfileInput.value, ruleAutoSaveProfileInput.value);
  132. ruleUrlInput.value = "";
  133. ruleProfileInput.value = ruleAutoSaveProfileInput.value = singlefile.config.DEFAULT_PROFILE_NAME;
  134. await refresh();
  135. ruleUrlInput.focus();
  136. } catch (error) {
  137. // ignored
  138. }
  139. }, false);
  140. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  141. ruleAddButton.disabled = !ruleUrlInput.value;
  142. const rules = await singlefile.config.getRules();
  143. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  144. ruleAddButton.disabled = true;
  145. }
  146. };
  147. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  148. ruleEditButton.disabled = !ruleEditUrlInput.value;
  149. const rules = await singlefile.config.getRules();
  150. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  151. ruleEditButton.disabled = true;
  152. }
  153. };
  154. showAutoSaveProfileInput.addEventListener("click", () => {
  155. if (showAutoSaveProfileInput.checked) {
  156. rulesContainerElement.classList.remove("compact");
  157. } else {
  158. rulesContainerElement.classList.add("compact");
  159. }
  160. }, false);
  161. addProfileButton.addEventListener("click", async () => {
  162. const profileName = prompt(browser.i18n.getMessage("profileAddPrompt"));
  163. if (profileName) {
  164. try {
  165. await singlefile.config.createProfile(profileName);
  166. await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  167. } catch (error) {
  168. // ignored
  169. }
  170. }
  171. }, false);
  172. deleteProfileButton.addEventListener("click", async () => {
  173. if (confirm(browser.i18n.getMessage("profileDeleteConfirm"))) {
  174. try {
  175. await singlefile.config.deleteProfile(profileNamesInput.value);
  176. profileNamesInput.value = null;
  177. await Promise.all([refresh(), singlefile.ui.menu.refresh()]);
  178. } catch (error) {
  179. // ignored
  180. }
  181. }
  182. }, false);
  183. renameProfileButton.addEventListener("click", async () => {
  184. const profileName = prompt(browser.i18n.getMessage("profileRenamePrompt"), profileNamesInput.value);
  185. if (profileName) {
  186. try {
  187. await singlefile.config.renameProfile(profileNamesInput.value, profileName);
  188. await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  189. } catch (error) {
  190. // ignored
  191. }
  192. }
  193. }, false);
  194. resetButton.addEventListener("click", async () => {
  195. if (confirm(browser.i18n.getMessage("optionsResetConfirm"))) {
  196. await singlefile.config.reset();
  197. await Promise.all([refresh(singlefile.config.DEFAULT_PROFILE_NAME), singlefile.ui.menu.refresh()]);
  198. await update();
  199. }
  200. }, false);
  201. autoSaveUnloadInput.addEventListener("click", async () => {
  202. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  203. autoSaveLoadOrUnloadInput.checked = true;
  204. }
  205. }, false);
  206. autoSaveLoadInput.addEventListener("click", async () => {
  207. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  208. autoSaveLoadOrUnloadInput.checked = true;
  209. }
  210. }, false);
  211. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  212. if (autoSaveLoadOrUnloadInput.checked) {
  213. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  214. } else {
  215. autoSaveUnloadInput.checked = false;
  216. autoSaveLoadInput.checked = true;
  217. }
  218. }, false);
  219. expandAllButton.addEventListener("click", () => {
  220. if (expandAllButton.className) {
  221. expandAllButton.className = "";
  222. } else {
  223. expandAllButton.className = "opened";
  224. }
  225. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  226. }, false);
  227. document.body.onchange = async event => {
  228. let target = event.target;
  229. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  230. if (target != profileNamesInput && target != showAllProfilesInput) {
  231. await update();
  232. }
  233. if (target == profileNamesInput) {
  234. await refresh(profileNamesInput.value);
  235. } else {
  236. await refresh();
  237. }
  238. }
  239. };
  240. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  241. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  242. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  243. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  244. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  245. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  246. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  247. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  248. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  249. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  250. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  251. lazyLoadImagesLabel.textContent = browser.i18n.getMessage("optionLazyLoadImages");
  252. maxLazyLoadImagesIdleTimeLabel.textContent = browser.i18n.getMessage("optionMaxLazyLoadImagesIdleTime");
  253. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  254. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  255. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  256. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  257. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  258. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  259. conflictActionLabel.textContent = browser.i18n.getMessage("optionConflictAction");
  260. conflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionConflictActionUniquify");
  261. conflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionConflictActionOverwrite");
  262. conflictActionPromptLabel.textContent = browser.i18n.getMessage("optionConflictActionPrompt");
  263. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  264. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  265. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  266. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  267. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  268. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  269. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  270. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  271. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  272. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  273. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  274. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  275. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  276. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  277. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  278. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  279. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  280. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  281. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  282. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  283. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  284. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  285. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  286. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  287. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  288. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  289. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  290. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  291. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  292. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  293. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  294. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  295. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  296. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  297. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  298. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  299. refresh();
  300. async function refresh(profileName) {
  301. const [profiles, rules] = await Promise.all([singlefile.config.getProfiles(), singlefile.config.getRules()]);
  302. const selectedProfileName = profileName || profileNamesInput.value || singlefile.config.DEFAULT_PROFILE_NAME;
  303. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  304. const profileNames = Object.keys(profiles);
  305. profileNamesInput.options.length = 0;
  306. ruleProfileInput.options.length = 0;
  307. ruleAutoSaveProfileInput.options.length = 0;
  308. ruleEditProfileInput.options.length = 0;
  309. ruleEditAutoSaveProfileInput.options.length = 0;
  310. let optionElement = document.createElement("option");
  311. optionElement.value = singlefile.config.DEFAULT_PROFILE_NAME;
  312. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  313. profileNamesInput.appendChild(optionElement);
  314. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  315. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  316. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  317. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  318. profileNames.forEach(profileName => {
  319. if (profileName != singlefile.config.DEFAULT_PROFILE_NAME) {
  320. const optionElement = document.createElement("option");
  321. optionElement.value = optionElement.textContent = profileName;
  322. profileNamesInput.appendChild(optionElement);
  323. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  324. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  325. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  326. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  327. }
  328. });
  329. optionElement = document.createElement("option");
  330. optionElement.value = singlefile.config.DISABLED_PROFILE_NAME;
  331. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  332. ruleAutoSaveProfileInput.appendChild(optionElement);
  333. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  334. const rulesDataElement = rulesElement.querySelector(".rules-data");
  335. Array.from(rulesDataElement.childNodes).forEach(node => node.remove());
  336. const editURLElement = rulesElement.querySelector(".rule-edit");
  337. const createURLElement = rulesElement.querySelector(".rule-create");
  338. createURLElement.hidden = false;
  339. editURLElement.hidden = true;
  340. rules.forEach(rule => {
  341. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  342. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  343. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  344. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  345. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  346. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  347. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  348. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  349. ruleElement.hidden = false;
  350. ruleElement.className = "tr data";
  351. rulesDataElement.appendChild(ruleElement);
  352. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  353. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  354. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  355. ruleDeleteButton.addEventListener("click", async () => {
  356. if (confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"))) {
  357. await singlefile.config.deleteRule(rule.url);
  358. await refresh();
  359. }
  360. }, false);
  361. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  362. ruleUpdateButton.addEventListener("click", async () => {
  363. if (editURLElement.hidden) {
  364. createURLElement.hidden = true;
  365. editURLElement.hidden = false;
  366. rulesDataElement.replaceChild(editURLElement, ruleElement);
  367. ruleEditUrlInput.value = rule.url;
  368. ruleEditProfileInput.value = rule.profile;
  369. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  370. ruleEditButton.onclick = async () => {
  371. rulesElement.appendChild(editURLElement);
  372. await singlefile.config.updateRule(rule.url, ruleEditUrlInput.value, ruleEditProfileInput.value, ruleEditAutoSaveProfileInput.value);
  373. await refresh();
  374. };
  375. }
  376. }, false);
  377. }
  378. });
  379. rulesElement.appendChild(createURLElement);
  380. profileNamesInput.value = selectedProfileName;
  381. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == singlefile.config.DEFAULT_PROFILE_NAME;
  382. const profileOptions = profiles[selectedProfileName];
  383. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  384. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  385. removeFramesInput.checked = profileOptions.removeFrames;
  386. removeImportsInput.checked = profileOptions.removeImports;
  387. removeScriptsInput.checked = profileOptions.removeScripts;
  388. saveRawPageInput.checked = profileOptions.saveRawPage;
  389. compressHTMLInput.checked = profileOptions.compressHTML;
  390. compressCSSInput.checked = profileOptions.compressCSS;
  391. lazyLoadImagesInput.checked = profileOptions.lazyLoadImages;
  392. maxLazyLoadImagesIdleTimeInput.value = profileOptions.maxLazyLoadImagesIdleTime;
  393. maxLazyLoadImagesIdleTimeInput.disabled = !profileOptions.lazyLoadImages;
  394. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  395. filenameTemplateInput.value = profileOptions.filenameTemplate;
  396. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  397. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  398. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  399. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  400. confirmFilenameInput.checked = profileOptions.confirmFilename;
  401. conflictActionInput.value = profileOptions.conflictAction;
  402. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  403. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  404. displayInfobarInput.checked = profileOptions.displayInfobar;
  405. displayStatsInput.checked = profileOptions.displayStats;
  406. backgroundSaveInput.checked = profileOptions.backgroundSave;
  407. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  408. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  409. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  410. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  411. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  412. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  413. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  414. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  415. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  416. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  417. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  418. infobarTemplateInput.value = profileOptions.infobarTemplate;
  419. confirmInfobarInput.checked = profileOptions.confirmInfobar;
  420. }
  421. function getProfileText(profileName) {
  422. return profileName == singlefile.config.DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == singlefile.config.DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  423. }
  424. async function update() {
  425. await pendingSave;
  426. pendingSave = singlefile.config.updateProfile(profileNamesInput.value, {
  427. removeHiddenElements: removeHiddenElementsInput.checked,
  428. removeUnusedStyles: removeUnusedStylesInput.checked,
  429. removeFrames: removeFramesInput.checked,
  430. removeImports: removeImportsInput.checked,
  431. removeScripts: removeScriptsInput.checked,
  432. saveRawPage: saveRawPageInput.checked,
  433. compressHTML: compressHTMLInput.checked,
  434. compressCSS: compressCSSInput.checked,
  435. lazyLoadImages: lazyLoadImagesInput.checked,
  436. maxLazyLoadImagesIdleTime: Math.max(maxLazyLoadImagesIdleTimeInput.value, 0),
  437. contextMenuEnabled: contextMenuEnabledInput.checked,
  438. filenameTemplate: filenameTemplateInput.value,
  439. shadowEnabled: shadowEnabledInput.checked,
  440. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  441. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  442. confirmFilename: confirmFilenameInput.checked,
  443. conflictAction: conflictActionInput.value,
  444. removeAudioSrc: removeAudioSrcInput.checked,
  445. removeVideoSrc: removeVideoSrcInput.checked,
  446. displayInfobar: displayInfobarInput.checked,
  447. displayStats: displayStatsInput.checked,
  448. backgroundSave: backgroundSaveInput.checked,
  449. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  450. autoSaveLoad: autoSaveLoadInput.checked,
  451. autoSaveUnload: autoSaveUnloadInput.checked,
  452. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  453. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  454. removeAlternativeImages: removeAlternativeImagesInput.checked,
  455. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  456. groupDuplicateImages: groupDuplicateImagesInput.checked,
  457. infobarTemplate: infobarTemplateInput.value,
  458. confirmInfobar: confirmInfobarInput.checked
  459. });
  460. await pendingSave;
  461. await singlefile.ui.menu.refresh();
  462. }
  463. })();