ui-options.js 30 KB

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