ui-options.js 36 KB

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