ui-options.js 37 KB

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