ui-options.js 29 KB

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