ui-options.js 31 KB

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