ui-options.js 29 KB

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