ui-options.js 35 KB

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