ui-options.js 27 KB

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