ui-options.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright 2010-2019 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 bgPage = await browser.runtime.getBackgroundPage();
  23. const singlefile = bgPage.singlefile;
  24. const removeHiddenElementsLabel = document.getElementById("removeHiddenElementsLabel");
  25. const removeUnusedStylesLabel = document.getElementById("removeUnusedStylesLabel");
  26. const removeUnusedFontsLabel = document.getElementById("removeUnusedFontsLabel");
  27. const removeFramesLabel = document.getElementById("removeFramesLabel");
  28. const removeImportsLabel = document.getElementById("removeImportsLabel");
  29. const removeScriptsLabel = document.getElementById("removeScriptsLabel");
  30. const saveRawPageLabel = document.getElementById("saveRawPageLabel");
  31. const compressHTMLLabel = document.getElementById("compressHTMLLabel");
  32. const compressCSSLabel = document.getElementById("compressCSSLabel");
  33. const loadDeferredImagesLabel = document.getElementById("loadDeferredImagesLabel");
  34. const loadDeferredImagesMaxIdleTimeLabel = document.getElementById("loadDeferredImagesMaxIdleTimeLabel");
  35. const addMenuEntryLabel = document.getElementById("addMenuEntryLabel");
  36. const filenameTemplateLabel = document.getElementById("filenameTemplateLabel");
  37. const shadowEnabledLabel = document.getElementById("shadowEnabledLabel");
  38. const setMaxResourceSizeLabel = document.getElementById("setMaxResourceSizeLabel");
  39. const maxResourceSizeLabel = document.getElementById("maxResourceSizeLabel");
  40. const confirmFilenameLabel = document.getElementById("confirmFilenameLabel");
  41. const filenameConflictActionLabel = document.getElementById("filenameConflictActionLabel");
  42. const filenameConflictActionUniquifyLabel = document.getElementById("filenameConflictActionUniquifyLabel");
  43. const filenameConflictActionOverwriteLabel = document.getElementById("filenameConflictActionOverwriteLabel");
  44. const filenameConflictActionPromptLabel = document.getElementById("filenameConflictActionPromptLabel");
  45. const removeAudioLabel = document.getElementById("removeAudioLabel");
  46. const removeVideoLabel = document.getElementById("removeVideoLabel");
  47. const displayInfobarLabel = document.getElementById("displayInfobarLabel");
  48. const displayStatsLabel = document.getElementById("displayStatsLabel");
  49. const backgroundSaveLabel = document.getElementById("backgroundSaveLabel");
  50. const autoSaveDelayLabel = document.getElementById("autoSaveDelayLabel");
  51. const autoSaveLoadLabel = document.getElementById("autoSaveLoadLabel");
  52. const autoSaveUnloadLabel = document.getElementById("autoSaveUnloadLabel");
  53. const autoSaveLoadOrUnloadLabel = document.getElementById("autoSaveLoadOrUnloadLabel");
  54. const removeAlternativeFontsLabel = document.getElementById("removeAlternativeFontsLabel");
  55. const removeAlternativeImagesLabel = document.getElementById("removeAlternativeImagesLabel");
  56. const removeAlternativeMediasLabel = document.getElementById("removeAlternativeMediasLabel");
  57. const titleLabel = document.getElementById("titleLabel");
  58. const userInterfaceLabel = document.getElementById("userInterfaceLabel");
  59. const filenameLabel = document.getElementById("filenameLabel");
  60. const htmlContentLabel = document.getElementById("htmlContentLabel");
  61. const imagesLabel = document.getElementById("imagesLabel");
  62. const stylesheetsLabel = document.getElementById("stylesheetsLabel");
  63. const fontsLabel = document.getElementById("fontsLabel");
  64. const otherResourcesLabel = document.getElementById("otherResourcesLabel");
  65. const autoSaveLabel = document.getElementById("autoSaveLabel");
  66. const autoSettingsLabel = document.getElementById("autoSettingsLabel");
  67. const autoSettingsUrlLabel = document.getElementById("autoSettingsUrlLabel");
  68. const autoSettingsProfileLabel = document.getElementById("autoSettingsProfileLabel");
  69. const autoSettingsAutoSaveProfileLabel = document.getElementById("autoSettingsAutoSaveProfileLabel");
  70. const showAllProfilesLabel = document.getElementById("showAllProfilesLabel");
  71. const showAutoSaveProfileLabel = document.getElementById("showAutoSaveProfileLabel");
  72. const groupDuplicateImagesLabel = document.getElementById("groupDuplicateImagesLabel");
  73. const confirmInfobarLabel = document.getElementById("confirmInfobarLabel");
  74. const infobarTemplateLabel = document.getElementById("infobarTemplateLabel");
  75. const miscLabel = document.getElementById("miscLabel");
  76. const helpLabel = document.getElementById("helpLabel");
  77. const addProfileButton = document.getElementById("addProfileButton");
  78. const deleteProfileButton = document.getElementById("deleteProfileButton");
  79. const renameProfileButton = document.getElementById("renameProfileButton");
  80. const resetButton = document.getElementById("resetButton");
  81. const exportButton = document.getElementById("exportButton");
  82. const importButton = document.getElementById("importButton");
  83. const fileInput = document.getElementById("fileInput");
  84. const profileNamesInput = document.getElementById("profileNamesInput");
  85. const removeHiddenElementsInput = document.getElementById("removeHiddenElementsInput");
  86. const removeUnusedStylesInput = document.getElementById("removeUnusedStylesInput");
  87. const removeUnusedFontsInput = document.getElementById("removeUnusedFontsInput");
  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. const resetAllButton = document.getElementById("resetAllButton");
  134. const resetCurrentButton = document.getElementById("resetCurrentButton");
  135. const resetCancelButton = document.getElementById("resetCancelButton");
  136. const confirmButton = document.getElementById("confirmButton");
  137. const cancelButton = document.getElementById("cancelButton");
  138. const promptInput = document.getElementById("promptInput");
  139. const promptCancelButton = document.getElementById("promptCancelButton");
  140. const promptConfirmButton = document.getElementById("promptConfirmButton");
  141. let pendingSave = Promise.resolve();
  142. let autoSaveProfileChanged;
  143. ruleProfileInput.onchange = () => {
  144. if (!autoSaveProfileChanged) {
  145. ruleAutoSaveProfileInput.value = ruleProfileInput.value;
  146. }
  147. };
  148. ruleAutoSaveProfileInput.onchange = () => {
  149. autoSaveProfileChanged = true;
  150. };
  151. rulesDeleteAllButton.addEventListener("click", async () => {
  152. if (await confirm(browser.i18n.getMessage("optionsDeleteDisplayedRulesConfirm"))) {
  153. await singlefile.config.deleteRules(!showAllProfilesInput.checked && profileNamesInput.value);
  154. await refresh();
  155. }
  156. }, false);
  157. createURLElement.onsubmit = async event => {
  158. event.preventDefault();
  159. try {
  160. await singlefile.config.addRule(ruleUrlInput.value, ruleProfileInput.value, ruleAutoSaveProfileInput.value);
  161. ruleUrlInput.value = "";
  162. ruleProfileInput.value = ruleAutoSaveProfileInput.value = singlefile.config.DEFAULT_PROFILE_NAME;
  163. autoSaveProfileChanged = false;
  164. await refresh();
  165. ruleUrlInput.focus();
  166. } catch (error) {
  167. // ignored
  168. }
  169. };
  170. ruleUrlInput.onclick = ruleUrlInput.onkeyup = ruleUrlInput.onchange = async () => {
  171. ruleAddButton.disabled = !ruleUrlInput.value;
  172. const rules = await singlefile.config.getRules();
  173. if (rules.find(rule => rule.url == ruleUrlInput.value)) {
  174. ruleAddButton.disabled = true;
  175. }
  176. };
  177. ruleEditUrlInput.onclick = ruleEditUrlInput.onkeyup = ruleEditUrlInput.onchange = async () => {
  178. ruleEditButton.disabled = !ruleEditUrlInput.value;
  179. const rules = await singlefile.config.getRules();
  180. if (rules.find(rule => rule.url == ruleEditUrlInput.value)) {
  181. ruleEditButton.disabled = true;
  182. }
  183. };
  184. if (localStorage.getItem("optionShowAutoSaveProfile")) {
  185. showAutoSaveProfileInput.checked = true;
  186. rulesContainerElement.classList.remove("compact");
  187. }
  188. showAutoSaveProfileInput.addEventListener("click", () => {
  189. if (showAutoSaveProfileInput.checked) {
  190. localStorage.setItem("optionShowAutoSaveProfile", 1);
  191. rulesContainerElement.classList.remove("compact");
  192. } else {
  193. localStorage.removeItem("optionShowAutoSaveProfile");
  194. rulesContainerElement.classList.add("compact");
  195. }
  196. }, false);
  197. if (localStorage.getItem("optionShowAllProfiles")) {
  198. showAllProfilesInput.checked = true;
  199. }
  200. showAllProfilesInput.addEventListener("click", () => {
  201. if (showAllProfilesInput.checked) {
  202. localStorage.setItem("optionShowAllProfiles", 1);
  203. } else {
  204. localStorage.removeItem("optionShowAllProfiles");
  205. }
  206. }, false);
  207. addProfileButton.addEventListener("click", async () => {
  208. const profileName = await prompt(browser.i18n.getMessage("profileAddPrompt"));
  209. if (profileName) {
  210. try {
  211. await singlefile.config.createProfile(profileName);
  212. await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  213. } catch (error) {
  214. // ignored
  215. }
  216. }
  217. }, false);
  218. deleteProfileButton.addEventListener("click", async () => {
  219. if (await confirm(browser.i18n.getMessage("profileDeleteConfirm"))) {
  220. try {
  221. await singlefile.config.deleteProfile(profileNamesInput.value);
  222. profileNamesInput.value = null;
  223. await Promise.all([refresh(), singlefile.ui.menu.refresh()]);
  224. } catch (error) {
  225. // ignored
  226. }
  227. }
  228. }, false);
  229. renameProfileButton.addEventListener("click", async () => {
  230. const profileName = await prompt(browser.i18n.getMessage("profileRenamePrompt"), profileNamesInput.value);
  231. if (profileName) {
  232. try {
  233. await singlefile.config.renameProfile(profileNamesInput.value, profileName);
  234. await Promise.all([refresh(profileName), singlefile.ui.menu.refresh()]);
  235. } catch (error) {
  236. // ignored
  237. }
  238. }
  239. }, false);
  240. resetButton.addEventListener("click", async () => {
  241. const choice = await reset();
  242. if (choice) {
  243. if (choice == "all") {
  244. await singlefile.config.resetProfiles();
  245. await Promise.all([refresh(singlefile.config.DEFAULT_PROFILE_NAME), singlefile.ui.menu.refresh()]);
  246. }
  247. if (choice == "current") {
  248. await singlefile.config.resetProfile(profileNamesInput.value);
  249. await refresh();
  250. }
  251. await update();
  252. }
  253. }, false);
  254. exportButton.addEventListener("click", async () => {
  255. await singlefile.config.export();
  256. }, false);
  257. importButton.addEventListener("click", () => {
  258. fileInput.onchange = async () => {
  259. if (fileInput.files.length) {
  260. await singlefile.config.import(fileInput.files[0]);
  261. await refresh(singlefile.config.DEFAULT_PROFILE_NAME);
  262. fileInput.value = "";
  263. }
  264. };
  265. fileInput.click();
  266. }, false);
  267. autoSaveUnloadInput.addEventListener("click", async () => {
  268. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  269. autoSaveLoadOrUnloadInput.checked = true;
  270. }
  271. }, false);
  272. autoSaveLoadInput.addEventListener("click", async () => {
  273. if (!autoSaveLoadInput.checked && !autoSaveUnloadInput.checked) {
  274. autoSaveLoadOrUnloadInput.checked = true;
  275. }
  276. }, false);
  277. autoSaveLoadOrUnloadInput.addEventListener("click", async () => {
  278. if (autoSaveLoadOrUnloadInput.checked) {
  279. autoSaveUnloadInput.checked = autoSaveLoadInput.checked = false;
  280. } else {
  281. autoSaveUnloadInput.checked = false;
  282. autoSaveLoadInput.checked = true;
  283. }
  284. }, false);
  285. expandAllButton.addEventListener("click", () => {
  286. if (expandAllButton.className) {
  287. expandAllButton.className = "";
  288. } else {
  289. expandAllButton.className = "opened";
  290. }
  291. document.querySelectorAll("details").forEach(detailElement => detailElement.open = Boolean(expandAllButton.className));
  292. }, false);
  293. removeScriptsInput.addEventListener("click", () => {
  294. if (!removeScriptsInput.checked) {
  295. removeHiddenElementsInput.checked = false;
  296. removeUnusedStylesInput.checked = false;
  297. }
  298. }, false);
  299. document.body.onchange = async event => {
  300. let target = event.target;
  301. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  302. if (target != profileNamesInput && target != showAllProfilesInput) {
  303. await update();
  304. }
  305. if (target == profileNamesInput) {
  306. await refresh(profileNamesInput.value);
  307. } else {
  308. await refresh();
  309. }
  310. }
  311. };
  312. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  313. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  314. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  315. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  316. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  317. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  318. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  319. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  320. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  321. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  322. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  323. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  324. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  325. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  326. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  327. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  328. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  329. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  330. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  331. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  332. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  333. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  334. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  335. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  336. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  337. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  338. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  339. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  340. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  341. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  342. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  343. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  344. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  345. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  346. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  347. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  348. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  349. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  350. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  351. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  352. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  353. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  354. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  355. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  356. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  357. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  358. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  359. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  360. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  361. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  362. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  363. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  364. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  365. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  366. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  367. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  368. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  369. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  370. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  371. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  372. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  373. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  374. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  375. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  376. resetAllButton.textContent = browser.i18n.getMessage("optionsResetAllButton");
  377. resetCurrentButton.textContent = browser.i18n.getMessage("optionsResetCurrentButton");
  378. resetCancelButton.textContent = promptCancelButton.textContent = cancelButton.textContent = browser.i18n.getMessage("optionsCancelButton");
  379. confirmButton.textContent = promptConfirmButton.textContent = browser.i18n.getMessage("optionsOKButton");
  380. document.getElementById("resetConfirmLabel").textContent = browser.i18n.getMessage("optionsResetConfirm");
  381. refresh();
  382. async function refresh(profileName) {
  383. const [profiles, rules] = await Promise.all([singlefile.config.getProfiles(), singlefile.config.getRules()]);
  384. const selectedProfileName = profileName || profileNamesInput.value || singlefile.config.DEFAULT_PROFILE_NAME;
  385. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  386. const profileNames = Object.keys(profiles);
  387. profileNamesInput.options.length = 0;
  388. ruleProfileInput.options.length = 0;
  389. ruleAutoSaveProfileInput.options.length = 0;
  390. ruleEditProfileInput.options.length = 0;
  391. ruleEditAutoSaveProfileInput.options.length = 0;
  392. let optionElement = document.createElement("option");
  393. optionElement.value = singlefile.config.DEFAULT_PROFILE_NAME;
  394. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  395. profileNamesInput.appendChild(optionElement);
  396. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  397. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  398. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  399. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  400. profileNames.forEach(profileName => {
  401. if (profileName != singlefile.config.DEFAULT_PROFILE_NAME) {
  402. const optionElement = document.createElement("option");
  403. optionElement.value = optionElement.textContent = profileName;
  404. profileNamesInput.appendChild(optionElement);
  405. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  406. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  407. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  408. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  409. }
  410. });
  411. optionElement = document.createElement("option");
  412. optionElement.value = singlefile.config.DISABLED_PROFILE_NAME;
  413. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  414. ruleAutoSaveProfileInput.appendChild(optionElement);
  415. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  416. const rulesDataElement = rulesElement.querySelector(".rules-data");
  417. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  418. const editURLElement = rulesElement.querySelector(".rule-edit");
  419. createURLElement.hidden = false;
  420. editURLElement.hidden = true;
  421. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  422. let rulesDisplayed;
  423. rules.forEach(rule => {
  424. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  425. rulesDisplayed = true;
  426. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  427. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  428. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  429. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  430. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  431. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  432. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  433. ruleElement.hidden = false;
  434. ruleElement.className = "tr data";
  435. rulesDataElement.appendChild(ruleElement);
  436. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  437. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  438. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  439. ruleDeleteButton.addEventListener("click", async () => {
  440. if (await confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"))) {
  441. await singlefile.config.deleteRule(rule.url);
  442. await refresh();
  443. }
  444. }, false);
  445. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  446. ruleUpdateButton.addEventListener("click", async () => {
  447. if (editURLElement.hidden) {
  448. createURLElement.hidden = true;
  449. editURLElement.hidden = false;
  450. rulesDataElement.replaceChild(editURLElement, ruleElement);
  451. ruleEditUrlInput.value = rule.url;
  452. ruleEditProfileInput.value = rule.profile;
  453. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  454. ruleEditUrlInput.focus();
  455. editURLElement.onsubmit = async event => {
  456. event.preventDefault();
  457. rulesElement.appendChild(editURLElement);
  458. await singlefile.config.updateRule(rule.url, ruleEditUrlInput.value, ruleEditProfileInput.value, ruleEditAutoSaveProfileInput.value);
  459. await refresh();
  460. ruleUrlInput.focus();
  461. };
  462. }
  463. }, false);
  464. }
  465. });
  466. rulesDeleteAllButton.disabled = !rulesDisplayed;
  467. rulesElement.appendChild(createURLElement);
  468. profileNamesInput.value = selectedProfileName;
  469. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == singlefile.config.DEFAULT_PROFILE_NAME;
  470. const profileOptions = profiles[selectedProfileName];
  471. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  472. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  473. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  474. removeFramesInput.checked = profileOptions.removeFrames;
  475. removeImportsInput.checked = profileOptions.removeImports;
  476. removeScriptsInput.checked = profileOptions.removeScripts;
  477. saveRawPageInput.checked = profileOptions.saveRawPage;
  478. compressHTMLInput.checked = profileOptions.compressHTML;
  479. compressCSSInput.checked = profileOptions.compressCSS;
  480. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages;
  481. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  482. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages;
  483. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  484. filenameTemplateInput.value = profileOptions.filenameTemplate;
  485. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  486. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  487. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  488. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  489. confirmFilenameInput.checked = profileOptions.confirmFilename;
  490. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  491. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  492. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  493. displayInfobarInput.checked = profileOptions.displayInfobar;
  494. displayStatsInput.checked = profileOptions.displayStats;
  495. backgroundSaveInput.checked = profileOptions.backgroundSave;
  496. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  497. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  498. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  499. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  500. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  501. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  502. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  503. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  504. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  505. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  506. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  507. infobarTemplateInput.value = profileOptions.infobarTemplate;
  508. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  509. removeFramesInput.disabled = saveRawPageInput.checked;
  510. loadDeferredImagesInput.disabled = saveRawPageInput.checked;
  511. }
  512. function getProfileText(profileName) {
  513. return profileName == singlefile.config.DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == singlefile.config.DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  514. }
  515. async function update() {
  516. await pendingSave;
  517. pendingSave = singlefile.config.updateProfile(profileNamesInput.value, {
  518. removeHiddenElements: removeHiddenElementsInput.checked,
  519. removeUnusedStyles: removeUnusedStylesInput.checked,
  520. removeUnusedFonts: removeUnusedFontsInput.checked,
  521. removeFrames: removeFramesInput.checked,
  522. removeImports: removeImportsInput.checked,
  523. removeScripts: removeScriptsInput.checked,
  524. saveRawPage: saveRawPageInput.checked,
  525. compressHTML: compressHTMLInput.checked,
  526. compressCSS: compressCSSInput.checked,
  527. loadDeferredImages: loadDeferredImagesInput.checked,
  528. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  529. contextMenuEnabled: contextMenuEnabledInput.checked,
  530. filenameTemplate: filenameTemplateInput.value,
  531. shadowEnabled: shadowEnabledInput.checked,
  532. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  533. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  534. confirmFilename: confirmFilenameInput.checked,
  535. filenameConflictAction: filenameConflictActionInput.value,
  536. removeAudioSrc: removeAudioSrcInput.checked,
  537. removeVideoSrc: removeVideoSrcInput.checked,
  538. displayInfobar: displayInfobarInput.checked,
  539. displayStats: displayStatsInput.checked,
  540. backgroundSave: backgroundSaveInput.checked,
  541. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  542. autoSaveLoad: autoSaveLoadInput.checked,
  543. autoSaveUnload: autoSaveUnloadInput.checked,
  544. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  545. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  546. removeAlternativeImages: removeAlternativeImagesInput.checked,
  547. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  548. groupDuplicateImages: groupDuplicateImagesInput.checked,
  549. infobarTemplate: infobarTemplateInput.value,
  550. confirmInfobarContent: confirmInfobarInput.checked
  551. });
  552. await pendingSave;
  553. await singlefile.ui.menu.refresh();
  554. }
  555. async function confirm(message) {
  556. document.getElementById("confirmLabel").textContent = message;
  557. document.getElementById("formConfirmContainer").hidden = false;
  558. confirmButton.focus();
  559. document.body.style.overflowY = "hidden";
  560. return new Promise(resolve => {
  561. confirmButton.onclick = event => hideAndResolve(event, true);
  562. cancelButton.onclick = event => hideAndResolve(event);
  563. window.onkeyup = event => {
  564. if (event.key == "Escape") {
  565. hideAndResolve(event);
  566. }
  567. };
  568. function hideAndResolve(event, value) {
  569. event.preventDefault();
  570. document.getElementById("formConfirmContainer").hidden = true;
  571. document.body.style.overflowY = "";
  572. resolve(value);
  573. }
  574. });
  575. }
  576. async function reset() {
  577. document.getElementById("formResetContainer").hidden = false;
  578. resetCancelButton.focus();
  579. document.body.style.overflowY = "hidden";
  580. return new Promise(resolve => {
  581. resetAllButton.onclick = event => hideAndResolve(event, "all");
  582. resetCurrentButton.onclick = event => hideAndResolve(event, "current");
  583. resetCancelButton.onclick = event => hideAndResolve(event);
  584. window.onkeyup = event => {
  585. if (event.key == "Escape") {
  586. hideAndResolve(event);
  587. }
  588. };
  589. function hideAndResolve(event, value) {
  590. event.preventDefault();
  591. document.getElementById("formResetContainer").hidden = true;
  592. document.body.style.overflowY = "";
  593. resolve(value);
  594. }
  595. });
  596. }
  597. async function prompt(message, defaultValue = "") {
  598. document.getElementById("promptLabel").textContent = message;
  599. document.getElementById("formPromptContainer").hidden = false;
  600. promptInput.value = defaultValue;
  601. promptInput.focus();
  602. document.body.style.overflowY = "hidden";
  603. return new Promise(resolve => {
  604. promptConfirmButton.onclick = event => hideAndResolve(event, promptInput.value);
  605. promptCancelButton.onclick = event => hideAndResolve(event);
  606. window.onkeyup = event => {
  607. if (event.key == "Escape") {
  608. hideAndResolve(event);
  609. }
  610. };
  611. function hideAndResolve(event, value) {
  612. event.preventDefault();
  613. document.getElementById("formPromptContainer").hidden = true;
  614. document.body.style.overflowY = "";
  615. resolve(value);
  616. }
  617. });
  618. }
  619. })();