ui-options.js 30 KB

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