ui-options.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 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.removeItem("optionShowAutoSaveProfile");
  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.removeItem("optionShowAllProfiles");
  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. removeScriptsInput.addEventListener("click", () => {
  282. if (!removeScriptsInput.checked) {
  283. removeHiddenElementsInput.checked = false;
  284. removeUnusedStylesInput.checked = false;
  285. }
  286. }, false);
  287. document.body.onchange = async event => {
  288. let target = event.target;
  289. if (target != ruleUrlInput && target != ruleProfileInput && target != ruleAutoSaveProfileInput && target != ruleEditUrlInput && target != ruleEditProfileInput && target != ruleEditAutoSaveProfileInput && target != showAutoSaveProfileInput) {
  290. if (target != profileNamesInput && target != showAllProfilesInput) {
  291. await update();
  292. }
  293. if (target == profileNamesInput) {
  294. await refresh(profileNamesInput.value);
  295. } else {
  296. await refresh();
  297. }
  298. }
  299. };
  300. addProfileButton.title = browser.i18n.getMessage("profileAddButtonTooltip");
  301. deleteProfileButton.title = browser.i18n.getMessage("profileDeleteButtonTooltip");
  302. renameProfileButton.title = browser.i18n.getMessage("profileRenameButtonTooltip");
  303. removeHiddenElementsLabel.textContent = browser.i18n.getMessage("optionRemoveHiddenElements");
  304. removeUnusedStylesLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedStyles");
  305. removeUnusedFontsLabel.textContent = browser.i18n.getMessage("optionRemoveUnusedFonts");
  306. removeFramesLabel.textContent = browser.i18n.getMessage("optionRemoveFrames");
  307. removeImportsLabel.textContent = browser.i18n.getMessage("optionRemoveImports");
  308. removeScriptsLabel.textContent = browser.i18n.getMessage("optionRemoveScripts");
  309. saveRawPageLabel.textContent = browser.i18n.getMessage("optionSaveRawPage");
  310. compressHTMLLabel.textContent = browser.i18n.getMessage("optionCompressHTML");
  311. compressCSSLabel.textContent = browser.i18n.getMessage("optionCompressCSS");
  312. loadDeferredImagesLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImages");
  313. loadDeferredImagesMaxIdleTimeLabel.textContent = browser.i18n.getMessage("optionLoadDeferredImagesMaxIdleTime");
  314. addMenuEntryLabel.textContent = browser.i18n.getMessage("optionAddMenuEntry");
  315. filenameTemplateLabel.textContent = browser.i18n.getMessage("optionFilenameTemplate");
  316. shadowEnabledLabel.textContent = browser.i18n.getMessage("optionDisplayShadow");
  317. setMaxResourceSizeLabel.textContent = browser.i18n.getMessage("optionSetMaxResourceSize");
  318. maxResourceSizeLabel.textContent = browser.i18n.getMessage("optionMaxResourceSize");
  319. confirmFilenameLabel.textContent = browser.i18n.getMessage("optionConfirmFilename");
  320. filenameConflictActionLabel.textContent = browser.i18n.getMessage("optionFilenameConflictAction");
  321. filenameConflictActionUniquifyLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionUniquify");
  322. filenameConflictActionOverwriteLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionOverwrite");
  323. filenameConflictActionPromptLabel.textContent = browser.i18n.getMessage("optionFilenameConflictActionPrompt");
  324. removeAudioLabel.textContent = browser.i18n.getMessage("optionRemoveAudio");
  325. removeVideoLabel.textContent = browser.i18n.getMessage("optionRemoveVideo");
  326. displayInfobarLabel.textContent = browser.i18n.getMessage("optionDisplayInfobar");
  327. displayStatsLabel.textContent = browser.i18n.getMessage("optionDisplayStats");
  328. backgroundSaveLabel.textContent = browser.i18n.getMessage("optionBackgroundSave");
  329. autoSaveDelayLabel.textContent = browser.i18n.getMessage("optionAutoSaveDelay");
  330. autoSaveLoadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoad");
  331. autoSaveUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveUnload");
  332. autoSaveLoadOrUnloadLabel.textContent = browser.i18n.getMessage("optionAutoSaveLoadOrUnload");
  333. removeAlternativeFontsLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeFonts");
  334. removeAlternativeImagesLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeImages");
  335. removeAlternativeMediasLabel.textContent = browser.i18n.getMessage("optionRemoveAlternativeMedias");
  336. groupDuplicateImagesLabel.textContent = browser.i18n.getMessage("optionGroupDuplicateImages");
  337. titleLabel.textContent = browser.i18n.getMessage("optionsTitle");
  338. userInterfaceLabel.textContent = browser.i18n.getMessage("optionsUserInterfaceSubTitle");
  339. filenameLabel.textContent = browser.i18n.getMessage("optionsFileNameSubTitle");
  340. htmlContentLabel.textContent = browser.i18n.getMessage("optionsHTMLContentSubTitle");
  341. imagesLabel.textContent = browser.i18n.getMessage("optionsImagesSubTitle");
  342. stylesheetsLabel.textContent = browser.i18n.getMessage("optionsStylesheetsSubTitle");
  343. fontsLabel.textContent = browser.i18n.getMessage("optionsFontsSubTitle");
  344. otherResourcesLabel.textContent = browser.i18n.getMessage("optionsOtherResourcesSubTitle");
  345. autoSaveLabel.textContent = browser.i18n.getMessage("optionsAutoSaveSubTitle");
  346. miscLabel.textContent = browser.i18n.getMessage("optionsMiscSubTitle");
  347. helpLabel.textContent = browser.i18n.getMessage("optionsHelpLink");
  348. infobarTemplateLabel.textContent = browser.i18n.getMessage("optionInfobarTemplate");
  349. confirmInfobarLabel.textContent = browser.i18n.getMessage("optionConfirmInfobar");
  350. resetButton.textContent = browser.i18n.getMessage("optionsResetButton");
  351. exportButton.textContent = browser.i18n.getMessage("optionsExportButton");
  352. importButton.textContent = browser.i18n.getMessage("optionsImportButton");
  353. resetButton.title = browser.i18n.getMessage("optionsResetTooltip");
  354. autoSettingsLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsSubTitle");
  355. autoSettingsUrlLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsUrl");
  356. autoSettingsProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsProfile");
  357. autoSettingsAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsAutoSaveProfile");
  358. ruleAddButton.title = browser.i18n.getMessage("optionsAddRuleTooltip");
  359. ruleEditButton.title = browser.i18n.getMessage("optionsValidateChangesTooltip");
  360. rulesDeleteAllButton.title = browser.i18n.getMessage("optionsDeleteRulesTooltip");
  361. showAllProfilesLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAllProfiles");
  362. showAutoSaveProfileLabel.textContent = browser.i18n.getMessage("optionsAutoSettingsShowAutoSaveProfile");
  363. ruleUrlInput.placeholder = ruleEditUrlInput.placeholder = browser.i18n.getMessage("optionsAutoSettingsUrlPlaceholder");
  364. refresh();
  365. async function refresh(profileName) {
  366. const [profiles, rules] = await Promise.all([singlefile.config.getProfiles(), singlefile.config.getRules()]);
  367. const selectedProfileName = profileName || profileNamesInput.value || singlefile.config.DEFAULT_PROFILE_NAME;
  368. Array.from(profileNamesInput.childNodes).forEach(node => node.remove());
  369. const profileNames = Object.keys(profiles);
  370. profileNamesInput.options.length = 0;
  371. ruleProfileInput.options.length = 0;
  372. ruleAutoSaveProfileInput.options.length = 0;
  373. ruleEditProfileInput.options.length = 0;
  374. ruleEditAutoSaveProfileInput.options.length = 0;
  375. let optionElement = document.createElement("option");
  376. optionElement.value = singlefile.config.DEFAULT_PROFILE_NAME;
  377. optionElement.textContent = browser.i18n.getMessage("profileDefaultSettings");
  378. profileNamesInput.appendChild(optionElement);
  379. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  380. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  381. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  382. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  383. profileNames.forEach(profileName => {
  384. if (profileName != singlefile.config.DEFAULT_PROFILE_NAME) {
  385. const optionElement = document.createElement("option");
  386. optionElement.value = optionElement.textContent = profileName;
  387. profileNamesInput.appendChild(optionElement);
  388. ruleProfileInput.appendChild(optionElement.cloneNode(true));
  389. ruleAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  390. ruleEditProfileInput.appendChild(optionElement.cloneNode(true));
  391. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  392. }
  393. });
  394. optionElement = document.createElement("option");
  395. optionElement.value = singlefile.config.DISABLED_PROFILE_NAME;
  396. optionElement.textContent = browser.i18n.getMessage("profileDisabled");
  397. ruleAutoSaveProfileInput.appendChild(optionElement);
  398. ruleEditAutoSaveProfileInput.appendChild(optionElement.cloneNode(true));
  399. const rulesDataElement = rulesElement.querySelector(".rules-data");
  400. Array.from(rulesDataElement.childNodes).forEach(node => (!node.className || !node.className.includes("rule-edit")) && node.remove());
  401. const editURLElement = rulesElement.querySelector(".rule-edit");
  402. createURLElement.hidden = false;
  403. editURLElement.hidden = true;
  404. ruleProfileInput.value = ruleAutoSaveProfileInput.value = selectedProfileName;
  405. let rulesDisplayed;
  406. rules.forEach(rule => {
  407. if (showAllProfilesInput.checked || selectedProfileName == rule.profile || selectedProfileName == rule.autoSaveProfile) {
  408. rulesDisplayed = true;
  409. const ruleElement = rulesElement.querySelector(".rule-view").cloneNode(true);
  410. const ruleUrlElement = ruleElement.querySelector(".rule-url");
  411. const ruleProfileElement = ruleElement.querySelector(".rule-profile");
  412. const ruleAutoSaveProfileElement = ruleElement.querySelector(".rule-autosave-profile");
  413. ruleUrlElement.textContent = ruleUrlElement.title = rule.url;
  414. ruleProfileElement.textContent = ruleProfileElement.title = getProfileText(rule.profile);
  415. ruleAutoSaveProfileElement.textContent = ruleAutoSaveProfileElement.title = getProfileText(rule.autoSaveProfile);
  416. ruleElement.hidden = false;
  417. ruleElement.className = "tr data";
  418. rulesDataElement.appendChild(ruleElement);
  419. const ruleDeleteButton = ruleElement.querySelector(".rule-delete-button");
  420. const ruleUpdateButton = ruleElement.querySelector(".rule-update-button");
  421. ruleDeleteButton.title = browser.i18n.getMessage("optionsDeleteRuleTooltip");
  422. ruleDeleteButton.addEventListener("click", async () => {
  423. if (confirm(browser.i18n.getMessage("optionsDeleteRuleConfirm"))) {
  424. await singlefile.config.deleteRule(rule.url);
  425. await refresh();
  426. }
  427. }, false);
  428. ruleUpdateButton.title = browser.i18n.getMessage("optionsUpdateRuleTooltip");
  429. ruleUpdateButton.addEventListener("click", async () => {
  430. if (editURLElement.hidden) {
  431. createURLElement.hidden = true;
  432. editURLElement.hidden = false;
  433. rulesDataElement.replaceChild(editURLElement, ruleElement);
  434. ruleEditUrlInput.value = rule.url;
  435. ruleEditProfileInput.value = rule.profile;
  436. ruleEditAutoSaveProfileInput.value = rule.autoSaveProfile;
  437. ruleEditUrlInput.focus();
  438. editURLElement.onsubmit = async event => {
  439. event.preventDefault();
  440. rulesElement.appendChild(editURLElement);
  441. await singlefile.config.updateRule(rule.url, ruleEditUrlInput.value, ruleEditProfileInput.value, ruleEditAutoSaveProfileInput.value);
  442. await refresh();
  443. ruleUrlInput.focus();
  444. };
  445. }
  446. }, false);
  447. }
  448. });
  449. rulesDeleteAllButton.disabled = !rulesDisplayed;
  450. rulesElement.appendChild(createURLElement);
  451. profileNamesInput.value = selectedProfileName;
  452. renameProfileButton.disabled = deleteProfileButton.disabled = profileNamesInput.value == singlefile.config.DEFAULT_PROFILE_NAME;
  453. const profileOptions = profiles[selectedProfileName];
  454. removeHiddenElementsInput.checked = profileOptions.removeHiddenElements;
  455. removeUnusedStylesInput.checked = profileOptions.removeUnusedStyles;
  456. removeUnusedFontsInput.checked = profileOptions.removeUnusedFonts;
  457. removeFramesInput.checked = profileOptions.removeFrames;
  458. removeImportsInput.checked = profileOptions.removeImports;
  459. removeScriptsInput.checked = profileOptions.removeScripts;
  460. saveRawPageInput.checked = profileOptions.saveRawPage;
  461. compressHTMLInput.checked = profileOptions.compressHTML;
  462. compressCSSInput.checked = profileOptions.compressCSS;
  463. loadDeferredImagesInput.checked = profileOptions.loadDeferredImages;
  464. loadDeferredImagesMaxIdleTimeInput.value = profileOptions.loadDeferredImagesMaxIdleTime;
  465. loadDeferredImagesMaxIdleTimeInput.disabled = !profileOptions.loadDeferredImages;
  466. contextMenuEnabledInput.checked = profileOptions.contextMenuEnabled;
  467. filenameTemplateInput.value = profileOptions.filenameTemplate;
  468. shadowEnabledInput.checked = profileOptions.shadowEnabled;
  469. maxResourceSizeEnabledInput.checked = profileOptions.maxResourceSizeEnabled;
  470. maxResourceSizeInput.value = profileOptions.maxResourceSize;
  471. maxResourceSizeInput.disabled = !profileOptions.maxResourceSizeEnabled;
  472. confirmFilenameInput.checked = profileOptions.confirmFilename;
  473. filenameConflictActionInput.value = profileOptions.filenameConflictAction;
  474. removeAudioSrcInput.checked = profileOptions.removeAudioSrc;
  475. removeVideoSrcInput.checked = profileOptions.removeVideoSrc;
  476. displayInfobarInput.checked = profileOptions.displayInfobar;
  477. displayStatsInput.checked = profileOptions.displayStats;
  478. backgroundSaveInput.checked = profileOptions.backgroundSave;
  479. autoSaveDelayInput.value = profileOptions.autoSaveDelay;
  480. autoSaveDelayInput.disabled = !profileOptions.autoSaveLoadOrUnload && !profileOptions.autoSaveLoad;
  481. autoSaveLoadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveLoad;
  482. autoSaveLoadOrUnloadInput.checked = profileOptions.autoSaveLoadOrUnload;
  483. autoSaveUnloadInput.checked = !profileOptions.autoSaveLoadOrUnload && profileOptions.autoSaveUnload;
  484. autoSaveLoadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  485. autoSaveUnloadInput.disabled = profileOptions.autoSaveLoadOrUnload;
  486. removeAlternativeFontsInput.checked = profileOptions.removeAlternativeFonts;
  487. removeAlternativeImagesInput.checked = profileOptions.removeAlternativeImages;
  488. groupDuplicateImagesInput.checked = profileOptions.groupDuplicateImages;
  489. removeAlternativeMediasInput.checked = profileOptions.removeAlternativeMedias;
  490. infobarTemplateInput.value = profileOptions.infobarTemplate;
  491. confirmInfobarInput.checked = profileOptions.confirmInfobarContent;
  492. removeFramesInput.disabled = saveRawPageInput.checked;
  493. loadDeferredImagesInput.disabled = saveRawPageInput.checked;
  494. }
  495. function getProfileText(profileName) {
  496. return profileName == singlefile.config.DEFAULT_PROFILE_NAME ? browser.i18n.getMessage("profileDefaultSettings") : profileName == singlefile.config.DISABLED_PROFILE_NAME ? browser.i18n.getMessage("profileDisabled") : profileName;
  497. }
  498. async function update() {
  499. await pendingSave;
  500. pendingSave = singlefile.config.updateProfile(profileNamesInput.value, {
  501. removeHiddenElements: removeHiddenElementsInput.checked,
  502. removeUnusedStyles: removeUnusedStylesInput.checked,
  503. removeUnusedFonts: removeUnusedFontsInput.checked,
  504. removeFrames: removeFramesInput.checked,
  505. removeImports: removeImportsInput.checked,
  506. removeScripts: removeScriptsInput.checked,
  507. saveRawPage: saveRawPageInput.checked,
  508. compressHTML: compressHTMLInput.checked,
  509. compressCSS: compressCSSInput.checked,
  510. loadDeferredImages: loadDeferredImagesInput.checked,
  511. loadDeferredImagesMaxIdleTime: Math.max(loadDeferredImagesMaxIdleTimeInput.value, 0),
  512. contextMenuEnabled: contextMenuEnabledInput.checked,
  513. filenameTemplate: filenameTemplateInput.value,
  514. shadowEnabled: shadowEnabledInput.checked,
  515. maxResourceSizeEnabled: maxResourceSizeEnabledInput.checked,
  516. maxResourceSize: Math.max(maxResourceSizeInput.value, 0),
  517. confirmFilename: confirmFilenameInput.checked,
  518. filenameConflictAction: filenameConflictActionInput.value,
  519. removeAudioSrc: removeAudioSrcInput.checked,
  520. removeVideoSrc: removeVideoSrcInput.checked,
  521. displayInfobar: displayInfobarInput.checked,
  522. displayStats: displayStatsInput.checked,
  523. backgroundSave: backgroundSaveInput.checked,
  524. autoSaveDelay: Math.max(autoSaveDelayInput.value, 0),
  525. autoSaveLoad: autoSaveLoadInput.checked,
  526. autoSaveUnload: autoSaveUnloadInput.checked,
  527. autoSaveLoadOrUnload: autoSaveLoadOrUnloadInput.checked,
  528. removeAlternativeFonts: removeAlternativeFontsInput.checked,
  529. removeAlternativeImages: removeAlternativeImagesInput.checked,
  530. removeAlternativeMedias: removeAlternativeMediasInput.checked,
  531. groupDuplicateImages: groupDuplicateImagesInput.checked,
  532. infobarTemplate: infobarTemplateInput.value,
  533. confirmInfobarContent: confirmInfobarInput.checked
  534. });
  535. await pendingSave;
  536. await singlefile.ui.menu.refresh();
  537. }
  538. })();