ui-options.js 39 KB

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